Android application cant write to file at second time - android

I have problem with writing data to file in Android Emulator. In my Android Emulator in /data folder I have created MyLogs folder and give full access to it. After I run my application and it create Log.txt file and place it in /data/MyLogs folder. All is Okay. After I have run my application in second time and application try to write some information in same file, but it cant't.
I think the main reason is that then at first time my application creates file the creator is different from second time. thats why I can't write to file second time !
Who have any ideas ?

To make a file writable for more than once use Context.MODE_APPEND
Sample Code
FileOutputStream fos;
try {
fos = openFileOutput("nuzz.txt", Context.MODE_APPEND);
fos.write(string.getBytes());
fos.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fos = openFileOutput("nuzz.txt", Context.MODE_APPEND);
fos.write("bye".getBytes());
fos.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Thanks
Deepak

Related

Android: files saved inside getFilesDir() disappear after application restart

I want to save a file to app-specific storage, like shown in documentation:
String filename = "myfile.txt";
String fileContents = "Hello world!";
try (FileOutputStream fos = context.openFileOutput(filename, Context.MODE_PRIVATE)) {
fos.write(fileContents.toByteArray());
}
Unfortunately, every time I restart the application all data are lost.
How to prevent data from being deleted?
Please note I am not interested in using SharedPreferences/External Storage/Database. Android Studio 3.5.3.
You need to close the file once you are done using it. Otherwise it is not stored to the disk, but only to the memory.
That's why when you "shut down" the app, and start over, it's not there anymore.
FileOutputStream fos = context.openFileOutput(filename, Context.MODE_PRIVATE);
try {
fos.write(fileContents.toByteArray());
} catch (IOException e) {
e.printStackTrace();
} finally{
try{
if (fos!=null){
fos.flush();
fos.close();
}
} catch( Exception e) {
e.printStackTrace();
}
}

Checking where the file is stored in android?

I am writing a log data to the File in android. But I am unable to understand where this file is stored on my android device ( google nexsus 7 ). Where and How should I look for the contents of the file ?
Following is the piece of code I am using to write log data to the file.
public File AppendingLog(String LogData){
try {
File logFile = new File(Environment.getExternalStorageDirectory(),
"yourLog.txt");
if (!logFile.exists()) {
try {
logFile.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
BufferedWriter buf = new BufferedWriter(new FileWriter(logFile,
true));
buf.append(LogData);
buf.newLine();
buf.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
return logFile;
}
android.util.Log.d("myapp", logFile.toString());
On most devices, it will be in /sdcard/. The exact path will be /sdcard/yourLog.txt.
As you are using Environment.getExternalStorageDirectory(), the actual path will differ between devices. Some devices return the external sd card path, but most will return the internal sd card's path, which usually is /sdcard/.
I think answers below are what you need.
You can also download Astro File Manager App which will also give you an idea where your files go on Android System.

android EROFS in eclipse

I am trying to read some files and put them into a new file using the below method in Eclipse.
But I am getting a read-only file system EROFS error in eclipse at run time.
Input file names will be provided as function parameter. Files are present in res\raw folder.
SampleFile.mp3 is an empty audio file placed at the location.
public void myfun(Set s)
{
try {
FileOutputStream fos=new FileOutputStream(".\\res\raw\sampleFile.mp3",true);
FileInputStream fis;
Iterator ptr=s.iterator();
String str;
while(ptr.hasNext()!=null)
{
str=ptr.next().toString();
fis=new FileInputStream(str);
int i;
while((i=fis.read())!=-1)
{
fos.write(i);
}
fis.close();
}
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
You are trying to write into the 'res/raw' folder of the current directory, but android does not work like that. Go read the Storage Options section of the Android Development Guide, and choose one that fits your needs (and use case).
Also, you want to concatenate multiple files into an mp3? Are they by any chance... mp3 files themselves that you want to play?

How to create and write xml-data in file on internal memory of android?

I have a problem in android file writing...I am using XML Serializer..I am importing all necessary files...but still not working..
Actually its creating file but file-size is zero (when i am watching files in DDMS)
So please share any solution you have
try {
fos=openFileOutput("ticket_new.txt", Context.MODE_PRIVATE);
OutputStreamWriter osw = new OutputStreamWriter(fos);
XmlSerializer tckt = Xml.newSerializer();
tckt.setOutput(osw);
tckt.startDocument(null,Boolean.valueOf(true));
tckt.setFeature("http://www.xmlpull.org/v1/doc/features.html#indent-output", true);
tckt.startTag(null,"results");
tckt.attribute(null, "count","1");
tckt.startTag(null,"result");
tckt.startTag(null,"id");
tckt.text("1");
tckt.endTag(null,"id");
tckt.startTag(null,"name");
tckt.text("Pwd");
tckt.endTag(null,"name");
tckt.endTag(null,"result");
tckt.endTag(null,"results");
tckt.endDocument();
fos.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Writing to an existing resource XML file in Android

I am trying to write to an existing XML file in the /res/xml directory on an Android device. Two questions:
1) Are these files able to be overwritten?
2) If so, how can I obtain a handle to that file?
I am trying this to no avail:
FileOutputStream fos;
try {
fos = openFileOutput("/res/xml/scores.xml", Context.MODE_PRIVATE);
fos.write(writer.toString().getBytes());
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
UPDATE
Trying to read from the file using:
....
PriorityQueue<Score> scores = new PriorityQueue<Score>();
XmlPullParserFactory xmlFac = XmlPullParserFactory.newInstance();
XmlPullParser qXML = xmlFac.newPullParser();
File scoresFile = new File(c.getExternalFilesDir(null), "scores.xml");
InputStream is = new FileInputStream(scoresFile);
qXML.setInput(is,null);
....
Trying to write to the file using:
....
File scoresFile = new File(getExternalFilesDir(null), "scores.xml");
OutputStream os = new FileOutputStream(scoresFile);
os.write(writer.toString().getBytes());
os.flush();
os.close();
....
You need to use the external storage on the device. You can't and shouldn't be writing to the resources. Instead, copy the resource to the external storage, and then modify it there.
EDIT: You can also use your application's internal data folder to save files to, but again, you will want to copy your resource there if you want to modify it. This will allow the files to remain internal to your application.

Categories

Resources