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
Related
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"));
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 had created files i.e., txt files and stored values into it using openFileOutput fn. This function creates the txt file in the data/data/app_name/files folder.. Now i am trying to save a media files like .mp3 file to that data/data/app-name folder.. Is it possible to do so?
Thanks in advance.
fOut = openFileOutput("a.txt", MODE_PRIVATE);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
String aa="hi";
osw.write(aa);
osw.flush();
osw.close();
This is how i had written a file to the data/data/app-name folder.. Nw i am trying to write a mp3 file but the osw.write(aa) accepts string or char array..
Context.openFileOutput() (reference) creates a FileOutputStream object which is capable of writing binary data (see write() method), so the answer is Yes, it can be used to write media files.
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"
Im getting an input stream, which is an mp3 in my raw folder, and then I am trying to write it back to the raw folder (I know this sounds stupid but I want to do it), but I get an error - file not found, myFile.mp3. Any ideas where I am going wrong?
try
{
File f=new File("android.resource//com.apps/raw/myFile.mp3");
InputStream myRawResource = context.getActivity().getResources().openRawResource(mp3ID);
OutputStream out=new FileOutputStream(f);
byte buf[]=new byte[1024];
int len;
while((len=myRawResource.read(buf))>0)
out.write(buf,0,len);
out.close();
myRawResource.close();
}
catch (IOException e){
//error
}
You can't write the raw and assets folder in android.
Edit:
Android documentation
I copied the key phrase (Tip from the documentation):
If you want to save a static file in your application at compile time, save the file in your project res/raw/ directory. You can open it with openRawResource(), passing the R.raw. resource ID. This method returns an InputStream that you can use to read the file (but you cannot write to the original file).
This can't be done because resources, at runtime, are read-only, are stored in the APK, and would need to be compiled for inclusion in R at compile time.