问题:当运行以下代码时,应当输出文件全部内容,然而:
文件内容:
1
2
3
输出:
1
代码:
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Scanner;
public class ReadClass {
public static final long versions = 1;
public static final String version = "V1.0.0";
public static String position = "";
private static Scanner scan = new Scanner(System.in);
public static void main(String[] args) throws IOException, InterruptedException {
println("阅读软件 " + version);
println("输入 ‘help’ 获取帮助");
while(true) {
print("输入命令:");
String cmd = scan.next();
if (cmd.equals("help") || cmd.equals("help help")) {
println("read 开始阅读");
println("setup 设置文件路径");
println("help 获取本帮助");
println("exit 退出程序");
println("使用 ‘help [命令]’ 获取详细信息");
} else if (cmd.equals("help read")) {
println("会开始显示指定路径中的txt文件");
println("按照文件名1,2,3,4···的顺序显示");
println("用 ‘setup’ 命令调整路径");
println("默认路径为程序所在文件夹");
} else if (cmd.equals("help setup")) {
println("重新设置 ‘read’ 命令的文件路径");
println("默认路径为程序所在文件夹");
} else if (cmd.equals("help exit")) {
println("退出程序");
} else if (cmd.equals("exit")) {
break;
} else if (cmd.equals("setup")) {
println("输入新路径");
position = scan.next();
} else if (cmd.equals("read")) {
read(position);
} else {
println("'" + cmd + "'不是一个命令!");
}
}
}
private static void println(String a) {
System.out.println(a);
}
private static void println() {
System.out.println();
}
private static void print(String a) {
System.out.print(a);
}
private static void read(String p1) throws IOException, InterruptedException {
for (long a = 1;true;a++) {
try {
String txt = p1 + a + ".txt";
Path path = Paths.get(txt);
List<String> all = Files.readAllLines(path, StandardCharsets.UTF_8);
String[] f = all.toArray(new String[all.size()]);
String nr = f[(int) Math.floor(Math.random())];
println(nr);
println();
println("任何输入下一章,输入 ‘u’ 回车上一章,输入 ‘e’ 回车退出阅读");
String cmd = scan.next();
if (cmd.equals("u")) {
a -= 2;
} else if (cmd.equals("e")) {
break;
}
} catch (Exception e) {
println("error");
}
}
}
}
请问这是什么问题?如何解决?