Wednesday, October 7, 2015

Java scanner utility to take input from console and save inputs in a file

This utility takes keyboard input what you type in console and saves all the string in a file

This is what i am inputting in the console

And this is where it is getting saved in a file



 package Scanner;  
 import java.io.BufferedOutputStream;  
 import java.io.File;  
 import java.io.FileNotFoundException;  
 import java.io.FileOutputStream;  
 import java.io.IOException;  
 import java.util.ArrayList;  
 import java.util.Iterator;  
 import java.util.Scanner;  
 public class ScannerHelper {  
      String line;  
      public static void main(String[] args) throws IOException {  
           // TODO Auto-generated method stub  
           new ScannerHelper().scanner();  
      }  
      public void scanner() throws IOException {  
           Scanner scan = new Scanner(System.in);  
           File f = new File("C:/Test/createdbyscanner.txt");  
           BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(f));  
           if (f.exists()) {  
                f.delete();  
           }  
           boolean condition = true;  
           ArrayList<String> arraylist = new ArrayList<String>();  
           while (condition) {  
                line = scan.nextLine();  
                arraylist.add(line);  
                if (line.equals(".")) {  
                     break;  
                }  
           }  
           Iterator<String> itr = arraylist.iterator();  
           while (itr.hasNext()) {  
                String scannerLine = itr.next().toString();  
                // System.out.println(scannerLine);  
                try {  
                     System.out.println("from scanner " + scannerLine);  
                     bos.write(scannerLine.getBytes());  
                     bos.write(' ');  
                     bos.write('\n');  
                } catch (IOException e) {  
                     // TODO Auto-generated catch block  
                     e.printStackTrace();  
                }  
           }  
           bos.flush();  
           bos.close();  
      }  
 }  

No comments:

Post a Comment