save raw file into internal storage - android

I have a text file in raw folder and now i want to save it somewhere in the internal storage in android platform so that it is non-accessible.
Please if someone can help me out. Thanx in advance.

Something like this should work
InputStream is = new FileInputStream("path_to_your_file");
FileOutputStream fos = openFileOutput("your_file", Context.MODE_PRIVATE);
// read from InputStream and write ot OutputStream
More info about InternalStorage

Related

Android - Write file to assets folder

I want to download some data and store it into a file. I am aware that I cannot write to the assets dir or any other place because of the sole nature of the APK file.
I am wondering, if I create a file with something like:
FileOutputStream fileout = openFileOutput("text.txt", MODE_PRIVATE);
OutputStreamWriter outputWriter = new OutputStreamWriter(fileout);
outputWriter.write("write this string to file.");
outputWriter.close();
Since I cannot write any files to assets or any raw dir, where it will be written?
What will the location of that file be? Where will it be located in the Android file system ?
Since I cannot write any files to assets or any raw dir, where it will be written?
openFileOutput() writes to internal storage.
What will the location of that file be? Where will it be located in the Android file system ?
That can vary by device and user. It will be to the same directory that is returned via a call to getFilesDir().
You can get the path to your file created with openFileOutput like this: MainActivity.this.getFilesDir().getAbsolutePath()+"/text.txt" and you can read it using an object of FileInputStream FileInputStream fis = new FileInputStream(new File(MainActivity.this.getFilesDir().getAbsolutePath()+"/text.txt"));

can't find file created in dir returned by getFilesDir()

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

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..

android : best way to open a file in temprory memory

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 ReadWrite into textfile in android?

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

Categories

Resources