Java 检查该行是否为文本文档中的最后一行
我正在尝试检查是否已到达文本文档中的最后一行,以便在文本文档中编写简单的一行 我尝试了以下代码,但出现了此错误: 线程“main”java.util.NoSuchElementException中出现异常:未找到任何行 我在Java 检查该行是否为文本文档中的最后一行,java,Java,我正在尝试检查是否已到达文本文档中的最后一行,以便在文本文档中编写简单的一行 我尝试了以下代码,但出现了此错误: 线程“main”java.util.NoSuchElementException中出现异常:未找到任何行 我在hello.txt文档中有文本,所以我不明白这个错误是什么意思 我怎样才能解决这个问题 谢谢你的帮助 代码: public static void main(String[] args) { try (PrintWriter writer = new PrintWri
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();
}
}
publicstaticvoidmain(字符串[]args){
try(PrintWriter=newprintWriter(“D:\\hl\u sv\\hello2.txt”);
Scanner Scanner=新扫描仪(“D:\\hl\u sv\\hello.txt”)){
while(scanner.hasNextLine()){
字符串行=scanner.nextLine();
//在这种情况下,该行是最后一行,没有编号。
//必须打印arrayList的内容。
如果(line.isEmpty()){/请查看链接文章以了解更多信息
基本上,hasNextLine()函数将告诉您是否已到达文件末尾(您似乎已经在使用该文件)。如果您希望将其作为一项功能,我会说稍微重新安排您的循环以合并它。您似乎正在执行以下操作
line = scanner.nextLine();
每圈两次
根据这些信息,删除其中一个调用将解决您的问题。您的问题是每次调用.nextLine()
两次,都会将扫描仪移动两行
修正:
publicstaticvoidmain(字符串[]args){
try(PrintWriter=newprintWriter(“D:\\hl\u sv\\hello2.txt”);
Scanner Scanner=新扫描仪(“D:\\hl\u sv\\hello.txt”)){
while(scanner.hasNextLine()){
字符串行=scanner.nextLine();
//在这种情况下,该行是最后一行,没有编号。
//必须打印arrayList的内容。
如果(line.isEmpty()){/只需测试hasNextLine()
一个相关的、相当有趣的话题是,如果我们使用文件.lines(path)
,这是一个流
,我们如何检测最后一行:)已更新
我在我的电脑上试用了这段代码,它的工作性能符合你的要求
这是我的密码:
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"));
及
因为PritWriter
需要File
作为参数,而Scanner
需要String
或File
作为参数。因为您在Scanner(“D:\\hl\u sv\\hello.txt”)
中只使用了String
,所以您在hello2.txt文件中得到了“D:\hl\u sv\hello.txt”行
您可以使用紧跟在main函数之后声明的line
变量来访问main类中的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())
扫描器
找不到任何行(在文件结尾之后),因此不存在这样的元素(下一行),因此出现此异常
线程“main”java.util.NoSuchElementException中出现异常:未找到任何行
对于在文件末尾添加文本的简单任务来说,这是许多不必要的代码。.编辑代码后,您是否会遇到相同的异常?没有异常,但是hello.txt中的行没有打印在hello2.txt中,除了这一行D:\hl_sv\hello.txt
,它不是我的hello.txt文档的一部分?!我想阅读file,然后在结尾写一些东西,所以我需要while语句。是的,你可以读取一个文件,然后在while循环中写入。但是你也可以在while循环之外执行。因为文件没有关闭,但是如果我在while循环之外执行,那么行
VARABLE是未知的。请你修改你的答案,用你的方式将其文本化。我再次更新了我的答案并显示了我在我的计算机中尝试过的代码,它符合您的规范。请检查。您必须将这一行writer.println(line);
添加到while循环中,以便在新的循环中打印hello.txt的行,否则它可以正常工作(hanks;)
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();
}
}
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())