Java: BufferedReader 未打印出 TXT 文件中的文件内容
Java: BufferedReader not printing out File content on TXT file
我在 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());
}
}
我尝试检查调试模式,发现在 FileReader fr = new FileReader(document);
行它会直接跳到 FileNotFoundException 中,我认为 FilePath == null问题来自.
好像不知道"Archivos"之后的路径
路径:
Root\Archivos\kiki.txt
我已经被困在这个问题上一整天了,有人可以帮忙吗!
carpeta.list()
没有给出完全限定的路径。它只给你文件名。所以下一次调用 new File(archivos[menu - 1])
将失败。在 new File(archivos[menu - 1])
中,您将需要提供完整的信息,然后您将不会获得异常。参考https://docs.oracle.com/javase/7/docs/api/java/io/File.html#list()
感谢@Jags,我解决了这个问题。解决方案如下:
在我的数组中,我返回了一个 String[],它基本上保存了字符串...所以当我尝试从我的其他方法调用它来读出内容时,它会显示文件的名称(这是一个字符串,但不会为其分配文件路径,因为它只是一个字符串)。
我在此处所做的更改:
public static File[] testFiles() {
String endPath = System.getProperty("user.dir");
String separator = File.separator;
String folderPath = endPath + separator + "Archivos";
File carpeta = new File(folderPath);
// here as you can see i used the listFiles() method to list all the files and
// and save them into the File[] array
File[] lista = carpeta.listFiles();
return lista;
}
现在在我的其他方法 showEditFiles() 中调用该方法时:
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();
File[] 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());
}
}
现在它打印出文件的第一行(在本例中是 hellow)。
我在 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());
}
}
我尝试检查调试模式,发现在 FileReader fr = new FileReader(document);
行它会直接跳到 FileNotFoundException 中,我认为 FilePath == null问题来自.
好像不知道"Archivos"之后的路径
路径: Root\Archivos\kiki.txt
我已经被困在这个问题上一整天了,有人可以帮忙吗!
carpeta.list()
没有给出完全限定的路径。它只给你文件名。所以下一次调用 new File(archivos[menu - 1])
将失败。在 new File(archivos[menu - 1])
中,您将需要提供完整的信息,然后您将不会获得异常。参考https://docs.oracle.com/javase/7/docs/api/java/io/File.html#list()
感谢@Jags,我解决了这个问题。解决方案如下:
在我的数组中,我返回了一个 String[],它基本上保存了字符串...所以当我尝试从我的其他方法调用它来读出内容时,它会显示文件的名称(这是一个字符串,但不会为其分配文件路径,因为它只是一个字符串)。
我在此处所做的更改:
public static File[] testFiles() {
String endPath = System.getProperty("user.dir");
String separator = File.separator;
String folderPath = endPath + separator + "Archivos";
File carpeta = new File(folderPath);
// here as you can see i used the listFiles() method to list all the files and
// and save them into the File[] array
File[] lista = carpeta.listFiles();
return lista;
}
现在在我的其他方法 showEditFiles() 中调用该方法时:
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();
File[] 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());
}
}
现在它打印出文件的第一行(在本例中是 hellow)。