Task: overwrite file 1 to file 2, all words, but one on each line, without any punctuation (using StringTokenizer).

I have completed all the items of the task except one. Write to output file.

import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.StringTokenizer; public class лаба2 { public static void main(String[] args) throws FileNotFoundException, IOException { String s; int l=0; BufferedReader in = new BufferedReader(new FileReader("F:/1.txt")); while ((s = in.readLine()) != null) { l++; StringTokenizer st; st = new StringTokenizer(s, " \t\n\r,.?"); while(st.hasMoreTokens()) { System.out.println(st.nextToken()); } } } } 

I can not catch up where to insert the file entry.

  • After reading from file 1 - bsuart

1 answer 1

What is the problem, we read the line, parse the tokens and immediately write to another file:

 //... BufferedReader in = new BufferedReader(new FileReader("F:/1.txt")); Writer wr = new FileWriter("F:/2.txt"); while ((s = in.readLine()) != null) { l++; StringTokenizer st = new StringTokenizer(s, " \t\n\r,.?"); while(st.hasMoreTokens()) { wr.write(st.nextToken()); wr.write(System.lineSeparator()); } } wr.close(); //...