File read and write : File « File « Android






File read and write

    

/***
  Copyright (c) 2008-2009 CommonsWare, LLC
  
  Licensed under the Apache License, Version 2.0 (the "License"); you may
  not use this file except in compliance with the License. You may obtain
  a copy of the License at
    http://www.apache.org/licenses/LICENSE-2.0
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/

package app.test;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

public class Test extends Activity {
  private final static String NOTES="notes.txt";
  private EditText editor;
  
  @Override
  public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);
    editor=(EditText)findViewById(R.id.editor);
    
    Button btn=(Button)findViewById(R.id.close);
    
    btn.setOnClickListener(new Button.OnClickListener() {
      public void onClick(View v) {
        finish();
      }
    });
  }
  
  public void onResume() {
    super.onResume();
    
    try {
      InputStream in=openFileInput(NOTES);
      
      if (in!=null) {
        InputStreamReader tmp=new InputStreamReader(in);
        BufferedReader reader=new BufferedReader(tmp);
        String str;
        StringBuilder buf=new StringBuilder();
        
        while ((str = reader.readLine()) != null) {
          buf.append(str+"\n");
        }
        
        in.close();
        editor.setText(buf.toString());
      }
    }
    catch (java.io.FileNotFoundException e) {
      // that's OK, we probably haven't created it yet
    }
    catch (Throwable t) {
      Toast
        .makeText(this, "Exception: "+t.toString(), 2000)
        .show();
    }
  }
  
  public void onPause() {
    super.onPause();
    
    try {
      OutputStreamWriter out=
          new OutputStreamWriter(openFileOutput(NOTES, 0));
      
      out.write(editor.getText().toString());
      out.close();    
    }
    catch (Throwable t) {
      Toast
        .makeText(this, "Exception: "+t.toString(), 2000)
        .show();
    }
  }
}


//main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent" 
  android:layout_height="fill_parent"
  android:orientation="vertical">
  <Button android:id="@+id/close"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:text="Close" />
  <EditText
    android:id="@+id/editor"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:singleLine="false"
    android:gravity="top"
    />
</LinearLayout>

   
    
    
    
  








Related examples in the same category

1.Read the created file and display to the screen
2.Create a Uri for files on external storage
3.Create a Uri for files on internal storage
4.View/Listen to media file
5.File Copy Util
6.Copy File Thread
7.Adaptation of the class File to add some usefull functions
8.Save to Android local file system
9.Using Ini file to manage Preferences
10.File Cache
11.Format File Size
12.File Upload
13.File Name Extension Utils
14.Write and append string to file
15.Transfers required files to the the private file system part in order to be access them from C Code.
16.returns a canonical line of a obj or mtl file.
17.Trims down obj files, so that they may be parsed faster later on.
18.Format File Size:KB, MB, GB
19.Get File Contents as String
20.Read File and return CharSequence
21.Copy File
22.Read/write From FileSystem, browse Account
23.Create Path and file under External Storage
24.Get File Extension Name
25.Extract File Name From String
26.Write String to file
27.Create an output file from raw resources.
28.Copies a directory from a jar file to an external directory.
29.Reads a file from /raw/res/ and returns it as a String
30.Read Config ini file
31.Read Write File Manager
32.Copy two files
33.Get the filename of the media file and use that as the default title
34.Copy File and Directory
35.Determines the MIME type for a given filename.
36.Move a file stored in the cache to the internal storage of the specified context
37.CharSequence from File
38.File Manager
39.Save Exception to a file
40.Delete a file and create directory
41.Get a list of filenames in this folder.
42.Delete Directory
43.Delete Directory 2
44.Delete Directory 3
45.Get readable folder size
46.Copy chars from a large (over 2GB) Reader to a Writer.