Where to put text files in directory in Android - java

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

Related

Write to ~/Library/ApplicationSupport/* with 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"

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.

Saves game level settings

I have been coding for about a month and I have found ways to adapt around ever problem but one. The problem as you can probably see by the title is how to make a way to make game saves. I am currently creating a very simple game that has about 5 classes of my code and maybe 2 of Java Swing GUI.
I know how I would like to go about the saving process but I have no idea how to do it in my code. How I would like to go about doing this is by making the code print a Number or Integer to a file to represent a Level. For example if you completed level 1 the number in the file would be 1. I have tried some templates for this but none of them work.
I understand how to write to a file but my problem is reading it from a jar or even creating a file then reading it from a place on the computer. I need to know how to find a file URL for different computers because some use Docs and Settings and other Users. Please could someone help.
Since the jar is read only, it can only contain the 'default settings'. See this answer for the general strategy to deal with such a embedded-resource.
Speaking of which (embedded resources) see the info. page for more details on how to access them.
Here is an example of storing and reading a Properties file from the 'current directory'.
As mentioned by #MadProgrammer though, it is safest to put the settings file into a (sub-directory) of user.home, as seen in this answer.
But a properties file is just one option. You might also serialize an object, or write the file in a custom format that your app. knows how to read, for the first two off the top of my head.
Besides 'serialize (in some form) in a File', there is also the Preferences API, or for desktop applications launched using Java Web Start, the PersistenceService. Here is a demo. of the service.
I need to know how to find a file url for different computers because
some use Docs and Settings and other Users
The System property user.home points to the user's home directory
File userHome = new File(System.getProperty("user.home"));

Holding Information in Android App

I've only just started to write in Java on Android, so please bear with me.
I have some settings I want to hold in my app, normally I would have used an xml file. Trouble is i'm not sure how to load it into the xml parser to read it.
I thought I might be able to drop it into /res/values/Info.xml and open it from there but it does'nt find the file.
I have also read that people are starting to use a SQLite database to hold information in, is this more the standard way to go?
thanks a lot
Luke
It sounds like what you want are Shared Preferences. Its a simple way of storing key value pairs, along with a UI for letting the user change them.
You could try storing to External Files (that way they can save settings on an SD card if they have 2.2). You wouldn't be able to modify the files in the res folder like you tried because these files get compiled into the app package. You could also try Internal Files found on the same page. SQLite might be a bit much for config settings.

Categories

Resources