java.io.filenotfoundexception:joiners.txt(访问被拒绝)



这是我的问题。该路径是真实的,并且访问访问的错误。 我尝试了其他解决方案,但它们都不适合我。

//this is my arraylist which i give value from the txt
       ArrayList<Person> PersonArrayList = new ArrayList<Person>();     
        FileReader inFile = new 
        FileReader("C:\Users\canertasan\Desktop");
        //this is my path but access denied is problem?
        BufferedReader inStream = new BufferedReader(inFile);
        String InstaNameText;
        while ((InstaNameText = inStream.readLine()) != null) {
             PersonData.add(new Person(InstaNameText));
        inStream.close();
    } 

路径名是指文件夹而不是文件,并且您不能以Reader打开文件夹。

解决方案取决于您要做什么。

  • 如果您要读取文件夹中的文件名,请使用File.list() -> String[]并迭代数组。

  • 如果要读取文件夹中所有文件的内容,请使用File.listFiles() -> File[]并迭代数组。对于每个文件,打开,读取行,然后关闭文件。

  • 如果您要读取Desktop文件夹中特定文件的内容,请使用该文件的路径名而不是文件夹。

桌面不是文件,它是文件夹fileReader应作为参数

给出文件名。
String filename = "C:\Users\UserName\Desktop\Joiners.txt"; //Fullpath txt file
String currentLine; //Current line
FileReader fr = null;
BufferedReader br = null;
try {
    fr = new FileReader(filename);
    br = new BufferedReader(fr);

    while((currentLine = br.readLine()) != null){
        System.out.println(currentLine);
    }
}
catch(FileNotFoundException ex){
    ex.printStackTrace();
}
catch(IOException ex){
    ex.printStackTrace();
}
try {
    if (br != null)
        br.close();
    if (fr != null)
        fr.close();
}
catch (IOException ex) {
    ex.printStackTrace();
}

最新更新