我在BufferedReader读取文件夹中txt文件的内容时遇到问题,该文件夹是通过方法showEditFile()
调用的,该数组使用方法pideNumero.preguntaUno();
中用户输入的数组,该数组需要一个int来迭代数组位置:
循环遍历文件夹“ Archivos”的数组。
public static String[] testFiles() {
String endPath = System.getProperty("user.dir");
String separator = File.separator;
String folderPath = endPath + separator + "Archivos";
File carpeta = new File(folderPath);
String[] lista = carpeta.list();
return lista;
}
将读取内容第一行的方法,该内容应为Hellowwwww:
public static void showEditFile() throws IOException {
System.out.println("Por Favor, elige un archivo con su numero para mostrar su contenido ");
System.out.println("Los archivos dentro la carpeta Archivos son: ");
Menu.listFiles.nomFiles();
String[] archivos = Menu.listFiles.testFiles();
int menu = Menu.pideNumero.preguntaUno();
File document = new File(archivos[menu - 1]);
try {
FileReader fr = new FileReader(document);
BufferedReader br = new BufferedReader(fr);
String line;
line = br.readLine();
System.out.println(line);
} catch (FileNotFoundException e) {
System.out.println("File not found." + document.toString());
} catch (IOException e) {
System.out.println("Unable to read file: " + document.toString());
}
}
我尝试在Debug模式下检查,发现在FileReader fr = new FileReader(document);
行上它将直接跳到FilePath == null的FileNotFoundException中,我认为是问题出处。
似乎不知道“ Archivos”之后的路径
路径:Root \ Archivos \ kiki.txt
我已经坚持了整整一天,现在有人可以帮忙!
carpeta.list()
没有给出完全限定的路径。它只为您提供文件名。因此,下一个呼叫new File(archivos[menu - 1])
将失败。在new File(archivos[menu - 1])
中,您将需要提供完整的信息,然后您将不会获得Exception。请参考https://docs.oracle.com/javase/7/docs/api/java/io/File.html#list()