我正在尝试检查是否已到达文本文档中的最后一行,以便在文本文档中写入简单的行。
我尝试了下面的代码,但收到此错误:
Exception in thread "main" java.util.NoSuchElementException: No line found
我在 hello.txt
文档中有文本,所以我不明白这个错误的含义。
我该如何解决这个问题?
感谢任何帮助。
代码:
public static void main(String[] args) {
try (PrintWriter writer = new PrintWriter("D:\\hl_sv\\hello2.txt");
Scanner scanner = new Scanner("D:\\hl_sv\\hello.txt")) {
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
// In the case the line is the last one and there is no number.
// The content of the arrayList must be printed.
if ((line = scanner.nextLine()).isEmpty()) {
System.out.println("last line in the text document");
writer.write("last line in the text document.");
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
编辑
public static void main(String[] args) {
try (PrintWriter writer = new PrintWriter("D:\\hl_sv\\hello2.txt");
Scanner scanner = new Scanner("D:\\hl_sv\\hello.txt")) {
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
// In the case the line is the last one and there is no number.
// The content of the arrayList must be printed.
if (line.isEmpty()) { // <-- Removed = scanner.nextLine()
System.out.println("last line in the text document");
writer.write("last line in the text document.");
}else{
writer.write(line);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
使用编辑代码,我不再收到错误,但我在文本文档 hello2.txt
中打印了 D:\hl_sv\hello.txt
行?
最佳答案
已更新
我在我的计算机上尝试了这段代码,根据您的规范,它工作正常。
这是我的代码:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
class Test {
public static void main(String[] args) {
try {
PrintWriter writer = new PrintWriter(new File(
"../Test/src/com/hello2.txt"));
Scanner scanner = new Scanner(new File("../Test/src/com/hello.txt"));
while (scanner.hasNextLine()) {
String line = scanner.nextLine()+"\n";
System.out.println(line);
writer.write(line);
}
System.out.println("last line in the text document");
writer.write("last line in the text document.");
scanner.close();
writer.close();
} catch (FileNotFoundException e) {
System.out.println(e);
}
}
}
已编辑
您应该添加新文件
new PrintWriter(new File("D:\\hl_sv\\hello2.txt"));
和
new Scanner(new File("D:\\hl_sv\\hello.txt")));
因为PritWriter
需要File
作为参数,而Scanner
需要String
或File
作为论证。由于您在 Scanner ( "D:\\hl_sv\\hello.txt")
中仅使用 String
,因此您得到了“D:\hl_sv\hello.txt” hello2.txt 文件中的行。
并且您可以使用在主函数之后立即声明的 line
变量来访问主类中的 line
变量。
所以你的代码应该是:
public static void main(String[] args) {
String line="";
try (PrintWriter writer = new PrintWriter(new File("D:\\hl_sv\\hello2.txt"));
Scanner scanner = new Scanner(new File("D:\\hl_sv\\hello.txt"))) {
while (scanner.hasNextLine()) {
line = scanner.nextLine();
// In the case the line is the last one and there is no number.
// The content of the arrayList must be printed.
}
System.out.println("last line in the text document");
writer.write("last line in the text document.");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
您可以通过非常简单的方式检查该文本文件的最后一行/逻辑只需在 while
循环之后执行您的操作。因为在代码中的 while 循环之后,文本文件中没有任何行。
public static void main(String[] args) {
try (PrintWriter writer = new PrintWriter(new File("D:\\hl_sv\\hello2.txt"));
Scanner scanner = new Scanner(new File("D:\\hl_sv\\hello.txt"))) {
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
// In the case the line is the last one and there is no number.
// The content of the arrayList must be printed.
}
System.out.println("last line in the text document");
writer.write("last line in the text document.");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
当您使用此声明时
if ((line = scanner.nextLine()).isEmpty())
扫描仪找不到任何行(在文件末尾之后),因此不存在这样的元素(下一行),因此出现此异常
Exception in thread "main" java.util.NoSuchElementException: No line found
关于java - 检查该行是否是文本文档中的最后一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30714916/