/** * FileTest --- reads input from the keyboard and saves it to a file. * @author Blanca J. Polo */ import java.io.*; class FileTest2{ /** * FileTest2 Reads input from the user and saves it into a file * We use a while loop to ask the user. We will keep on asking for * input until the user types all by itself (empty string). * @param arg A string array containing the command line arguments. * @return No return value. * @exception Exception If there are any problems with user input */ public static void main (String arg[ ]) throws Exception{ //variables needed to write to a file String sFilename = "myFile"; File f = new File(sFilename); FileOutputStream fos = new FileOutputStream(f); PrintStream ps = new PrintStream(fos); //variables needed to read from the user InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); String oneLine=""; System.out.println("writing to a file"); System.out.println("\n... Enter the first line please "); oneLine = br.readLine( ); //while the user doesn't type the empty string while (!oneLine.equals("")){ ps.println(oneLine); System.out.println("Enter the next line please... "); oneLine = br.readLine( ); }//while System.out.println("\n\nTo see the file that contains your data"); System.out.println("you may edit the file called myFile"); } //main } // class