Write to and read from a file

Quickly share your Android Code Snippets here...

Re: Write to and read from a file

Postby iwooll » Tue Sep 13, 2011 3:21 pm

I have put this in my application and it works just fine when I put it at the start of the onCreate method but it won't work if I put it in an onClick listener as per my code below.

Button saveButton = (Button) findViewById(R.id.button2);
saveButton.setOnClickListener(new OnClickListener(){

public void onClick(View v) {
WriteSettings(this,settings);

}
});

I get the message: The method WriteSettings(Context, String) in the type NewCustomer is not applicable for the arguments (new View.OnClickListener(){}, String)

Can anybody point me in the right direction?
iwooll
Once Poster
Once Poster
 
Posts: 1
Joined: Tue Sep 13, 2011 3:15 pm

Top

Re: Write to and read from a file

Postby karthikk » Sat Nov 19, 2011 3:47 am

Thank you for the wonderful post! :)
karthikk
Freshman
Freshman
 
Posts: 5
Joined: Wed Nov 16, 2011 8:05 am

Re: Write to and read from a file

Postby redbulletnl » Mon Nov 21, 2011 9:12 am

If you want to write @ your sd card you need to ad a permission to your manifest
Code: Select all
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>




Code: Select all
try{

           File file = new File(Environment.getExternalStorageDirectory(), fileName);

           BufferedWriter writer = new BufferedWriter(new FileWriter(file,true));
          
           writer.write("\n");
           writer.write("Some text");
           writer.flush();
           writer.close();
           }

           catch (IOException e) {
               e.printStackTrace();
           }
redbulletnl
Experienced Developer
Experienced Developer
 
Posts: 84
Joined: Wed Sep 14, 2011 7:44 am

Top
Previous

Return to Code Snippets for Android

Who is online

Users browsing this forum: No registered users and 3 guests