-1

I'm trying to create a CSV file in a Java program. No matter where the file is located, I've this error:

Exception in thread "main" java.io.FileNotFoundException: <location> (Access is denied)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(Unknown Source)

I have the same error for the .PDF and .DOC files. I have no error with .TXT, .JSON ......

I have the administator rights.

I think it's Microsoft Office which protect .DOC and .CSV files.

demongolem
  • 9,474
  • 36
  • 90
  • 105
Jerem Lvz
  • 1
  • 1
  • 1
    "Access is denied" suggest you have permission issue. I would check which credential the Java application runas. Try some basic file reading from the temp directory just to eliminate code error or security issue. – Minh Kieu May 16 '17 at 16:53
  • Please post your code and the conrete file location. XY Problem?!.... – Ben May 16 '17 at 17:08

3 Answers3

1

When you call new File your are supposed to give the file name, not only the directory you want to put your file in.

Try with something like

File file = new File("D:/Data/" + item.getFileName());

Cork Kochi
  • 1,783
  • 6
  • 29
  • 45
  • I would ask more question before post as "Answer" as others will mark you down :) – Minh Kieu May 16 '17 at 16:55
  • One possibility of error I just pointed out , if that helps him fine – Cork Kochi May 16 '17 at 16:55
  • File file = new File("D:/Data/" + item.getFileName()); This line works if the file is a .txt file or .json file. But, if I try with a .csv file, it doesn't work. If I remove the extension, for example "result" instead of "result.csv", it works. The problem is the extension .csv and I don't know how fix this problem. – Jerem Lvz May 16 '17 at 17:11
0

Try printing the fileObj.getAbsolutePath(); to see if the file object is referring to the correct location/file.

If it is correct, then the problem is with access.

Remember, you may see the Access Denied error even if the file already exists and is being used by another program.

Jerin Joseph
  • 1,087
  • 9
  • 17
-1

You should consider posting some code so that we can review the code blocks to identify a solution.