Merge multiple PDF (from same folder) document with Java PdfBox [duplicate] - java

I am trying to read the files inside a folder, but when I run the program it throws this exception. I tried with some other folders also. It throws the same exception.
Exception in thread "main" java.io.FileNotFoundException: C:\backup (Access is denied)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)

You cannot open and read a directory, use the isFile() and isDirectory() methods to distinguish between files and folders. You can get the contents of folders using the list() and listFiles() methods (for filenames and Files respectively) you can also specify a filter that selects a subset of files listed.

check the rsp's reply
check that you have permissions to read the file
check whether the file is not locked by other application. It is relevant mostly if you are on windows. for example I think that you can get the exception if you are trying to read the file while it is opened in notepad

Also, in some cases is important to check the target folder permissions. To give write permission for the user might be the solution. That worked for me.

Here's a gotcha that I just discovered - perhaps it might help someone else. If using windows the classes folder must not have encryption enabled! Tomcat doesn't seem to like that. Right click on the classes folder, select "Properties" and then click the "Advanced..." button. Make sure the "Encrypt contents to secure data" checkbox is cleared. Restart Tomcat.
It worked for me so here's hoping it helps someone else, too.

Check the file path properly, usually we mention the location and forget to specify the file name or the exact position where it belongs to.

This solution worked:
My JDK is installed in one directory(C drive) and
my java program as well as other files are in another directory(D drive).
Here's the solution from another Stack Overflow question

Related

How to open Files from a Path that is not inside the Project in Java? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I have a problem or a question regarding Java and or Eclipse. I have a programm that does the following:
open up a UI where the user can search for a folder
if the folder is picked the absolut path is getting passed to the main
there are few folders in that chosen path
there are functions that read from the data and create a new excel with the read data
The path that I am getting is correct!
Gernerally the programm works... But it only works if:
The chosen Path from the User is a folder that is inside the project folder where I run the programm from.
OR
The Names of the files that are getting read are specified inside the project folder. What I mean with that is: if the file test.vcc is inside the package then I could open it from the desktop if there is a file that is also named test.vcc
So it seems like Java or Eclipse cant open up paths that are not inside the project or named in the project - is that true?
Running the programm gives me NullPointerExceptions for every File that is not especially named or inside the project.
Sincerly Faded
Update
Okay so I have noticed something that I am doing wrong definetly!
I was replacing the whole FILEPATH that the User choses with ".\" which obviouslly means that it will not care about the filepath and replace it with .\ instead of really taking the path.
String filePathS = filePath.replace(FILEPATH, ".\\");
When I am using this it works because it can find the files in the project Folder. So after seeing this I just took the real Path that the user choosed.
Its basically this:
String filePathSX = filePath.replace("/", "\\");
or this
String filePathSX = filePath.replace("\\", "/");
which either gives me a Path like this: C:\Users\me\dev\foldername\part\test.txt or like this C:/Users/me/dev/foldername/part/test.txt
I can check that in the destination the file test.txt does indeed exist and the path is correct. Still when it tries to do something with that file it gives me a NullPointerExeption.
IF I would leave the replace function for the whole path with .\, then it would take the test.txt file that is inside the package and it would run without any problems.
So I think that I am somehow accsessing the file in the wrong way. How should the path look like on windows if the user chooses something? c/user/... or c\user...
Because both ways do not work for me.
So it seems like Java or Eclipse cant open up paths that are not inside the project or named in the project - is that true?
Yes and no. But mostly no, that is not true.
Some OSes (most notably, Mac OS) have a security policy framework in place that denies any file access to all applications unless you explicitly allow it. If you're on a mac, that sounds like it could be the culprit.
Other than that, though - the answer is no: That is incorrect.
You'd have to ask a new question and provide a bunch of details if it's not the Mac OS thing: Show the code, show the result of printing the path, and show the actual file structure on disk, for example.
NullPointerException sounds even weirder, so maybe your code is just problematic.
This is not true...
File f = new File("C:\\path\\to\\your\\file");
// perform some checks here to make sure Desktop can actually open it
Desktop desktop = Desktop.getDesktop();
desktop.open(f);
EDIT: if you are getting NPEs, your path contains no file, try creating a file with a certain name at that location, and then opening it in your file explorer to see where it actually went

Saving a file in Windows

