Storing and reading the Android File IO streams

A, File Internal memory:

1, the information storage function:

//存储信息
private void save(String content){
    FileOutputStream fileOutputStream=null;
    try {
        fileOutputStream=openFileOutput(mFileName, MODE_PRIVATE);
        fileOutputStream.write(content.getBytes());
    } catch (IOException e) {
        e.printStackTrace();
    }finally {
        try {
            assert fileOutputStream != null;
            fileOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

2, a function of information read:

//读取信息
private String read(){
    FileInputStream fileInputStream=null;
    try {
        fileInputStream=openFileInput(mFileName);
        byte[] buff=new byte[1024];
        StringBuilder sb=new StringBuilder();
        int len=0;
        while ((len=fileInputStream.read(buff)) >0 ){
            sb.append(new String(buff,0, len));
        }
        return sb.toString();
    } catch (IOException e) {
        e.printStackTrace();
    }finally {
        try {
            if (fileInputStream != null) {
                fileInputStream.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return  null;
}

3, the button in the button event listener call:

//存储
  mBtnFileSave.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
          save(mEtFileName.getText().toString());
      }
  });
//读取
  mBtnFileShow.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
          mTvFileContent.setText(read());
      }
  });

Two, File external storage:

1, first add access in the AndroidManifest.xml file:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

2, the home interface onCreate method app corresponding to the added Activity:

ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},1);

3, add the function information stored in the page in Activity:

//存储信息
private void save(String content){
   FileOutputStream fileOutputStream=null;
   try {
       //创建文件夹
       File dir=new File(Environment.getExternalStorageDirectory(), "FILE");
       if (!dir.exists()){
           dir.mkdirs();
       }
       //创建文件
       File file=new File(dir, mFileName);
       if (!file.exists() ){
           file.createNewFile();
       }
       fileOutputStream=new FileOutputStream(file);
       fileOutputStream.write(content.getBytes());
   } catch (IOException e) {
       e.printStackTrace();
   }finally {
       try {
           assert fileOutputStream != null;
           fileOutputStream.close();
       } catch (IOException e) {
           e.printStackTrace();
       }
   }

}

4, add a function to read information:

//读取信息
private String read(){
   FileInputStream fileInputStream=null;
   try {
       File file=new File(Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator + "FILE",mFileName);
       fileInputStream = new FileInputStream(file);
       byte[] buff=new byte[1024];
       StringBuilder sb=new StringBuilder();
       int len=0;
       while ((len=fileInputStream.read(buff)) >0 ){
           sb.append(new String(buff,0, len));
       }
       return sb.toString();
   } catch (IOException e) {
       e.printStackTrace();
   }finally {
       try {
           if (fileInputStream != null) {
               fileInputStream.close();
           }
       } catch (IOException e) {
           e.printStackTrace();
       }
   }
   return  null;
}

5, the above two methods onCreate call function:

//存储
mBtnFileSave.setOnClickListener(new View.OnClickListener() {
	   @Override
	   public void onClick(View v) {
	       save(mEtFileName.getText().toString());
	   }
});
//读取
mBtnFileShow.setOnClickListener(new View.OnClickListener() {
	   @Override
	   public void onClick(View v) {
	       mTvFileContent.setText(read());
	   }
});
Published 76 original articles · won praise 52 · views 40000 +

Guess you like

Origin blog.csdn.net/WU2629409421perfect/article/details/104074923