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.
Related
Possibly a duplicate, though I doubt so since I have not seen anything so far completely answering my criteria in a way that I can complete my program
Background
What I need is to access another jar, from a seperate jar, read and write files to that jar. So far what I have done is change the jar to a zip and then I can delete files, but the problem I am having is with writing files back in, specifically image files (.txt works perfectly fine)
Question
How do I write image files to a zip (that was originally a jar) from another java program (in the end product another jar)
Note
I have looked around and most sources say this is not possible, but those questions dealt with this during the running of a program, my special case is that the other program is not running, but in file format. All I want to do is write and image in and convert it back to a jar and not have any problems with running that jar in the end.
Thank you!
Use FileSystems to access, write and replace the contents of the jar file:
try (FileSystem fs = FileSystems.newFileSystem(Paths.get("path/file.jar"), null)) {
Files.copy(Paths.get("path/to/image"), // path to an external image
fs.getPath("image.jpg"), // path inside a jar file
StandardCopyOption.REPLACE_EXISTING);
}
I am a newbie. A wanna to check for existing a folder or file in directory in past.
For better. Example, i may a directory C:\Users\Admin\AppData\ and i wanna to check of existing a directory Test in that path. That maybe be checket by:
File file = new File(System.getenv("APPDATA") + "\\Test\\");
if(file.isDirectory()){
///...
} else { ////....}
But i wanna to check if that directory is deleted - when. Please help with code examples... be VERY and VERY thanks
Instead of the File class, I recommend looking at the Files class - it is there to help you do many things. For example, Files.createFile(...) will check to see if a file exists before creating. You can then pass a positive result to FileWriter(...) for your work.
You can check for the presence of a file of folder, but not that it was deleted (e.g. checking a log file of past actions). I recommend using the logic of "if not there then it never existed or was deleted". Another option when working with files is to use parameters to always overwrite the file if that is what you want.
You are asking a question about the operating system. What happens after a file or folder is deleted is unique to each operating system. A notional recycle bin's awareness of a file or folder's original location was, and where that content may have been moved to is specific to an operating system (and usually isn't just moved into another folder).
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.
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.
I'm just getting into using "Java Resource Files" and i have a few questions...
I am hoping to distribute my program to others and I'm assuming JAR file isn't the best way. I'd probably go about it by "converting to exe" is this good practice? what are the limitations?
If I convert to an exe does it keep the resource files?
I'm actually just trying to use a resource file. This file is a text file and will just save the users directories for certain files so they don't need set them up every time they open the program. is this even the best way to go about it?
how do you reference the resource file in the code itself?
Here is what I've done.
created a new resource file and since I'm using Netbeans I can see its location under the files tab in the navigator it looks like this:
Mainproject
build
classes
myclass
resources
directories.txt
here is how i'm trying to access it but when i debug it is coming back null.
private void getPaths()//todo
{
try
{
InputStream is = getClass().getResourceAsStream("/resources/directories.txt");
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null)
{
System.out.println(line);
}
br.close();
isr.close();
is.close();
}
catch(Exception e)
{
}
}
"Converting to EXE" is just a fancy way of saying "wrapping the Jar files into an executable container"
"I'm assuming JAR file isn't the best way" not really. It's nice to provide a OS specific means for launching the program at times, but it's not always the best solution.
"what are the limitations?". Well, to start with, you're limiting your self to a single platform. For Mac's you need to bundle the application into a "app" bundle. For linux, I think most people provide scripts to launch their code.
You could also be limiting your self to particular bit depth. If all you supply is a x32 bit executable, then you'll only ever run within a x32 bit environment. This may not be an issue, but you're limiting the available memory to start with...
So yes, generally, your resource files will be safe.
A resource file is generally embedded. What you're describing in part 3 is more akin to a configuration file. This file needs to be stored on the file system (out side of your exe/jar) so it can easily be updated.
"how do you reference the resource file in the code itself?"
For embedded resources you will need to start by using getClass().getResource(...). For you configuration file, I'd say just like any other file...
I would also have a look at Deployment some ideas on the suggest mechanisms for deploying Java programs,
Jar is a perfect format for distribution. You can convert to exe , but the user will still need the JVM installed to run it. Jars are executed with a doubleclick if the JVM is installed AND the jar has a properly formed manifest file.
You can open any file from the JVM, text, binary, XML, property file etc.
To save user settings a good choice is a property file - see http://www.mkyong.com/java/java-properties-file-examples/