I have a folder called WalnutiQ. Inside this folder is is a file at WalnutiQ/train/model/MARK_II/Save.java
Save.java
JsonFileInputOutput.saveObjectToTextFile(myObjectJson,
"Digits.txt");
which works! However, the file Digits.txt is unfortunately saved in WalnutiQ/Digits.txt
How do I save the file Digits.txt at WalnutiQ/train/model/MARK_II/Digits.txt???
I am programming in java in eclipse in windows. I have tried
JsonFileInputOutput.saveObjectToTextFile(myObjectJson,
"/train/model/MARK_II/Digits.txt");
JsonFileInputOutput.saveObjectToTextFile(myObjectJson,
"\\train\\model\\MARK_II\\Digits.txt");
but neither work.
judging from the result you are getting your current directory is pointed at WalnutIQ. You might try using .\train\model\MARK_II\Digits.txt. Windows treats the . (period) as a token for "current directory". Your other attempt would have tried to find the train directory in the root of C because the \ (backslash) is a token for the root (c:). It likely fails because that folder does not exist - unless it created it... might go look :) I don't use Json in eclipse which is why I'm not answer your question with code.
Have you considered using the full absolute file path to the text file, rather than the relative one you are currently using? This may not be practical, depending on how you plan on using your program, but it might be worth a shot.
Depending on how the library saves files (I imagine it's using File behind the scenes), you should be able to supply an absolute path to the location you want it to save to.
JsonFileInputOutput.saveObjectToTextFile(myObjectJson,
"/path/to/WalnutiQ/train/model/MARK_II/Digits.txt");

Java FileInputStream can't find file (LibGDX)

So I have this class:
http://pastebin.com/EwXFwuZz
And this directory tree:
http://s14.directupload.net/file/d/3099/uskko5mo_png.htm
And I'm working with the LibGDX Framework on this project. This is basically my problem:
I have a file that contains level information in "chunks". Each line is one chunk. I want to read the file line per line. Unfortunately the built in FileHandling system of LibGDX doesn't support line by line reading so I thought to stick to the stock java one.
However I'm getting this "FileNotFound" Exception:
java.io.FileNotFoundException: ./assets/data/lvls/example.txt (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:120)
at java.io.FileReader.<init>(FileReader.java:55)
at gemini.cute.game.xvii.database.LevelReader.<init>(LevelReader.java:49)
at gemini.cute.game.xvii.core.MainLauncher.create(MainLauncher.java:40)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:124)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:107)
With LibGDX the file is found but read into ONE single gigantic string. And for some reason with the same path (even going from the root) the file isn't found.
For people not familiar with LibGDX I'm coding in the upper "CuteGameXVII" project but for compilation I run the "Main" in "CuteGameXVII-desktop". The assets folders are linked via eclipse and worked for other resources so far.
Am I missing something super obvious here? If so, please help me :P Thank you in advance.
If you're running the Java program from a directory with path $DIR, the input file should be at $DIR/assets/data/lvls/example.txt. Based on the exception that you've received, the input file doesn't exist at this location.
I'd suggest that you first try using the absolute path to the input file in your code. Then, figure out what the relative path to it is.
I experienced this issue too. In order to read a file from your asset directory with LibDGX you must use the LibGDX method replacing:
new FileInputStream("SomeFile.txt")
by
Gdx.files.internal("SomeFile.txt").read()
assuming "someFile.txt" is in your asset root folder.

Where does Netbeans read files from?

I'm trying to read from a text file in Netbeans. In the top level of my project directory I have foo.txt. Then in my code I have:
File file = new File("foo.txt");
It throws a FileNotFoundException, however. It's a Java web application using Spring and Tomcat, but I'm not sure if those details matter since I'm running the whole thing inside Netbeans. Basically, I just want to know where I need to put the file so Netbeans will read it.
Update - good call guys, it's looking in Tomcat's bin directory. Now this may be a stupid question but, how would I go about getting it to look in my top level project directory? I feel like dropping text files into tomcat's bin would be innapropriate.
You can try printing the absolute path of the File object to see where it is looking on the filesystem.
System.out.println(file.getAbsolutePath());
I would use the following to figure out where to put the file:
System.out.println(System.getProperty("user.dir"));
To directly answer your question, If you're running an application on Tomcat, files will be opened from the current working directory. That will likely be the bin/ folder in your tomcat directory.
You can find out for sure where your program is looking by examining the result of file.getAbsolutePath().
However, for web applications, I would suggest putting files you need to read in your classpath so you don't have to depend on a certain file structure when you deploy your web application.
try System.getProperty("user.dir") to get current working directory

java.io.FileNotFoundException: (Access is denied)

I am trying to read the files inside a folder, but when I run the program it throws this exception. I tried with some other folders also. It throws the same exception.
Exception in thread "main" java.io.FileNotFoundException: C:\backup (Access is denied)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
You cannot open and read a directory, use the isFile() and isDirectory() methods to distinguish between files and folders. You can get the contents of folders using the list() and listFiles() methods (for filenames and Files respectively) you can also specify a filter that selects a subset of files listed.
check the rsp's reply
check that you have permissions to read the file
check whether the file is not locked by other application. It is relevant mostly if you are on windows. for example I think that you can get the exception if you are trying to read the file while it is opened in notepad
Also, in some cases is important to check the target folder permissions. To give write permission for the user might be the solution. That worked for me.
Here's a gotcha that I just discovered - perhaps it might help someone else. If using windows the classes folder must not have encryption enabled! Tomcat doesn't seem to like that. Right click on the classes folder, select "Properties" and then click the "Advanced..." button. Make sure the "Encrypt contents to secure data" checkbox is cleared. Restart Tomcat.
It worked for me so here's hoping it helps someone else, too.
Check the file path properly, usually we mention the location and forget to specify the file name or the exact position where it belongs to.
This solution worked:
My JDK is installed in one directory(C drive) and
my java program as well as other files are in another directory(D drive).
Here's the solution from another Stack Overflow question

Categories