【发布时间】: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