【问题标题】:Java BufferedReader FileReader issueJava BufferedReader FileReader 问题
【发布时间】:2014-03-13 14:47:28
【问题描述】:

所以我有以下代码:

import java.io.*;

public class Plagiarism {

    public static void main(String[] args) {

        Plagiarism myPlag = new Plagiarism();

        if  (args.length == 0) {
            System.out.println("Error: No files input");
        }
        else if (args.length > 0) {
            try {
                for (int i = 0; i < args.length; i++) {
                    BufferedReader reader = new BufferedReader (new FileReader (args[i]));
                    simplify (reader);
                    reader.close();
                }
            }
            catch (Exception e) {
                System.err.println ("Error reading from file");
            }
        }
    }

    public static void simplify(BufferedReader input) throws IOException {
        String line = null;

        line = input.readLine();
        while (line != null) {
            line = line.replaceAll ("[^a-zA-Z0-9 ]", "");
            line = line.toLowerCase();
        }    
    }

}

此代码的问题在于它可以编译,但是当我运行它并在命令行中添加 2 个参数时,例如。 Java 抄袭 text1.txt text2.txt。编辑:当我运行它时,它什么也不做,甚至没有完成,就像它卡在某个地方一样。

感谢您的帮助。

【问题讨论】:

  • 在您的 catch 子句中添加 System.out.println(e.getMessage()); 以了解异常是什么。
  • 首先不要抓Exception,而是抓一个更具体的。 Exception 还捕获 RuntimeException 并派生出所有未检查的异常。
  • 打印您的 Stacktrace (e.printStackTrace())。
  • 你的 text1.txt 和 text2.txt 在哪里?你收到 FileNotFoundException 了吗?
  • 对不起,我编辑了这个问题。文本文件位于同一目录中。

标签: java try-catch bufferedreader filereader


【解决方案1】:

您不是一次读取文件(您需要使用线程来执行此操作)。

问题出在您的 simplify 方法中。

line = input.readLine();
while (line != null) {

...应该变成:

while ((line = input.readLine()) != null)

原因是你只调用了一次readLine,否则只迭代第一行的值。

使用正确的while 循环,您可以在将readLine 调用的值分配给line 变量后将其传递给非null 条件。

然后,您可以按照您在评论中的建议,使用您在 while 循环中操作的 line String 做任何您想做的事情,例如将其添加到 array 或 Collection。

例如:

public static List<String> simplify(BufferedReader input) throws IOException {
    String line = null;
    List<String> result = new ArrayList<String>();

    while ((line = input.readLine()) != null) {
        result.add(line.replaceAll ("[^a-zA-Z0-9 ]", "").toLowerCase());
    }    
    return result;
}

...然后在您的main 方法中...

List<String> foo = simplify(reader);

【讨论】:

  • 感谢您的回答。但是,对于第二个问题,您是什么意思?这段代码肯定可以工作吗?如果它不能工作,我怎样才能让它与读者一起工作?谢谢
  • 我明白了。我不想编辑文件 - 我想先将 2 个文本文档放入数组中,然后编辑这些数组,这就是我正在尝试的。
  • @user3364788 啊哈,我的错。那么你不需要需要作家。让我再编辑一次。
  • 在这个while循环之后肯定应该填充“行”吗?当我尝试打印“行”时,它仍然为空......
  • @user3364788 它被填充在内部 while 循环中!在line 之前阅读的每一行都是null。在循环之外它将是null,因为null相等是循环退出条件。
【解决方案2】:

编辑:当我运行它时,它什么也不做,甚至没有完成,就像它卡在某个地方一样。

这是完全正常的。看看你的while 循环:

    while (line != null) {
        line = line.replaceAll ("[^a-zA-Z0-9 ]", "");
        line = line.toLowerCase();
    }

line 在这里永远不会为空。你应该:

while ((line = input.readLine()) != null)
    // etc 

您有一个更根本的问题:如果您的目标是替换输入文件中的行,您的程序将永远无法正常工作...

试试这个:

private static final Pattern PATTERN = Pattern.compile("[^a-zA-Z0-9 ]+");

private static void simplify(final String fileName)
    throws IOException
{
    final Path path = Paths.get(fileName);
    final Path tempfile = Files.createTempFile(fileName.getFileName(), "tmp");
    try (
        final BufferedReader reader = Files.newBufferedReader(path);
        final BufferedWriter writer = Files.newBufferedWriter(tempfile);
    ) {
        String line;
        while ((line = reader.readLine()) != null) {
            line = PATTERN.matcher(line).replaceAll("").toLowerCase();
            writer.write(line);
            writer.newLine();
        }
        writer.flush();
    }
    Files.move(tempfile, path, StandardCopyOption.REPLACE_EXISTING);
}

【讨论】:

  • 感谢您的回答。虽然我不认为这不是我需要的。从根本上说,我是在比较两个文本文档,所以我不会替换文件中的内容,而是使用它然后编辑数组中的内容。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多