Android的檔案儲存(寫入和讀取)
阿新 • • 發佈:2019-01-25
package com.t20.fileop; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import android.os.Bundle; import android.app.Activity; import android.content.Context; import android.view.View; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { private EditText etContent; private TextView tvContent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //輸入框 etContent=(EditText) findViewById(R.id.etContent); //文字框 tvContent=(TextView) findViewById(R.id.tvContent); } /** * 寫入檔案 * @param v */ public void write(View v){ //檔案輸出流 FileOutputStream out=null; //緩衝寫入器 BufferedWriter writer=null; String content=etContent.getText().toString(); if(content.isEmpty()){ Toast.makeText(MainActivity.this, "請輸入內容", Toast.LENGTH_SHORT).show(); return; } try { //安卓封裝好的檔案寫入檔案方法 out=openFileOutput("data.txt", Context.MODE_PRIVATE);//許可權:-rwx------(只能自己讀寫) writer=new BufferedWriter(new OutputStreamWriter(out)); //------------------檔名-------------寫入模式----- //開始寫入 writer.write(content); Toast.makeText(MainActivity.this, "檔案寫入成功!", Toast.LENGTH_SHORT).show(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { if(writer!=null){ writer.close(); } if(out!=null){ out.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } /** * 儲存檔案到SD卡(寫入SD卡需要許可權,在AndroidManifest.xml中設定android.permission.WRITE_EXTERNAL_STORAGE) * @param v */ public void writeSD(View v){ //檔案輸出流 FileOutputStream out=null; String content=etContent.getText().toString(); if(content.isEmpty()){ Toast.makeText(MainActivity.this, "請輸入內容", Toast.LENGTH_SHORT).show(); return; } //設定檔案路徑 File file=new File(getExternalFilesDir(null), "/data.txt"); try { out=new FileOutputStream(file); out.write(content.getBytes()); Toast.makeText(MainActivity.this, "成功儲存到SD卡!", Toast.LENGTH_SHORT).show(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { if(out!=null){ out.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } /** * 讀取檔案 * @param v */ public void read(View v){ //檔案輸入流 FileInputStream in=null; //緩衝讀取器 BufferedReader reader=null; StringBuilder builder=new StringBuilder(); try { //安卓封裝好的檔案讀取檔案方法 in=openFileInput("data.txt"); reader=new BufferedReader(new InputStreamReader(in)); //使用流逐行讀取 String line=""; while ((line=reader.readLine())!=null) { builder.append(line); } tvContent.setText(builder.toString()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { if(reader!=null){ reader.close(); } if(in!=null){ in.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } /** * 從SD卡中讀取檔案(讀取SD卡需要許可權,在AndroidManifest.xml中設定android.permission.READ_EXTERNAL_STORAGE) * @param v */ public void readSD(View v){ //檔案輸入流 FileInputStream in=null; //設定檔案路徑 File file=new File(getExternalFilesDir(null), "/data.txt"); try { in=new FileInputStream(file); //使用緩衝來讀 byte[] buf=new byte[1024];//每1024位元組讀一次 StringBuilder builder=new StringBuilder(); while (in.read(buf)!=-1) { builder.append(new String(buf).trim()); } tvContent.setText(builder.toString()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { if(in!=null){ in.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }