android 操作文件分为两种,
第一种:直接写到手机内存中,手机内存有限。
第二种:写到手机的内存卡(SD)中,进行操作文件。
以下分为两种情况的读精心操作,一个简单的demo.在此做一个记录。
代码。
package com.hkrt.action; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintStream; import java.io.RandomAccessFile; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.os.Environment; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class IOtestActivity extends Activity { public EditText edit; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); edit = (EditText)this.findViewById(R.id.inputEdit); Button read = (Button)this.findViewById(R.id.read); Button write = (Button)this.findViewById(R.id.write); write.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // write(edit.getText().toString()); writeDB(edit.getText().toString()); Toast.makeText(IOtestActivity.this, "写入成功", 0).show(); } }); read.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // String str= read(); String str=readSD(); if(str!=null){ Toast.makeText(IOtestActivity.this, "获取到的值:"+str, 0).show(); } } }); } /**从手机内存中读取*/ private String read() { StringBuffer sb = null; try { FileInputStream fis = openFileInput("info"); byte[] buff = new byte[8192]; int hasread = 0; sb = new StringBuffer(); while ((hasread = fis.read(buff)) > 0) { sb.append(new String(buff, 0, hasread)); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return sb.toString(); } /**写到手机内存中*/ private void write(String content) { FileOutputStream fos = null; try { fos = openFileOutput("info", Context.MODE_APPEND); PrintStream ps = new PrintStream(fos); ps.print(content); } catch (FileNotFoundException e) { e.printStackTrace(); } finally { if (fos != null) { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } } } /**从sd卡中取内容*/ private String readSD(){ FileInputStream fis=null; StringBuffer sb = new StringBuffer(""); //检测是否有sd卡,有没有对sd卡进行读写的权限 if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ File sdCardDir = Environment.getExternalStorageDirectory();//获取到文件的存储位置 try { fis= new FileInputStream(new File(sdCardDir.getCanonicalFile()+"/info2")); BufferedReader br = new BufferedReader(new InputStreamReader(fis)); String str=null; while((str=br.readLine())!=null){ sb.append(str); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ if(fis!=null){ try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } } } return sb.toString(); } /**把内容写到SD卡中*/ private void writeDB(String str){ RandomAccessFile random =null; if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ try { File sdCardDir = Environment.getExternalStorageDirectory();//获取到文件的存储位置 File targetFile= new File(sdCardDir+"/info2"); random= new RandomAccessFile(targetFile, "rw"); random.seek(targetFile.length()); random.write(str.getBytes()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ if(random!=null){ try { random.close(); } catch (IOException e) { e.printStackTrace(); } } } } } }
效果图:
SD