so my program is going to need to crate a file with login information. After that, everytime the program will just run normally, unless the login file gets deleted some how. I want to know of a quick way that I can do this in a multi OS way. I dont want to save the file to a specific part on the disk, because then I have to do it for every OS. What else can I do?
You can use System.getProperty("home.dir") to figure out the user's home directory in a non-OS-specific way
Read more about this on http://docs.oracle.com/javase/tutorial/essential/environment/sysprop.html
Depending on what you want to store, you may not need a file at all. See
http://docs.oracle.com/javase/1.5.0/docs/api/java/util/prefs/Preferences.html#userRoot%28%29
or
http://docs.oracle.com/javase/1.5.0/docs/api/java/util/prefs/Preferences.html#systemRoot%28%29
Related
I have a Java app that uses a .txt file as a database. Every time I run the program it reads the file and copies its contents into an array. After I'm done the data in the array will have changed, and since I don't know any way of deleting lines in a file I update the file by deleting it, creating a new one with the same name and writing back the content of the array to the file.
This works if I always run the program from the same user (my OS is Ubuntu by the way). But if I run the program from user A, it will create a file that only has read/write permission for user A. When I attempt to run the program from user B, it will be able to read the file, but it won't be able to delete it in order to update it because it does not have permission to delete the file.
My question is: is there any way to make my Java program create a file that has read/write permission for everyone? or is there any better way to update the contents of the file without having to delete it? maybe there is a better alternative to using files altogether to store data once the program is closed?
P.D. This is my first time asking in StackOverflow, so please bear with me if I didn't write the question properly or I missed some details you might need. Also I am still pretty noob with programming, so I'm sorry in advance if I asked any stupid questions. Thank you very much for your time and for your answers.
If this is a Posix-conforming system, set the Posix file permissions to whatever you want.
See Files.setPosixFilePermissions.
By the way, you don't have to explicitly delete the file, you can just overwrite it.
I am using Java language
What I want is that Can any one help me to write a code that
When i click on delete option of any file or folder I get notify before delete that I ma deleting a file Whether I want to continue ?
I have seen many examples that notify after the file is deleted.
One thing I want to make clear is as I click on file placed Desktop or My document directory I must get notify that
You are deleting a file .do you want to continue ?
What I really need is I want the exact answer or code
Please help
I shall be very thankful to you
No, this is not possible in Java. The operating system handles the file access, and another process is not capable of preventing the system denying access to those files. The only way you could do this is by having a file system written in Java (say, a loopback mounted WebDAV share) to which you could intercept the file requests with this kind of information. But not only would this be difficult to achieve, it also would only work if all of the access you are doing is via your loopback mounted system; it wouldn't work for files located on the disk or from other network shares.
So, in summary, you cannot do this with any programming language without writing your own filesystem and using that to intercept requests.
I have a Web Start application which needs to save a single text file. I would like to know where I should write the file, such that it goes in a tidy place regardless of the platform it runs on.
If I get my filepath like this...
new File(System.getProperty("user.dir") + System.getProperty("file.separator") + "test.txt);
... then the file gets written somewhere nice and inconspicuous on Linux, but on the desktop on Windows, alongside the icon I'm running the program from, and that's not ideal because the user shouldn't necessarily need to know or care that the file exists.
Is there a good, platform-independent way of getting a file path that allows me to save a file somewhere discreet? I don't much care where it ends up, providing it's not in full view on the desktop.
Thanks.
Neil
user.dir is the current working directory; use user.home instead and java.io.tmpdir for temporary files.
Here's the list of predefined properties for Java 1.5.
What about using a .yourapp directory under user.home instead? You shouldn't face any permission problem, a windows user won't really notice it, it would be hidden on GNU/Linux.
For temporary stuff, you may prefer java.io.tmpdir but keep in mind that the content of this directory (/tmp on GNU/Linux) might be wiped out upon reboot (e.g. with Debian distros). So this is really for temporary stuff.
PS: I prefer to use File.separator over System.getProperty("file.separator").
I strongly suggest using the PersistenceService.
If you don't need the file per user or to be incredibly permanent you can use System.getProperty("java.io.tmpdir"));
As others have stated, "user.home" is a decent choice. If you can get away with it, Preferences are another good choice for storing certain things.
If you do end up using something like "user.home" then make sure you have a fallback position. I've been in many situations where a user's home directory was not writable (locked down windows installs under a domain server for example).
If you store data in the "user.home" directory then put it under a ".myApp" style sub-directory and first verify that you can create that directory. If you cannot create that directory then you could always prompt the user for a place to store it. And then store that location in a system Preference so that it's known the next time the app is run.
I am using Java to develop an application, it needs to manage the file on the computer. The application have the ability/function to delete the file on the system. But I want to check whether the selected file is using/reading by another application or not first. Because I don't want to delete the file which is reading/using. How can I do so?
Maybe you could use tryLock()?
On Windows, you can't delete files which are in use ("locked"). Java itself doesn't offer an API to check.
If another application is using the file or actively reading it, then provided that application has done its job correctly (opening the file with a read lock), you won't be able to delete the file -- you'll get an IOException (specifically, a sharing violation). Catch the exception to know whether there was a problem.
What I need is to instruct OS to open the file with the default program used for that file type. Exactly as if, i.e., that file was double-clicked by user under Windows.
The purpose is, i.e., "your PDF file was generated. Click here to open it".
In platform-independent way, if possible...
I dont know exact terms for what I want, so if someone could update tags, I'd most appreciate that ;)
You need the Desktop class, and the open() method in particular.
Launches the associated application to
open the file. If the specified file
is a directory, the file manager of
the current platform is launched to
open it.
Since Java 6, we have Desktop.open() for exactly that purpose.