[英]Java: BufferedReader not printing out File content on TXT file
I'm having issues with BufferedReader reading out the content of a txt file within a folder which is called via a method showEditFile()
using an array with input of the user from method pideNumero.preguntaUno();
我在使用 BufferedReader 读取文件夹中 txt 文件的内容时遇到问题,该文件夹通过方法
showEditFile()
调用,该方法使用带有来自方法pideNumero.preguntaUno();
which takes an int to iterate through the array positions:它需要一个 int 来遍历数组位置:
Array that loops through the Folder "Archivos".遍历文件夹“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;
}
Method which would read the first line of the content which should be Hellowwwww :读取内容的第一行的方法应该是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());
}
}
I tried to check in Debug mode an see that on line FileReader fr = new FileReader(document);
我试图在调试模式下检查并在线查看
FileReader fr = new FileReader(document);
it would jump straight into the FileNotFoundException with the FilePath == null which I think comes the problem from.它会使用 FilePath == null直接跳入 FileNotFoundException,我认为这是问题所在。
It seems it doesn't know the path after "Archivos"似乎它不知道“Archivos”之后的路径
Path: Root\Archivos\kiki.txt路径:根\Archivos\kiki.txt
I've been stuck on this for a full day now can someone please help!我已经坚持了一整天了,有人可以帮忙吗!
carpeta.list()
does not give fully qualified path. carpeta.list()
没有给出完全限定的路径。 It only gives you file name.它只给你文件名。 So next call
new File(archivos[menu - 1])
will fail.所以下一次调用
new File(archivos[menu - 1])
将失败。 In new File(archivos[menu - 1])
you will need to provide full and then you will not get Exception.在
new File(archivos[menu - 1])
中,您需要提供完整的,然后您将不会得到异常。 Refer to https://docs.oracle.com/javase/7/docs/api/java/io/File.html#list()参考https://docs.oracle.com/javase/7/docs/api/java/io/File.html#list()
So thanks to @Jags I figured the issue out.所以感谢@Jags,我解决了这个问题。 Solution below:
解决方案如下:
In my array I was returning a String[] which saves basically strings... So when I try to call it from my other method to read out the content it would show me the name of the file (which is a string but wouldn't have a file path assigned to it because it's just a string).在我的数组中,我返回了一个基本上保存字符串的 String[]...没有分配给它的文件路径,因为它只是一个字符串)。
Changes I made here:我在这里所做的更改:
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;
}
Now when calling the method in my other method showEditFiles():现在在我的另一个方法 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());
}
}
Now it prints out the first line of the file (which in this case was hellow).现在它打印出文件的第一行(在本例中是hello)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.