我想计算文件中的行数,并将其拆分为标记。我似乎无法让我的代码工作,有人可以提供建议。提前致谢
import java.util.*;
import java.io.*;
public class kup
{
public static void main(String args[]) throws Exception
{
FileReader fileInput = new FileReader("C:\\save\\input.txt");
BufferedReader readInput = new BufferedReader(fileInput);
FileWriter fileOutput = new FileWriter("C:\\save\\output.txt");
PrintWriter outFile = new PrintWriter(fileOutput);
Scanner scanLine = new Scanner(readInput);
String textInput = scanLine.nextLine();
StringTokenizer stringtokenizer = new StringTokenizer(textInput);
int tokenCount = stringtokenizer.countTokens();
int lineCount = 0;
while(scanLine.hasNextLine())
{
while(stringtokenizer.hasMoreTokens())
{
String string = stringtokenizer.nextToken();
outFile.println(string);
}
lineCount++;
}
outFile.println("Number of words: " +tokenCount);
outFile.println("Number of lines: " +lineCount);
readInput.close();
outFile.close();
}
}