Need help in saving data from app

Search This thread

arpitkh96

Senior Member
Feb 17, 2012
1,156
645
27
panipat
Code:
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import java.io.*;
public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    [user=439709]@override[/user]
    public void onCreate(Bundle savedInstanceState)
{
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }public void save(View view){
EditText e=(EditText) findViewById(R.id.editText1);
String a=String.valueOf(((e.getText().toString())));
try{FileOutputStream s=new FileOutputStream("/data/data/com.mycompany.myapp/first.sav");
ObjectOutputStream save= new ObjectOutputStream(s);
save.writeObject(a);}
catch(Exception f){e.setText(""+f);}
}
}
This code saves the entered data in first.sav file. But if i add different data again, it replaces the earlier saved data but i want both the older and newer saved. How to do it
Sent from my GT-S5570 using xda premium
 

mkrstudio

Member
Oct 2, 2013
28
40
You mean appending some new text to your existing file?

Code:
try {
    PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("/data/data/com.mycompany.myapp/first.sav", true)));
    out.println("the text");
    out.close();
} catch (IOException e) {
    e.setText(""+e);
}
 
  • Like
Reactions: arpitkh96

arpitkh96

Senior Member
Feb 17, 2012
1,156
645
27
panipat
You mean appending some new text to your existing file?

Code:
try {
    PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("/data/data/com.mycompany.myapp/first.sav", true)));
    out.println("the text");
    out.close();
} catch (IOException e) {
    e.setText(""+e);
}

Thnxx guys my problem is solved now





you can use Filewriter like this



the 2nd parameter will open file in append mode.







As per Developer's site:

Code:
 FileOutputStream(String path, boolean append)
Constructs a new FileOutputStream that writes to path.

http://developer.android.com/reference/java/io/FileOutputStream.html

Using your current code, just add true to your FileOutputStream



Sent from my GT-S5570 using xda premium
 

Top Liked Posts