Deleting a file, opended by an external application, after closing it - java

I'm creating a pdf file in my java program.
After having it created to a specified Path, I'm opening it with the user's standard application.
File myFile = new File("F:/test.pdf");
Desktop.getDesktop().open(myFile);
My standard application for pdf files is for example Adobe Reader - so Adobe Reader opens up and displays the file. - So far so good.
Is there any way to delete this "test.pdf" after I close the file/my Adobe Reader?

Check the following link:
Java: Check if file is already open
Run an infinite loop after you open the loop, as mentioned in the above thread, verify and close the file accordingly.
Thanks,
JK

You can create the file in the temp directory, so you will not have to worry about removing it.
To create a file in the temp directory you should use the File.createTempFile method.

Related

Java (IntelliJ) does not open file at a set path

I've just installed IntelliJ on OSX and I'm trying to write a project where I'm trying to read a text file (among other things).
In this project there's a very essential feature that I need:
It has to be able to open, read and write a text file at some arbitrary given path on the filesystem. In other words, making any changes to the working directory other than from the main source file is not an option.
I have the following code that produces the following output:
String musicPath = "/Users/test/Desktop/testfolder/";
File file = new File(musicPath + "filelist.txt");
System.out.println(file.canExecute());
System.out.println(file.canRead());
System.out.println(file.canWrite());
System.out.println(file.getAbsolutePath());
The output is:
false
true
true
/Users/test/Desktop/testfolder/
However, when I'm adding the line
FileReader filelist = new FileReader(file);
I'm getting a file not found exception. Needless to say, the file exists.
I've set the permissions such that anybody can read/write that file or folder but I'm still getting the same thing.
Could anybody tell me if there is a way to make the program recognise the file I have on the system? From every place this question is asked I see 3 types of replies: either check if the file exists, check the permissions or change the working directory from the project config.

How to create a .lnk file on the desktop using Java?

How can I create a .lnk file using Java? such as I want to create a .lnk file on my desktop which opens the following directory C:\Windows\System32\calc.exe. I have found this website but it is for creating an URL for the website shortcut. it basically write out 2 lines ([InternetShortcut] and
URL = XXXXXX.com) by using FileWriter and save it as .URL file. but it seems not working with lnk extension.

File gets deleted on Linux but not on Windows

So I have this program in Java, where I make a file, write to it and save it.
But after the program finishes it's job, I want it to delete the file it created.
Here is the code with which I make the file and delete it:
RandomAccessFile file = null;
file = new RandomAccessFile("myFile.zip", "rw");
file.write(buffer,0,read);
file.close();
File file = new File("myFile.zip");
file.delete();
It cannot be related to how Windows and Linux use their file paths ( \ or /) as I don't really specify it other than showing it to be at the root of my project.
So what might be the case in this situation?
Windows notices the open file handle and refuses to delete the open file. That's a policy in Windows. Files which are open do not disappear. The process holding the open file handle can rely on that the file will stay.
Linux has a different policy. There a file can be deleted from all directories (yes, it can be in more than one when it is hard linked), even if a process still has an open handle on it. The file itself will then not be removed from the disk. The process using the open handle can still process the file, make it grow, shrink it, write to it, read from it. But after the handle gets closed, the file gets removed automatically by the file system.
These different policies of the to OSes you are using are the reason for your observation.

File doesn't read after creating jar using netbeans

i have a small application which checks for values from a file and display the result in a jframe.
A file contain list of word to check. this file is placed in project folder "testing" and the main source testing.java file is present in location "testing\src\testing"
input file : c:\document..\netbeans\testing\
java file : c:\document..\netbeans\testing\src\testing\
when i place the input file inside folder "c:\document..\netbeans\testing\src\testing\
" the input file is not taken as input, it works only when kept on folder "c:\document..\netbeans\testing\"
so when a jar file is created it has not included the input file in that, even i manually input that is not taking the input file in and working.
some path setting issue? what can be done to solve this issue?
any help pls??
Once you create the jar, the file becomes an embedded resource. If you try to read it as a File it will no long be the same file system path as you originally use in the program. It must now be read from the class path.
To read the file from the class path, you will want to use getClass().getResourceAsStream(), which return an InputStream. If your file is in the same location (package) as your class file, then you should use
InputStream is = getClass().getResourceAsStream("input.txt");
Then you can read from the InputStream
BufferedReader reader = new BufferedReader (new InputStreamReader(is));
This generally happens, when you don't use absolute path...!
As when you run your program from IDE(Netbeans) then the HOME_FOLDER is your ProjectFolder. Relative to which you would have given the file_path(that has to be accessed in your program).
But after building, jar is present in ProjectFolder/dist. When you run the jar file the HomeFolder is not ProjectFolder rather it is ProjectFolder/dist.
So, to make it successful, to need to copy all files and folders from ProjectFolder/dist to ProjectFolder.
Then run the jar.. Hope it will fix the issue
Try putting double backslashes in your file paths. Like this:
c:\\document..\\netbeans\\testing\\src\\testing\\
This is the format that java normally requires it to be in

Java - read file from directory for jar

I have an application that creates a temporary mp3-file and puts it in a directory like C:\
File tempfile = File.createTempFile("something", ".mp3", new File("C:\\));
I'm able to read it by just using that same tempfile again.
Everything works fine in the Eclipse IDE.
But when I export my project for as a Runnable jar, my files are still being made correctly (I can play them with some normal music player like iTunes) but I can't seem to read them anymore in my application.
I found out that I need to use something like getClass().getResource("/relative/path/in/jar.mp3") for using resource files that are in the jar. But this doesn't seem to work if I want to select a file from a certain location in my file system like C:\something.mp3
Can somebody help me on this one?
It seems you dont have file name of the temp files . When you was running your program in eclipse that instance was creating a processing files, but after you made a runable you are not able to read those file that instance in eclipse created, You runable file can create its own temp file and can process them,
To make temp files globe put there (path + name ) entries in some db or property file
For example of you will create a temp file from the blow code
File tempfile = File.createTempFile("out", ".txt", new File("D:\\"));
FileWriter fstream = new FileWriter(tempfile);//write in file
out = new BufferedWriter(fstream);
the out will not be out.txt file it will be
out6654748541383250156.txt // it mean a randum number will be append with file
and you code in runable jar is no able to find these temp files
getClass().getResource() only reads resources that are on your classpath. The path that is passed to getResource() is, in fact, a path relative to any paths on your current classpath. This sounds a bit confusing, so I'll give an example:
If your classpath includes a directory C:\development\resources, you would be able to load any file under this directory using getResource(). For example, there is a file C:\development\resources\mp3\song.mp3. You could load this file by calling
getClass().getResource("mp3/song.mp3");
Bottom line: if you want to read files using getResource(), you will need those files to be on your classpath.
For loading from both privileged JARs and the file system, I have had to use two different mechanisms:
getClass().getClassLoader().getResource(path), and if that returns null,
new File(path).toURI().toURL();
You could turn this into a ResourceResolver strategy that uses the classpath method and one or more file methods (perhaps using different base paths).

Categories

Resources