Hoody
Posts: 33
Nickname: hoodlum
Registered: Feb, 2002
|
|
Can anyone tell me where I went wrong??
|
Posted: Mar 21, 2002 7:34 PM
|
|
i am supposed touse the string tokenizer so i can separte each word i put into the input file so it prints one word per line
// Reads text from an input file one line at a time, breaks the line into words // Then prints each word to another file, one word per line. // from an input file.
import java.io.FileReader; import java.io.BufferedReader; import java.io.FileWriter; import java.io.PrintWriter;
import java.io.IOException; import java.io.FileNotFoundException; import java.util.StringTokenizer;
public class ReadWriteFileAsTokens
{ public static void main(String[] args) throws IOException
{ // Initializations FileReader reader = null; FileWriter writer = null;
String inFileName = "TokensIn.txt"; String outFileName = "TokensOut.txt";
// Open input and output files try { reader = new FileReader(inFileName); writer = new FileWriter(outFileName); } catch(FileNotFoundException e) { System.err.println("Cannot find input file "); System.exit(1); // abnormal termination status code } catch(IOException e) { System.err.println("Cannot open input/output file " ); System.exit(2); }
// Set up to read a line and write a line BufferedReader in = new BufferedReader(reader); PrintWriter out = new PrintWriter(writer);
// Read a line from one file and write to the other out.println("Copied file is:");
boolean done = false; while(!done)
{ String inputLine = in.readLine();
if (inputLine == null) { done = true; } else { StringTokenizer tokenizer= new StringTokenizer(inputLine, "|"); String word = tokenizer.nextToken(); out.println(inputLine); } } // end of input // initialize a tokenizer and attach it to a line
// Close files try { in.close(); } catch(IOException e) { System.err.println("Error closing file."); } finally { out.close(); }
} }
|
|