I write file to sdcard using BufferedWriter.After that I want to overwrite header of file,but other data must be without changes( In the header I must add size of file). So I think I must change position where I need write.
How can I do that?What function should I use?
You can use a RandomAccessFile to access a file, and modify it.
Related
I need to read some data from file in internal storage, then remove and rewrite file with new data. What is the best way to do it(safest method for data)?
I'd first rename the original file (append something like .orig), then read it, write the new file and when all that's successful, remove the renamed original file. This ensures the most that no data gets lost.
Using Eclipse, Android SDK.
I have a text file full of data that I need pulled in. (For now, it's the easier the way, eventually I'm going to need to scrape dynamic data from a URL, but for now I have the test data I need in this text file).
I've created a class to open this file, but no matter how I try to open it I keep geeting "file not found" exceptions.
I've tried putting my "data.txt" file in various relative paths (within my App):
- "/AppName/"
- "/AppName/src/com/example/appname/data.txt"
I've tried passing different relative paths. I've tried putting the text file in the same path of the .java class file that's trying to open it, and it still can't find it! What am I doing wrong?
What am I doing wrong?
You have two main options of where to store this file within your project directory: assets/ and res/raw/.
If you use assets/, you can call getAssets() on your Activity (or other Context), and on there call open() with the relative path within assets/ to get an InputStream on this file (e.g., assets/data.txt would be accessed via getAssets().open("data.txt")).
If you use res/raw/, you can call getResources() on your Activity (or other Context), and on there call openRawResource(), passing in the R.raw value based upon your filename (e.g., res/raw/data.txt would be accessed via getResources().openRawResource(R.raw.data)).
Use /res/raw directory. I read sound files from raw:
InputStream soundFile1 = context.getResources().openRawResource(soundOneId);
Where context is a base context of activity passed to the class.
http://developer.android.com/reference/android/content/Context.html
I have a file that I open and parse and some of the categories can be modified. Im wondering how I can update the original Xml file with the new values.
I assume that the XML file is a normal sequential file. If so, you could try to update just one tag, but it would be easier and safer just to rewrite the entire file.
How can we modify a text file from android, such as
append at the end of the file.
clear data at any intermediate position and also blank lines in it.
Is there any option to delete a particular line from the text file, I read so many tutorials regarding this all have mentioned to create a temporary file and asked to copy the contents from the original file except that particular line, and to replace the original file with this temporary file.
Is there any alternative to do this with in that original file itself without creating a temporary file?
Thanks in Advance.
You can't do the second option programmatically, that's virtually impossible. The first option however is possible. Just use an InputStream Object to load your file, then you can get the text from the InputStream as a String, I believe, and then you can append the data at the end. Now that I think of it, it might be possible, but with a heck of a lot of work, to edit the text at the middle... But that's up to you to figure out...
Pretty much everything that is possible with java's file api's can be done in android
You can use a FileOutputStream, passing true for append and then using the normal output stream operations.
For changing files in a random-access manner, try RandomAccessFile. This allows you to change the current byte you are pointing to. However, if you delete data in this manner it won't automatically cause the file to contract to fill the empty space; you would need to do this manually and then change the length of the file via setLength().
In Reference to this android file download problem
Can anyone explain what does this line mean in the code
FileOutputStream f = new FileOutputStream(new File(root,"Video.mp4"));
And what does it mean by the parameter root within the File().
Do I need to specify the root path to save the file?
If it is the case then how do we specify the root path in android ?
Regards
And what does it mean by the parameter root within the File(). Do I need to specify the root path to save the file? if it is the case then how do we specify the root path in android?
The code snippet from the question you linked doesn't define the variable, but if the code is downloading a file to the device, I would assume that it's a path on the SD card. Environment.getExternalStorageDirectory() will give you the root path to the SD card. You'll also need to specify the WRITE_EXTERNAL_STORAGE permission in your manifest.
If you're working on the emulator, you can create a virtual SD card when you create the emulator image.
The java.io.File(File, String) or java.io.File(String, String) are standard java constructors for Java. The first argument is just the parent directory path, while the second is the actual file name. If the file is in the current working directory or you know the full path as one string you can avoid the 2 argument constructors.
Since you are trying to download a file you can just acquire the file through a normal URL.openStream() to get an InputStream to get the contents of your downloaded file. For writing the data out you will follow the example you linked to to write the contents.
I'm unsure what the root variable was pointed to in the example. I'm not able to help you beyond this though since I have only gone through the first Hello, Android example myself.