-2

Here is my problem. The path is true and the error is access denied. I tried other solution but none of them work for me.

//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();
    } 
Caner Taşan
  • 71
  • 1
  • 1
  • 3

2 Answers2

1

Desktop isn't a file, it's a folder FileReader should be given a file name as parameter

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();
}
Flavio Rossi
  • 101
  • 1
  • 5
1

The pathname refers to a folder not a file, and you cannot open a folder as a Reader.

The solution depends on what you are trying to do.

  • If you are trying to read the names of the files in the folder, then use File.list() -> String[] and iterate the array.

  • If you are trying to read the content of all of the files in the folder, then use File.listFiles() -> File[] and iterate the array. For each file, open, read lines and then close the file.

  • If you are trying to read the content of a specific file in the Desktop folder, then use the pathname for the file, not the folder.

Stephen C
  • 632,615
  • 86
  • 730
  • 1,096