我正在尝试将标记化的字符串保存到 .txt 文件中...例如,inRead.txt
是:
She sells, sea shells, by the sea shore.
我让我的程序对其进行标记,但我似乎无法将标记化的字符串保存到Write.txt
在Write.txt
我刚刚得到:
She sells, sea shells, by the sea shore.
基本上我想保存我输出的内容Write.txt
任何帮助将不胜感激:D
这是我的输出:
She sells, sea shells, by the sea shore.
[Split by spaces.]
She
sells,
sea shells,
by
the
sea
shore.
-----------------------------
[Split by commas.]
She sells
sea shells
by the sea shore.
而我目前的代码:
import java.io.*;
import java.util.StringTokenizer;
public class readWriteTokenized{
public static void main( String[] args ){
String readString;
try{
BufferedReader br = new BufferedReader( new FileReader("Read.txt"));
readString = br.readLine();
System.out.println("\n" + readString);
br.close();
StringTokenizer stnz = new StringTokenizer(readString);
System.out.println("\n[Split by spaces.]\n");
while( stnz .hasMoreTokens()){
System.out.println(stnz .nextToken());
}
StringTokenizer stnz2 = new StringTokenizer(readString, ".");
System.out.println("\n-----------------------------");
System.out.println("\n\n[Split by comma.]\n");
while( stnz2.hasMoreTokens()){
System.out.print(stnz2.nextToken());
}
File NewTextFile = new File("C:/TestJava/Write.txt");
FileWriter fw = new FileWriter(NewTextFile);
fw.write(readString);
fw.close();
}
catch (IOException e){
System.out.println("Catch error!");
}
}
}