/* Program: FileIO.java This program reads successive lines of text from an input file until eof, tokenizes each line, and writes the tokens one per line to an output file. In this example, the input file used is the program itself, so the output file created (FileIO.txt) contains all the tokens found in the program. Note in line 38 that a path is specified for the input file: "C:\\Documents and Settings\\Greg\\Desktop\\2210\\FileIO.java" If the file does not exist in the specified folder, or if there is an error in the path (i.e., if a particular folder does not exist in the specified "parent" folder or on the specified drive) then a FileNotFoundException will be thrown. Note in line 42 that no path is specified for the output file, just the file name. Therefore the file will be written to your NetBeans project folder. */ import java.io.* ; // for FileWriter, PrintWriter, and IOException import java.util.Scanner ; /** * A class to demonstrate basic I/O of sequential text files. */ public class FileIO { public static void main(String[] args) throws IOException { // note required "throws" clause in main method heading, above // create a Scanner object associated with the text file to be read. Scanner reader = new Scanner (new File("C:\\Documents and Settings\\Greg\\Desktop\\2210\\FileIO.java")) ; // create PrintWriter object for output file PrintWriter outfile = new PrintWriter( new FileWriter("FileIO.txt") ) ; // read lines from the input file until eof... while( reader.hasNext() ) // while not eof... { String line = reader.nextLine() ; // create Scanner object for the line just read... Scanner lineScan = new Scanner(line) ; while ( lineScan.hasNext() ) // while more tokens... { String token = lineScan.next() ; // ...get next token outfile.println(token) ; // ...write token to file } } // Must remember to close file when done, or data may be lost! outfile.close() ; } } // to see the program output, open the file "FileIO.txt", located in // your NetBeans project folder