SoFunction
Updated on 2025-04-09

Android development examples to implement Files file reading and parsing function

This article describes the Android development and implementation of Files file reading and parsing function. Share it for your reference, as follows:

package ;
import .;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
public class MainActivity extends AppCompatActivity {
  EditText edt;
  Button btn;
  TextView tv;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    (savedInstanceState);
    setContentView(.activity_main);
    edt = (EditText) findViewById();
    btn = (Button) findViewById();
    tv = (TextView) findViewById();
    (new () {
      @Override
      public void onClick(View view) {
        WriteFiles(().toString());
        (readFiles());
      }
    });
  }
  //Save the file content  public void WriteFiles(String content){
    try {
      FileOutputStream fos = openFileOutput("",MODE_PRIVATE);
      (());
      ();
    } catch (FileNotFoundException e) {
      ();
    } catch (IOException e) {
      ();
    }
  }
  //Read the file  public String readFiles(){
    String content = null;
    try {
      FileInputStream fis = openFileInput("");
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      byte[]buffer = new byte[1024];
      int len = 0;
      while ((len = (buffer))!=-1)
      {
        (buffer,0,len);
      }
      content = ();
      ();;
      ();
    } catch (FileNotFoundException e) {
      ();
    } catch (IOException e) {
      ();
    }
    return content;
  }
}

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:andro
  xmlns:tools="/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:paddingBottom="@dimen/activity_vertical_margin"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  tools:context="">
  <EditText
    android:layout_width="wrap_content"
    android:layout_height="200dp"
    android:
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />
  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:
    android:layout_below="@+id/editText"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="90dp" />
  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Text"
    android:
    android:layout_below="@+id/button"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />
</RelativeLayout>

For more information about Android related content, please check out the topic of this site:Android file operation skills summary》、《Android View View Tips Summary》、《Android programming activity operation skills summary》、《Android layout layout tips summary》、《Android development introduction and advanced tutorial》、《Android resource operation skills summary"and"Android control usage summary

I hope this article will be helpful to everyone's Android programming design.