Java reading txt.file - access denied?
Asked Answered
L

4

6

I have created a txt file which I have saved in My Documents on my computer. I am trying to read the txt file through FileReader and BufferedReader. However, when I try to run the program I get the error message:

java.io.FileNotFoundException: <filelocation> (Access is denied)

Does anyone know what causes this, and how I might fix the problem? I have tried saving the document other places too, but I always get this message. I am sure the file path is correclty entered.

Longford answered 25/10, 2011 at 18:29 Comment(5)
I think that exception tell everything "FileNotFoundException". Can You show the code plz?Abuttals
This might help: #4281643Chapnick
Can you add the relevant code of where you are accessing the file? Your exception message is also missing some info (ie. the file name) ... can you add the full stack trace as well?Pedigo
Thank you for the comments. As far as the entire error message goes, it is as follows: Error upon reading: java.io.FileNotFoundException: C:\Users\Kristian\Documents (Access is denied). Most of my code is not written in English, however, so I would need some time if you wish for me to put up the code in its entirety. I can perhaps try some of the methods suggested in the link provided by G_HLongford
Do you close the stream properly after reading? "C:\Users\Kristian\Documents (Access is denied)" looks like you have problems with the spaces in the path. Please post the line where the file name is created.Luzon
L
5

One random guess is that you may be having multiple instance of your test program running so the file is locked by earlier instance of your program which is still running. You can check open consoles in the eclipse or you can restart eclipse.

Leitao answered 25/10, 2011 at 18:45 Comment(3)
Thanks a lot. This actually helped! The only thing now is that I get something a bit mystifying in the outprint. My outprint says: " Student 20: 30.4 Student 19: 87.6 Student 21: 91.34" However, the symbols  are not meant to appear. What could cause this?Longford
It may be that your file is having some character that the eclipse console does not support. You can configure the encoding for the console in run-as dialog. Can you update the question with the contents of file and the program incase the problem persists.Leitao
Hi. Thanks again. I tried opening the file in a different Eclipse program which I also use for reading textfiles, and here everything runs smoothly. Thus, since I am now able to run the file properly here, I think I will just leave it at that. I really appreciate your help though!Longford
L
12

java.io.FileNotFoundException: C:\Users\Kristian\Documents (Access is denied)

You are trying to read a directory, not a .txt file.

Leishaleishmania answered 25/10, 2011 at 23:19 Comment(1)
This was also my issue. Why can't they just say "folder was specified instead of file"???Lucilla
L
5

One random guess is that you may be having multiple instance of your test program running so the file is locked by earlier instance of your program which is still running. You can check open consoles in the eclipse or you can restart eclipse.

Leitao answered 25/10, 2011 at 18:45 Comment(3)
Thanks a lot. This actually helped! The only thing now is that I get something a bit mystifying in the outprint. My outprint says: " Student 20: 30.4 Student 19: 87.6 Student 21: 91.34" However, the symbols  are not meant to appear. What could cause this?Longford
It may be that your file is having some character that the eclipse console does not support. You can configure the encoding for the console in run-as dialog. Can you update the question with the contents of file and the program incase the problem persists.Leitao
Hi. Thanks again. I tried opening the file in a different Eclipse program which I also use for reading textfiles, and here everything runs smoothly. Thus, since I am now able to run the file properly here, I think I will just leave it at that. I really appreciate your help though!Longford
M
0

there could be 2 types of issue.

  1. your path is containing space in folder or file name. you can solve it by using the non-8dot3 file name by using the command dir /p /x in command prompt of that folder path.
  2. you can save the form in your project folder and use relative path ./txt.file. if you have the file under a path say <project folder>/input/txt.file, your relative path will be ./input/txt.file. If this doesn't work try googling the code for file reading and you will find out your mistake.
Murchison answered 25/10, 2011 at 18:59 Comment(3)
Thanks a lot. Appreciate your tips. Together with the help above, I am now able to run this properly in one of my Eclipse programs.Longford
@EJP Please find justification for my answer File file = new File("./ src/CollectionUtils.java"); System.out.println(file.getAbsolutePath() + file.isFile()); File file1 = new File("./src/CollectionUtils.java"); System.out.println(file1.getAbsolutePath() + file1.isFile()); **sample output ** /Users/nbabu/workspace/FunProject/./ src/CollectionUtils.javafalse /Users/nbabu/workspace/FunProject/./src/CollectionUtils.javatrueMurchison
@NaveenBabu What answer? Neither of the points in your answer identifies the actual problem, and the mess of code in your comment remains unexplained.Leishaleishmania
E
-1

Could be that you have to change the security settings on your folder. Go into the directory, right click properties, select Security , advances and change permission so that all child dependencies inherit the security changes.

Esque answered 28/3, 2015 at 11:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.