fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(FirstNmstring.getBytes());
fos.close();
I used this code to store data in a file in internal memory of mobile. Just as I make another entry into my application, the existing record gets deleted. I want to add every entry that I enter. could any one help me out on this.
You can open the file for Append
fos = openFileOutput(FILENAME, Context.MODE_APPEND);
Everything you write to it will be appended at the end of the file.
Related
I'm developing an Android application I have to implement a function that create a folder with different files.
I wish the files in the folders were hidden, and this point is not a problem, but I want assign certain permission to files, for example I need that files are not readable / writable by user but only from application.
Also I wish the files in the folder were deleted if the application was uninstalled.
This is the code that i use to create hidden folder:
File JSONStorage = new File(Environment.getExternalStorageDirectory(), ".BMA");
if (!JSONStorage.exists()) {
if (!JSONStorage.mkdirs()) {
Log.wtf("log: ", "Failed to create directory");
}
}
From official Android documentation:
String FILENAME = "hello_file";
String string = "hello world!";
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();
This will create file on device internal memory. This file can be read only by application. (Context.MODE_PRIVATE)
I am a beginner in android also in Java, many times I get confused with various Java implementations in android one of them is:
In the statements
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
what is fos in the above statement? It is obviously object of fileOutputStream but why is it not implemented as
FileOutputStream fos = new FileOutputStream(FILENAME, Context.MODE_PRIVATE);
If possible give me a simple example in java/android.
openFileOutput is a method of the Context that automatically creates a file output stream to your context's data folder.
If you use the constructor of FileOutputStream, you have to pass a full file path as the first argument, while the Context takes only the file name. For files that need not be accessible via file managers, use openFileOutput for saving the file and openFileInput for reading the file again.
One part of my app needs to write a data file out.
I use getFilesDir() and it gives me a path like this (after adding a file name):
/data/data/com.casadelgato.zillaconfigdroid/files/Log.1378357559316.csv
I create a PrintWriter to the file, write a bunch of stuff to it, and close it.
No errors.
The problem is that I can't find the file anywhere on my Android device.
mention the file name in the path
FileOutputStream fos = context.openFileOutput(path,
Context.MODE_PRIVATE);
write the file into the stream...and you get the file by passing the same file name
FileInputStream openFileInput = context.openFileInput(path);
read the input stream
I have a encrypted file, which on decryption is to opened in temporary memory, and when user closes the file they temporary file is deleted, What's the way to achieve this functionality?
You cannot use external storage for the file since it will be available to whole world(what if user ejects SD card before you delete it?)
You should use internal storage so that you can remove it once the user is done. Use MODE_WORLD_READABLE so that user/other apps can only read it. When user is done, you can delete it.
String FILENAME = "hello_file";
String string = "hello world!";
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_WORLD_READABLE);
fos.write(string.getBytes());
fos.close();
File file = new File(FILENAME);
file.delete();
Good practice
Use internal storage and check out this: getCacheDir()
Bad practice
create the file on your sdcard
extStorageDirectory = Environment.getExternalStorageDirectory().toString();
File file = new File(extStorageDirectory, "filename.ext");
try {
outStream = new FileOutputStream(file);
//write data;
outStream.flush();
outStream.close();
}
delete the file
extStorageDirectory = Environment.getExternalStorageDirectory().toString();
File file = new File(extStorageDirectory, "filename.ext");
boolean deleted = file.delete();
As LAS_VEGAS said using external storage is a terrible idea!!
See this link for cache getCacheDir()
http://developer.android.com/guide/topics/data/data-storage.html#InternalCache
or use the normal internal storage with MODE_PRIVATE
ie
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();
then after wards
deleteFile(FILENAME);
EDIT: MODE_PRIVATE would make it not accessible by other applications so maybe not a good idea !:(
But also if it has to decrypt a file and save it unencrypted, if someone really wanted at that file they could get it I'm sure since you're having it world readable for a while
How to read and write strings in text file in android which is kept in raw folder and also how to clear contents of that file in android?
I have done this way and my "temp.txt" file is in /raw folder. But i am not getting any output in file. Also I am not getting any error for this.
FileOutputStream fos = openFileOutput("temp", Context.MODE_APPEND);
fos.write("Example Text in a file".getBytes());
fos.flush();
fos.close();
Try this one
Try to put file int /asset folder and then do operations