Example of reading and writing android files

Source: Internet
Author: User

(1) Procedures

The two buttons in the code process the event and read and write the text respectively.

1) file write operations

First, call the openFileOutPut () method of Activity to obtain the output stream of the text file. The first parameter is the name of the text file, and the second parameter is the file opening method.

Call the write () method of the Outputstream object to write the obtained text information in Textview to the outputstream object, and call the close () method to complete the write operation.

2) file read Operations

First, call the openFileInPut () method of Activity to obtain the input stream of the text file,

Call the read () method of the input stream object to read the byte information in the input stream into a ByteArrayOutputStream object. Finally, convert ByteArrayOutputStream to string and display it in Textview.

(2) Layout File

 
     
          
           
       
      
      
          
       
      
          
           
       
      
  
 

(2) Code

package com.liuzuyi.file;import java.io.ByteArrayOutputStream;import java.io.InputStream;import java.io.OutputStream;import android.support.v7.app.ActionBarActivity;import android.support.v7.app.ActionBar;import android.support.v4.app.Fragment;import android.app.Activity;import android.content.Context;import android.os.Bundle;import android.util.Log;import android.view.LayoutInflater;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import android.widget.Toast;import android.os.Build;public class MainActivity extends Activity {private  EditText filename;private  EditText context;private TextView textcontent;private static final String TAG="simplefile";protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);filename=(EditText)findViewById(R.id.filename);context=(EditText)findViewById(R.id.content);textcontent=(TextView)findViewById(R.id.textcontent);Button savebtn=(Button)this.findViewById(R.id.savebutton);Button viewbtn=(Button)this.findViewById(R.id.readbutton);savebtn.setOnClickListener(l);viewbtn.setOnClickListener(l);}   private View.OnClickListener l =new OnClickListener() {public void onClick(View v) { Button button =(Button)v;  String namestr = filename.getText().toString().trim();  String contentstr =context.getText().toString();  switch ( button.getId() ) {case R.id.savebutton:String resid_s ="success";OutputStream filsos= null;try {filsos=MainActivity.this.openFileOutput(namestr+".txt", Context.MODE_APPEND) ;filsos.write(contentstr.getBytes());filsos.close();} catch (Exception e) { resid_s = "faile"; e.printStackTrace();}Toast.makeText(MainActivity.this,  resid_s,Toast.LENGTH_LONG).show();Log.i(TAG, namestr);Log.i(TAG, contentstr);break;case R.id.readbutton:String resid_v ="success";InputStream filsIs= null;String contentst = null;try {filsIs=MainActivity.this.openFileInput(namestr+".txt") ;ByteArrayOutputStream oStream = new ByteArrayOutputStream();byte[] buffer = new byte[1024];int len = -1;  while( (len = filsIs.read(buffer)) != -1 )    {oStream.write(buffer,0,len);}  contentst=oStream.toString();  oStream.close();  filsIs.close();} catch (Exception e) { resid_v ="faile"; e.printStackTrace();}textcontent.setText( contentst);Log.i(TAG, contentst);Toast.makeText(MainActivity.this, resid_v,Toast.LENGTH_LONG).show();Log.i(TAG,namestr);break;}  }    };}



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.