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.
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());
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.
You should consider posting some code so that we can review the code blocks to identify a solution.
Related
I have a big issue on a Java tool I work with.
Every time I try to upload an xlsx file that is marked as "Restricted with protection", I got the same exception
**org.apache.poi.openxml4j.exceptions.InvalidOperationException: Can't open the
specified file: 'C:/bea/test.xslx'**
**at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:106)**
**at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:221)**
**Caused by: java.util.zip.ZipException: error in opening zip file**
**at java.util.zip.ZipFile.open(Native Method)
at java.util.zip.ZipFile.<init>(Unknown Source)
at java.util.zip.ZipFile.<init>(Unknown Source)
at java.util.zip.ZipFile.<init>(Unknown Source)
at org.apache.poi.openxml4j.opc.internal.ZipHelper.openZipFile(ZipHelper.java:174)
at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:104)
... 6 more**
There is no password on that excel file, it's just an upgrade of MS office that apply this kind of restriction for every file that is downloaded from the Internet. If I change it into "Unlimited access" everything is fine.
The java tool first create a copy of the excel file I try to upload in a temporary path. The file is correctly created (as the original one) into excelPath.
The exception is caught at this line
opcPkg = OPCPackage.open(excelPath, PackageAccess.READ);
I tried to change the PackageAccess into READ_WRITE, but nothing has changed.
The tool I'm working with is Java 8 based, it's built with Ant and runs on Jboss EAP 7.2.
--EDIT--
Thanks to #Markus and #Gagravarr I found there is a sort of encryption on that file that allows only selected people to open the document (ie. trying to open it in my personal laptop I'm asked to insert email and password to open it).
In order to repeat the exception it's suffice to open an xlsx, then go to File-->Info-->Protect Workbook -->Restrict Access and change into "Restricted Access" as shown below
Restriction
Is anyone familiar with this kind of issue? Is it possible to bypass this kind of restriction and open the document in java?
Any hint would be really appreciated!
Thanks a lot in advance.
There is a difference between ZipFile and ZipInputStream implementation. Please try:
File file = new File(excelPath);
FileInputStream input = new FileInputStream(file);
opcPkg = OPCPackage.open(input);
Update:
IRM restricted documents / AD RMS of course can not be opened by Apache POI. This is a microsoft specific feature that needs access to licensing server / Active Directory. I never heard of any implementation that integrates this on linux.
I'm using this code File serviceAccountFile = new File(Main.class.getResource("/serviceAccountKey.json").getFile()); to access a json file inside my "resources" folder. It works fine when I'm running it from intellij. But i get this error when running the program from the jar.
java.io.FileNotFoundException: file:\C:\Users\Ashirwada\Documents\IIT\JAVA\POS\target\POS-0.6-jar-with-dependencies.jar!\serviceAccountKey.json (The filename, directory name, or volume label syntax is incorrect)
at java.base/java.io.FileInputStream.open0(Native Method)
at java.base/java.io.FileInputStream.open(Unknown Source)
at java.base/java.io.FileInputStream.<init>(Unknown Source)
at ashirwada.pos.Firebase.initializeFirebase(Firebase.java:27)
at ashirwada.pos.Main.init(Main.java:33)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown Source)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(Unknown Source)
at java.base/java.lang.Thread.run(Unknown Source)
I have other files like fxml files and jpg inside my resource file. They get detected and runs fine. This json file is the only thing thats giving me problems. I opened the Jar with winrar and the json file is there with the rest of the fxml files and the jpg. I'm using maven to compile my jar with the dependencies i need.
That's because after you package your resource into a jar, the string you get by URL.getFile() would be invalid for the constructor File(String path).Use getResourceAsStream instead may solve your problem.
System cannot resolve file C:\Users\Ashirwada\Documents\IIT\JAVA\POS\target\POS-0.6-jar-with-dependencies.jar!\UI\serviceAccountKey.json due to the file is in jar file
You should extract the jar in to a folder and read json file from there
Or you can read file as a resource
InputStream in = etClass().getResourceAsStream("/UI/serviceAccountKey.json");
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
try this
File(Main.class.getClassLoader().getResource("/serviceAccountKey.json").getFile());
while starting jboss I am getting below error:
java.io.FileNotFoundException: file:\C:\workspace4.3.2\workspacePLE.metadata.plugins\org.jboss.ide.eclipse.as.core\JBoss_AS_4.21507285539456\deploy\LocalDeploy.ear\ple.war\WEB-INF\lib\ple-data.jar!\data\config\HTMLEntities.res (The filename, directory name, or volume label syntax is incorrect)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:120)
at java.io.FileInputStream.<init>(FileInputStream.java:79)
at com.inetpsa.ple.utilitaires.PleCharEntities.initialize(PleCharEntities.java:37)
at com.inetpsa.ple.initialisation.OUInit.initHtmlEntities(OUInit.java:340)
at com.inetpsa.ple.initialisation.OUInit.interneInit(OUInit.java:367)
at com.inetpsa.ple.initialisation.OUInit.reinit(OUInit.java:424)
at com.inetpsa.ple.initialisation.OUInit.init(OUInit.java:412)
But the file is present at this location (however it is inside a jar).
The code for getting file path is:
String htmlEntFileName = OUInit.class.getClassLoader().getResource("data/config/HTMLEntities.res").getPath();
Please let me know if I am missing anything.
You are using FileInputStreamso I assume you ase the path to create new FileInputStream, but once the file is inside a jar it can't be accessed as a file anymore.
The file can be accessed as an InputStream using either Class.getResourceAsStream() or Class.getResource().openStream().
java.io.IOException: open failed: ENOENT (No such file or directory)
This happens on this line of code:
File db = getDatabasePath("questions");
db.createNewFile();//HERE
I am trying to create a database file with the name questions.db , which will be empty. I will then pull the file from the server and insert it there. I then want to be able to access the database from sqlite.
You did not show the stack trace, it can help to pinpoint it. But since the File#createNewFile() is not doing much, the only possible reason for this to fail is, that the (relative) path includes a non-existing parent directory. createNewFile() will not create those.
You typically write
File db = ...
db.getParentFile().mkdirs();
db.createNewFile();
to fix this. (Or use a different - aka existing - location).
I am probably doing something stupid here but my app cannot read data from the external storage. But i can write files just fine. In fact, i pull the files from an FTP server and write them to external storage which works fine. But as soon as I try to read them with:
getApplicationContext().openFileInput(files[i]);
I get java.io.FileNotFoundException: /data/data/co.za.infestation.opendesign.app/files/event1.txt: open failed: ENOENT (No such file or directory)
Which is weird because I know the files are there. And i know files[i] has the correct file name because I printed them before after:
String[] files = getExternalFilesDir(null).list();
Yes I did add the permissions in the manifest file... And yes, i did hardcode the file names without success..Maybe i need more coffee.
Thanks!
openFileInput()
is for opening files which reside in private internal memory. You can see the error path:
/data/data/co.za.infestation.............
Try to open with a
FileInputStream()
instead.