案例:
activity_main.xml
MainActivity.java
package com.example.administrator.filemanager; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.os.Environment; import android.support.v7.app.ActionBarActivity; import android.text.method.ScrollingMovementMethod; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.TextView; import android.widget.Toast; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintStream; public class MainActivity extends Activity { private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView) findViewById(R.id.textView); textView.setTextSize(14); } /*写入私有文件*/ public void writePrivateFileClick(View view) { try { //Context.MODE.PRIVATE 创建私有文件 APPEND是增加内容 OutputStream out = openFileOutput("xfei.txt", Context.MODE_APPEND); String info = "我是中国人"; byte[] bytes = info.getBytes(); out.write(bytes, 0, bytes.length); out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /*读取私有文件*/ public void readPrivateFileClick(View view) { try { InputStream in = openFileInput("xfei.txt"); byte[] bytes = new byte[1024]; StringBuffer sb = new StringBuffer(); int len = -1; while ((len = in.read(bytes)) != -1) { sb.append(new String(bytes, 0, len)); } in.close(); textView.setText(" " + sb); // Toast.makeText(this,sb,Toast.LENGTH_SHORT).show(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } //读取目录raw下的文件 public void readRAWFileClick(View view) { try { InputStream in = getResources().openRawResource(R.raw.xfei); byte[] bytes = new byte[1024]; StringBuffer sb = new StringBuffer(); int len = -1; while ((len = in.read(bytes)) != -1) { sb.append(new String(bytes, 0, len)); } in.close(); // Toast.makeText(this,sb,Toast.LENGTH_SHORT).show(); textView.setText(" " + sb); textView.setMovementMethod(ScrollingMovementMethod.getInstance()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } //写入一个内部私有缓存文件 public void writePrivateCacheDataClick(View view) { //创建一个缓存的文件 //String temp= getCacheDir()+"/temp.tmp"; //System.out.println(); try { File temp = File.createTempFile("temp", null, getCacheDir()); FileOutputStream out = new FileOutputStream(temp); PrintStream ps = new PrintStream(out); ps.print("我可是要成为编程王的男人"); ps.close(); out.close(); } catch (IOException e) { e.printStackTrace(); } } public void isSDCardClick(View view) { //判断是否有SD卡 if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { Toast.makeText(this, "有SD卡", Toast.LENGTH_SHORT).show(); if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED_READ_ONLY)) { Toast.makeText(this, "SDCard只读", Toast.LENGTH_SHORT).show(); } else {//sdcard路径 System.out.println(Environment.getExternalStorageDirectory().getPath()); //访问android内置的文件夹 System.out.println(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)); } } else { Toast.makeText(this, "没有SD卡", Toast.LENGTH_SHORT).show(); } } //写SD卡私有文件 public void writeSDcardPrivateFileClick(View view) { //获得路径 File file = getExternalFilesDir(null); if (file != null) { try { FileOutputStream out = new FileOutputStream(file + "/xfei.txt"); PrintStream ps = new PrintStream(out); ps.print("今天吃药没?"); ps.close(); out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } //写入SD卡 私有缓存文件 public void writeSDcardPrivateCacheFileClick(View v) { try { File temp= File.createTempFile("xfei",null,getExternalCacheDir()); if (temp != null) { FileOutputStream out = new FileOutputStream(temp); PrintStream ps = new PrintStream(out); ps.print("今天吃药没?给你药吃"); ps.close(); out.close(); } } catch (IOException e) { e.printStackTrace(); } } }