我有个问题。我基本上被要求使用一种方法从输出文件中打印。所以一开始我实际上开始用行填充输出文件..然后当我尝试实际阅读它们并将它们打印在屏幕上时,出现了一个小问题。我实际上使用了“调试”选项,结果问题出在“line = input.nextLine()”代码行中,但我真的不知道为什么……我的意思是这就是我们从输出中读取的方式文件...帮助将不胜感激。
到目前为止,这是我的工作:
import java.util.*;
import java.io.*;
public class Problem_3 {
public static void main(String[] args) {
PrintWriter outFile = null;
File f1 = new File("try.txt");
try {
outFile = new PrintWriter("try.txt");
outFile.println("First line!");
outFile.println("Second line!");
outFile.println("Third line!");
cat(f1);
} catch (Exception e) {
outFile.print("(1) Exception: " + e.getMessage()); // no such element exception
}
outFile.close();
}
/*
* outFile = new PrintWriter(f1); outFile.println("Line 1");
* outFile.println("Line 2"); outFile.println("Line 3"); outFile.print("");
* cat(f1); } catch (Exception e) { System.out.println("(1)Exception: " +
* e.getMessage()); } outFile.close(); }
*/
public static void cat(File file) throws FileNotFoundException {
Scanner input = null;
String line = "";
input = new Scanner(new FileReader(file));
//line = input.next();
// line = input.nextLine();// this line calls the exception in the main method
while ((line != null)) {
System.out.println("In the while loop");
System.out.println("Line 323" + line);
return;
}
input.close();
}
}