I can't find anywhere that explains this, but I might have just missed it. Anyway, if you use java.io.File.delete () to delete a file, what happens to the file? I only need an answer for how this works on Windows. Does the file get sent to the Recycling Bin, a separate designated location for files that were deleted by Java, or is it completely lost? Again, sorry if this is a duplicate. I couldn't find an answer anywhere. Thanks
Using the Recycle Bin is a bit more complicated to do than using File.delete(). To make Java work on every platform, File.delete() simply deletes the file for good.
Is it possible with Java to delete to the Recycle Bin?
If you want to not actually delete the files completely, why not move "deleted" files to your app's own designated folder and clear the folder periodically?
Related
my program on startup will:
1. search for a file
2. read the file
3. and set a string to the files contents
But the way ive done it it will only work if they have the exact path that i am hard coding in.
i want the path to adapt to other computers. I think i should use the Path class but ive just heard about that so not sure where to go.
basically i want it to search for a file on any users desktop, and if its not there make it.
if you need some code to clarify i can post it just let me know
I could think of two options.
You can simply specify a file name such as "myFile.txt", so the program will search this file in its program/project folder.
If it does not exist you can write the code to create it in the program folder, instead of hard coding any absolute path.
Else, you can try using the javax.swing.JFileChooser class to pop up an Open and Save dialog box.
This will give the end-user the freedom to select any file for reading and writing.
I found below two articles with some example on how to use the class. Please refer them for more information.
https://www.codejava.net/java-se/swing/show-save-file-dialog-using-jfilechooser
How to "Open" and "Save" using java
Thanks.
You can use the path "./yourfile.txt". It will search for "yourfile.txt" in the directory ".". That means the project's current directory. Maybe it can help you.
I have some questions regarding external folder use within Java,
I would like it so that when you load the .jar file, it creates external folders, with the names "resources", and I was wondering how I would go about doing that?
Another question is that I would like to know how to download files from my FTP server (ftp.connorwright.uk) into the "resources" folder, which will lead me onto my next question
My last question is how can I reference these external folders within my code, without any IOException errors or anything
I have tried using the CommonsIO library, but that didn't seem to help.
Thank you.
Your first question is easy
File folder = new File("path\to\your\folder\resources");
folder.mkdir();
You can replace "mkdir" with "mkdirs" to create the entire path of folders in case it doesn't exist
Your second question is a bit complicated, but this website has a great tutorial on it
http://www.codejava.net/java-se/networking/ftp/java-ftp-file-download-tutorial-and-example
And lastly, the only way you can avoid IOExceptions is if the files you are looking for actually exists, there's no universal trick.
Hope this helps
I change files like /etherpad/trunk/infrastructure/ace/www/ace2_inner.js and then run /etherpad/trunk/etherpad/rebuildjar.sh and yet when I run it again everything looks the same. Is there a trick I'm missing?
I've also tried running rebuildjar.sh with clearcache to no avail.
Look at bin/build.sh and track down which scripts are called directly and which scripts are called via subscripts. The whole thing is a mess :/
ace2_inner is perhaps one of the autogenerated .js files? What you need to do is read the first 10-20 lines of every source code file before you edit it, to make sure you're not editing an autogenerated file.
If you don't know the structure, then use bin/build.sh every time until you learn how to handle the source code. (This advice isn't actually etherpad-specific :)
And then make sure you've STOPPED all etherpad servers before starting up a new one.
Always clear the browser cache properly after changing the etherpad files. Otherwise none of your changes will show up.
I have a strange problem. When I try to delete a file created by my application it gets deleted and gets replaced with a junk file of the exact same filesize. Can someone please help me out with this? Beats me. The same thing happens when I try to delete the file manually.
are you perhaps using an NFS file system on linux? NFS will leave tombstones behind deleted files in some cases.
(Unless you specify your operating system and post some of your code, this is pure guesswork.)
Since deleting the same file manually causes the same behaviour, it's reasonable to assume that this is not an issue with your code specifically.
Some filesystems (FUSE on Linux comes to mind, as well as some network filesystems) present this behaviour when deleting files that are in use by another process.
I have a java app, that needs to save and load user settings. I want to save them in a file located in the JAR file, how could I achieve this?
That's not possible. Rather consider using java.util.prefs.Preferences which is designed for exactly this purpose. See also this little guide.
This is not a sensible course of action.
A JAR file is basically just a ZIP file. To rewrite its contents you need to extract them in full, make changes as needed and then write them to a new file that replaces the old one.
If the program that is going to do this is the same one as that contained in the JAR file, this becomes impossible as the file is write protected during execution.
You'd be better advised to store your configuration elsewhere.
That is not the way to store preferences as others said.
If you has to do it that way then :
Locate the JAR from code: How to get the path of a running JAR file?
Unjar the contents to temp folder
Modify in the temp folder
Jar temp folder to the new JAR file.
To add to what Kris said, most security experts will tell you that it's generally a bad security practice to allow end-user applications to modify their own code. What you're asking for would require that.