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.

5
  • 2
    I think that exception tell everything "FileNotFoundException". Can You show the code plz? Commented Oct 25, 2011 at 18:32
  • 1
    This might help: stackoverflow.com/questions/4281143/… Commented Oct 25, 2011 at 18:32
  • 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? Commented Oct 25, 2011 at 18:34
  • 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_H Commented Oct 25, 2011 at 18:37
  • 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. Commented Oct 25, 2011 at 18:50

4 Answers 4

13

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

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

Sign up to request clarification or add additional context in comments.

1 Comment

This was also my issue. Why can't they just say "folder was specified instead of file"???
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.

3 Comments

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?
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.
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!
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.

3 Comments

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.
@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.javatrue
@NaveenBabu What answer? Neither of the points in your answer identifies the actual problem, and the mess of code in your comment remains unexplained.
-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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.