For my application to work, I need to delete a file which is a Calendar Cache file on Mac. If I do it manually it goes to trash with no problems, it is just not emptying the trash afterwards, but it is fine for me. I tried to run my Java application with a method to delete that same file but the method failed. I used file.delete() method. Are there any ways of deleting that file? Is there a way to stop the process that is using that file? Thank you for your help.
Try something like this:
File fin = new File("yourfile.txt");
for (File file : fin.listFiles()) {
FileDeleteStrategy.FORCE.delete(file);
}
Related
I am completely new to Java and I am
using somebody elses code to read a binary file
but the file will not open. I am running the code in Eclipse under Windows 10
the file is called
bookDeepDist.dat
I have put it in the project folder. The full path name which I have also tried (without success ) is
C:\Users\Alan\eclipse-workspace\readDatabase\bookDeepDist.dat
The code that fails is:
public void openBook() throws IOException {
file = getClass().getResourceAsStream(BOOKPATH[bookNr]);
if (file == null)
throw (new IOException("Could not open File "+BOOKPATH[bookNr]));
}
The error message is:
Could not open File bookDeepDist.dat
so it seems to be trying to open the correct file.
Can anybody give me any idea what could be going wrong?
The problem is, that getResourceAsStream() looks in the classpath of the running Java program. That's why it is working, when you put the file in the project folder. What you want, is a stream of the file outside of the program's classpath.
Instead of
file = getClass().getResourceAsStream(BOOKPATH[bookNr]);
try using
file = new FileInputStream(BOOKPATH[bookNr]);
I am creating a Javafx application in Intelij and FileInputStream works perfectly. However, when I create a .jar file from the project and try to run it the code fails to run as it is unable to locate the file in the file input stream.
Here is my code:
ObjectInputStream os = new ObjectInputStream(new FileInputStream("src/settingStorage.bin"));
Am I doing something wrong?
There is always some issue accessing a file outside a jar, depending on where the file location is. You can check this SO question/answer to have an idea on accessing your file.
Read properties file outside JAR file
Try this:
EDIT: Look at this again, I left part out.
ObjectInputStream os = new ObjectInputStream(new FileInputStream(SomeClass.class.getResourceAsStream(“settingStorage.bin")));
This usually works for me. When I run this, it operates fine, in AND out of the development area. Don’t include /src, as when you call getResourceAsStream off a class, it already checks inside the jar.
Cheers!
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.
we are running jar using batch file as window service and place xml files in c:/processed
directory;
if xml has an errorneous format then move it to error folder; we are using below method to move the file to error folder but getting below exception.
I think when the process is running its not able to move the file to error folder .
FileUtils.moveToDirectory("a.xml", "c:/processed/error", false);
exception in movedErrorFolder: Failed to delete original file 'c:/processed/a.xml' after copy to 'c:/processed/error/a.xml'
The java process does not have the right to delete the file, but if you can write on it then you can delete it.
The file is locked by another process. This occurs typically when you write a csv open with Excel.
The file is locked by the java process self, close() the file before moving it.
After reader.close() put Thread.sleep() - It works like charm
reader.close();
// Thread is Explicitly made to Sleep as Threads were shared and files were not getting Moved.
Thread.sleep(10000);
Another exception giving more details is probably nested in the Exception, held by the cause property
1) Make sure you do not have the xml open in an editor/viewer or used by another process.
2) make sure you .close() before moving the file.
Check if you opened a file inside the directory for read or write and you didn't call .close(); before trying to delete the parent directory.
Could you try this method from apache:
void org.apache.commons.io.FileUtils.moveFileToDirectory(File srcFile, File destDir, boolean createDestDir)
e.g.
use this import
import org.apache.commons.io.FileUtils;
and your code would be like this:
FileUtils.moveFileToDirectory(new File("c:/processed/a.xml"), new File("c:/processed/error"), false);
I was struggling with this error since last 24 hours. None of the answers above worked for me. My operating system is Windows 7 64 bit and I am using JDK 6. I tried methods FileUtils.moveToDirectory as well as file.delete.
I suspected it had something to do with Java. I uninstalled and reinstalled JDK 6 (I ran the installer as an Administrator just to be sure) and restarted my machine and Eureka, the error disappeared and things started working.
Now don't ask me why I am still using JDK 6 in 2017 (Some enterprise software ****)
I'm new to Java, just trying to make a simple utility to move, copy & delete some wav files on my pc, but java.io.File delete() fails. The wav files in question have read-only unchecked (in windows explorer) but File canWrite() returns false & setWritable(true) fails. I must be doing something stupid because nobody seems to have had this problem before?
You are using a relative path and you are not in the directory you think you are.
Specify absolute paths or determine the current path before starting.
Run the java application as administrator.
Then try.
file.setWritable(true);
file.delete();
Try to run the garbage collector:
File file = new File("test-file.txt");
System.gc()
boolean success = file.delete();
http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/System.html