How can I check that I can delete a file in Java?
For example, I should be able to delete file C:/file.txt but I never will be able to delete the C:/ or Computer, or My Documents etc.
Solution described in possible duplicate does not work for me.
Removing file requires write permission of the file's parent, i.e. directory where file is stored. Directory in java is also represented by instance of class java.io.File that has method canWrite().
So, to check whether file can be deleted you should call file.getParent().canWrite().
On my Windows 7 64 bit box using NTFS and Java 7 (Oracle JDK), the only thing which worked for me reliably is
boolean canDelete = file.renameTo(file)
This is surprisingly simple and works also for folders, which have "somewhere below" an "open" or "locked" file.
Other things I tried and produced false-positives: aquire a FileLock, File#canWrite, File#setLastModified ("touch"), file.getParent().canWrite()
Related
so I did run into one very weird issue. The idea is simple: create temp dir, place some files in it and then try to access them. Now the problem is that calling File.createTempDir() or Files.createTempDirectory(prefix) creates new file inside AppData/Local/temp with shortened path, so the full path to folder looks something like C:/Users/FirstNam~1/AppData/Local/Temp/myFolder/myFile.txt instead of C:/Users/FirstName LastName/AppData/Local/Temp/myFolder.myFile.txt.
The difference is that generated path inside java contains FirstNam~1 instead of FistName SecondName. Java then throws exception File Not Found.
When I try to copy and paste shortened path into file explorer I get an error saying that file does not exist, but if I do change shortened path to full one then file opens and it works as intended.
Is there any way to fix it? Ether by forcing java to use full path names or enabling something in windows? I did Enable NTFS long paths policy, but it did not help.
This is happening when using java 8/11 and windows 10 running on VM, project is using AGP and gradle. Temp file is created inside groovy file that extends Plugin<Project>
Just when I lose hope and create a ticket, couple hours after that I find the answer. So, java has method Path.toRealPath() which solves this ~1 issue. After using this method paths no longer contain shortening and are correctly resolved.
EDIT: looks like java is doing everything correct and paths are actually valid, problem did come from library that I'm using and it's a bug with it.
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 have a "how-should-I-implement- it- on -the- best- way -in -java" question. I have written a program in Java that reads a csv file that is stored on a fixed directory. (BufferedReader & FileReader) The file is then read out and the data is displayed in an XYDataset (XY graph) using jFreeChart.
Question: The program should now run without a development interface, that means I export it as .exe. However, since the directory is now to be "independent", that means it can be changed at any time, I should now save the directory externally and / or stored it should be without the code must be changed. In addition, the data from the directory (csv-files) should also be called regardless of the code. That means there are different .csv files inside the directory & you should sort by name & most recent date. That It should always open the latest .csv file with the "1" in the name and another time the .csv file with the "2" in the name. My question is now: How should I implement this best?
My first idea would be an XML file to access my Java program, but I do not know exactly how I should store the file path there.
maybe like this: ? file: /// H: /Test/Testfile.csv
Does somebody know how to "outsourced" & then accessed a directory or other ideas, how I could implement that outside the "real" code?
Would be very happy about help!
I have a Java program that is supplied a directory name, gets a list of all the file in that directory using dirName.listFiles() and then iterates through every file parsing information from them.
The files would normally all just be normal text files, but I am using SVN and there seems to be a directory called .svn in my dirName directory which is causing my program to fail because .svn is a directory and not a text file.
Now, I could implement filters using a FileFilter object, but I would really only expect text files to be in that directory in the final program.
My question is: Is there a way round my issue without using a FileFilter? I also think that my program is ignoring the .svn directory in other programs that I've written, so I'm not sure why it's an issue now.
Thanks in advance.
You would have this issue with many version control systems (not just SVN) as some of them have files on disk that help identify where the working copy comes from (.svn for SVN, view.dat for clearcase). You really should just implement a FileFilter to exclude those, or use the ones from commons-io:
makeSvnAware
It's null safe, so if you give it null input, it simply returns an svn filter for you. If you give it another IOFileFilter (a subinterface of FileFilter) it simply returns one that does an AND between the existing filter and the svn filter.
FileFilter svnFilter = FileFilterUtils.makeSvnAware(null);
You could call isDirectory() on each object that listFiles() returns.
Two possible soulutions (at least):
FileFilter or FileNameFilter
isFile()
Look here: http://docs.oracle.com/javase/6/docs/api/java/io/File.html
Better than using java for file and directory search, i would prefer writing a jni program and use C's dirent.h and stat.h to differentiate between files. The jni program would be much faster.
If dirName is not the root directory of your working copy, you can upgrade to the latest version of svn. This doesn't have an .svn directory for every directory but only for the root.
I've never seen that,internally generated?How does it work?
Can check what I meen here:
http://issues.apache.org/jira/secure/attachment/12401970/nutch_0.9_OR.patch
search "java~"
and you can see "java.old" there,what's that again?
It's probably some cruft leftover from emacs. With emacs, whenever you save a file, it saves a backup of the previous version of the file, and the backup is named with the original filename with a tilde appended to it. If this is the case (which you can easily verify by comparing file with file~), then you can safely ignore all of the files named with tildes.
Are you sure its generated from some java process? ~ in files typically means a temporary file created by editors, such as vim when you modify something.