Deleting content of Internal Storage file in android - android

I have created a file for storing important data from my application , at the press of a button i want to clear data from that file without deleting it;I am using MODE_APPEND to append the data from my application.Please do suggest.

Try this
PrintWriter writer = new PrintWriter(file);
writer.print("");
writer.close();
this will print empty string in the file

Related

Use PrintWriter to recreate a text file in /res/raw/ with android?

I am creating an app where the configuration file needs to be re-written on startup and would like to use PrintWriter to create it. The problem is PrintWriter takes a file as a parameter. is there any way to reference a text document in /res/raw/ as File object in order to fit the parameters for PrintWriter?
I don't know if this will help but here is some code to better explain my question:
(its the basic idea but doesnt work...)
PrintWriter writer = new PrintWriter(new File("res/raw/config.txt"));
/*(I need to acces my config file... here ) */
You can't write to the res/raw directory because it is part of your apk file. You will need to either save the configuration a preference file using PreferenceManager.getDefaultSharedPreferences(Context context) or write a file to your app private directory. You can create a file like this:
PrintWriter writer = new PrintWriter(context.openFileOutput("config.txt", MODE_PRIVATE));

in which path should i need to put the text file to write in android

I have an android app and i need to write some string values in text file. i have coding to write in a text file. but i don't know where should i create my text file.Please refer my code below,
FileOutputStream fOut = null;
OutputStreamWriter osw = null;
try{
fOut = openFileOutput(“public.dat”, Context.MODE_PRIVATE);
osw = new OutputStreamWriter(fOut);
osw.write(“TEXT”);
osw.close();
fOut.close();
}catch(Exception e){
e.printStackTrace(System.err);
}
please inform me in which path should i create the text file.
If you want to store data locally to your app, on the device's internal file system (not on external SD card), you should always consider SharedPreferences, which are, by default, private files in the private storage space of your app, with convenient access.
SharedPreferences are perfect for key/value pairs, but not very suitable for certain data formats. So if you have different needs, the Context class has a broad variety of methods on offer. Please note that both Activity and Application objects are Contexts so you'll always be able to access these. Please refer to the documentation:
getDir()
getFilesDir()
openFileOutput()
There are also methods to get the path where external SD cards are mounted on the particular device, for temporary files, etc.
The best way is to create the file in the directory of your application in sdcard.
fOut = openFileOutput(Environment.getExternalStorageDirectory().getAbsolutePath()+“/yourAppFolder/public.dat”, Context.MODE_PRIVATE);
I think it is not a best optimal solution but it is worth full if it is working..

how to create .json file and how to write json object into json file ...In android

I am using this code for creating the new file and write the json object in test.json file but I am unable to create file.Please find mistake and give the best result
One things I have also write the code for permission in manifest file..
Thanks,
String fileName = "test";
File file = new File("/data/data/packagename/test.json");
file.createNewFile();
DataOutputStream fos = new DataOutputStream(openFileOutput(fileName , Context.MODE_PRIVATE));
if(file.exists()){
fos.write(jsString.getBytes());
fos.close();
}
This will not land on C:/ anyway. At best at your SD card or some other folder, and since android is a unix based system. you should be careful on how you use "slashes"

where created files are stored physically in android?

I am creating file using following code
OutputStreamWriter out = new OutputStreamWriter(openFileOutput("try.txt",0));
// write the contents on mySettings to the file
out.write("erterter");
// close the file
out.close();
Files created in this manner are stored in your application's private storage: /data/data/com.package/files

write data in binary format in Files in Android

hi all:
making a sample application in which we can store data on File and then could Read it.,
m using the following code.:
Code to Write:
OutputStreamWriter out = null;
try {
out = new OutputStreamWriter(openFileOutput("finear.fin", 0));
out.write(data); //data is String variable
out.close();
Toast.makeText(getApplicationContext(), "Data has been Saved! ", Toast.LENGTH_LONG).show();
}
Code to Read:
instream = openFileInput("finear.fin");
if(instream!=null)
{
InputStreamReader inputReader = new InputStreamReader(instream);
BufferedReader buffreader = new BufferedReader(inputReader);
String dataRead = buffreader.readLine();
}
Now problem is: The Data is stored in simple Text Form but i want that no one could read my data from that file and it should store the data in some binary form or some symbols type format so that when one opens the file in windows it should not display my text stored in the file or should display some other data.. BUT also that data could be readable in String form when i read. PLease Help
You can use Android's internal storage, which is private to your application:
You can save files directly on the device's internal storage. By default, files saved to the internal storage are private to your application and other applications cannot access them (nor can the user). When the user uninstalls your application, these files are removed.
Here you can find the docs: http://developer.android.com/guide/topics/data/data-storage.html#filesInternal
Encrypt the String before saving it to the stream and the other way to read it. If you want your file's contents to be unreadable, encryption would be a win.

Categories

Resources