0

我想计算文件中的行数,并将其拆分为标记。我似乎无法让我的代码工作,有人可以提供建议。提前致谢

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();
    }

}
4

1 回答 1

5

您只使用第一行初始化标记器。我怀疑你想要类似的东西:

int tokenCount = 0;
int lineCount = 0;

while (scanLine.hasNextLine())
{
    String line = scanLine.nextLine();
    StringTokenizer tokenizer = new StringTokenizer(line);

    while (tokenizer.hasMoreTokens())
    {
        String string = tokenizer.nextToken();
        outFile.println(string);
        tokenCount++;
    }
}

就我个人而言,我可能会坚持BufferedReader“逐行阅读”——你并没有真正使用Scanner这里的任何细节。所以:

String line;
while ((line = readInput.readLine()) != null)
{
    StringTokenizer tokenizer = new StringTokenizer(line);

    while (tokenizer.hasMoreTokens())
    {
        String string = tokenizer.nextToken();
        outFile.println(string);
        tokenCount++;
    }
}

请注意,您也应该关闭您的 reader/writers/streams finally 块,或者如果您使用的是 Java 7,则使用 try-with-resources 语句。

于 2012-06-23T09:43:11.487 回答