Write to ~/Library/ApplicationSupport/* with java? - java

I'm writing an Application for Mac in Java that needs to store a few preference files. By default, they seem to be storing to the User folder, but I'd like to store the files in the Library/ApplicationSupport folder, but I can't seem to figure out how to do that.
I've tried File file = new File("/Library/ApplicationSupport/AppName"); then file.mkdir() but it keeps returning false. I've tried adding ~ to the front of /Library/, and that didn't work either.
I've also tried just writing the file to the desired directory on a FileOutputStream, but no luck there either. I'm open to other ways of storing my preference files, I just don't want them stored in an obtrusive way to the user.
Thanks!

You can't write to /Library/ApplicationSupport because your user would not have the permission.
The plain Java io classes don't understand "~", so to write to ~/Library/ApplicationSupport, you need:
System.getProperty("user.home") + "/Library/ApplicationSupport"

Related

Creating or reading files on any computer

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.

Is there a way to write file atomically in Java on Linux?

Linux Api has O_TMPFILE flag to be specified with open system call creating unnamed temporary file which cannot be opened by any path. So we can use this to write data to the file "atmoically" and the linkat the given file to the real path. According to the open man page it can be implemented as simple as
char path[1000];
int fd = open("/tmp", O_TMPFILE | O_WRONLY, S_IWUSR);
write(fd, "123456", sizeof("123456"));
sprintf(path, "/proc/self/fd/%d", fd);
linkat(AT_FDCWD, path, AT_FDCWD, "/tmp/1111111", AT_SYMLINK_FOLLOW);
Is there a Java alternative (probably non crossplatform) to do atomic write to a file without writing Linux-specific JNI function? Files.createTempFile does completely different thing.
By atomic write I mean that either it cannot be opened and be read from or it contains all the data required to be writted.
I don't believe Java has an API for this, and it seems to depend on both the OS and filesystem having support, so JNI might be the only way, and even then only on Linux.
I did a quick search for what Cygwin does, seems to be a bit of a hack just to make software work, creating a file with a random name then excluding it only from their own directory listing.
I believe the closest you can get in plain Java is to create a file in some other location (kinda like a /proc/self/fd/... equivalent), and then when you are done writing it, either move it or symbolic link it from the final location. To move the file, you want it on the same filesystem partition so the file contents don't actually need to be copied. Programs watching for the file in say /tmp/ wouldn't see it until the move or sym link creation.
You could possibly play around with user accounts and filesystem permissions to ensure that no other (non SYSTEM/root) program can see the file initially even if they tried to look wherever you hid it.

Where and how to store text files for reading/writing

I have an app that accesses words from a csv text files. Since they usually do not change I have them placed inside a .jar file and read them using .getResourceAsStream call. I really like this approach since I do not have to place a bunch of files onto a user's computer - I just have one .jar file.
The problem is that I wanted to allow "admin" to add or delete the words within the application and then send the new version of the app to other users. This would happen very rarely (99.9% only read operations and 0.1% write). However, I found out that it is not possible to write to text files inside the .jar file. Is there any solution that would be appropriate for what I want and if so please explain it in detail as I'm still new to Java.
It is not possible because You can't change any content of a jar which is currently used by a JVM.
Better Choose alternate solution like keeping your jar file and text file within the same folder

Creating a file just to be uploaded into the online form

couldn't find relevant question on SO I'm asking a new one. I can create a file using FileWriter class, but it requires to specify the path for that file (physically creates the file). What I want to achieve is to create a file like in-memory, without specyfying the path or saving it on the disk and then upload it into the online form with selenium webdriver, is that somehow possible?
But there also is another problem, html fileUpload element will accept the path such as:
driver.findElement(By.id("Content_CV")).sendKeys("C:\\Users\\name\\Documents\\my_cv.pdf");
but will it accept the file itself? Probably not, so assuming that some of you knows how to create a file without saving it on the disk, would there be a way of providing path to that file anyway (given its virtual location)?
I'm trying to figure it out and I did some google research, yet here I am. Thanks for any attempt of help :)
If your question is just how to create a temporary file without caring about how to name it and where to create it without overwriting existing files, then you can simply use File.createTempFile. This will create an actual new temporary file on disk in the directory designated by the operating system for that purpose. You might also want to have a look at File.deleteOnExit.
If you are not looking for a java solution, you can simply create the file in a RAM drive
Software for this exist for all mayor operating systems.
Have a look at the wikipedia link above for more information.

Where to put text files in directory in Android

I have a text file i want to include in my Android application, it is not a string file it is a standard text file. It contains data that defines the characteristics of a "map" that is drawn on a board. The file is not an XML file so i am unsure where i should put it or if this isn't good file structure for android? Are you suppose to do this? If you are then under what directory are you suppose to put them? How then are you suppose to access the file? I know how to use FileInputStreams and FileOutputStreams i just need to know how to access the file. All relevant answers are welcome and appreciated!
Use assets or raw folder in your android folders structure to keep that file. For more info read this
You have to put your file in the assets folder as Waqas said.
Now to access it you do it like that.
I give you an example using BufferedReader
BufferedReader reader = new BufferedReader(
new InputStreamReader(getAssets().open("YourTextFile.txt")));
Be careful. In my case, I don't know why for now, I cannot read text files bigger than ~1MB and I had to split them in multiple small files. It seems other had the same problem of file size but I didn't find any information about that on Android developer site. If any one knows more about this ....
FOLLOW UP
My problem with the 1MB was due to a know bug/limitation of earlier versions of Android. Since using recent versions of Android, that problem is not present anymore.
I would just like to add to the accepted answer (I don't have enough reputation to comment unfortunately.) The link there to the tutorial that explains how to set up the res/raw method or the assets method is mostly good, but there's actually a MUCH easier way. Look at the function described there called LoadFile. That function is rather verbose. Lets say all you need is an InputStream variable so that you can read and write to a file. Then delete everything after line 77! Also you don't need the resource id at all! You can use this function:
//get the file as a stream
iS = resources.openRawResource(R.raw.mygpslog)
Now all you have to do is return iS and you will have your much desired file handle.
For reference, the tutorial is right here -> http://www.41post.com/3985/programming/android-loading-files-from-the-assets-and-raw-folders

Categories

Resources