文件存储

布局:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout
 3     xmlns:android="http://schemas.android.com/apk/res/android"
 4     xmlns:app="http://schemas.android.com/apk/res-auto"
 5     xmlns:tools="http://schemas.android.com/tools"
 6     android:layout_width="match_parent"
 7     android:layout_height="match_parent"
 8     android:orientation="vertical"
 9     tools:context="net.bwie.localdata.activity.FileActivity">
10 
11     <Button
12         android:id="@+id/read_file_btn"
13         android:layout_width="wrap_content"
14         android:layout_height="wrap_content"
15         android:text="读取文件"/>
16 
17     <Button
18         android:id="@+id/write_file_btn"
19         android:layout_width="wrap_content"
20         android:layout_height="wrap_content"
21         android:text="写入文件"/>
22 
23     <TextView
24         android:id="@+id/result_tv"
25         android:text="结果"
26         android:layout_width="wrap_content"
27         android:layout_height="wrap_content"/>
28 
29 </LinearLayout>

Activity:

  1 public class FileActivity extends AppCompatActivity implements View.OnClickListener {
  2 
  3     protected Button mReadFileBtn;
  4     protected Button mWriteFileBtn;
  5     protected TextView mResultTv;
  6 
  7     public static void startActivity(Context context) {
  8         context.startActivity(new Intent(context, FileActivity.class));
  9     }
 10 
 11     @Override
 12     protected void onCreate(Bundle savedInstanceState) {
 13         super.onCreate(savedInstanceState);
 14         super.setContentView(R.layout.activity_file);
 15         initView();
 16     }
 17 
 18     @Override
 19     public void onClick(View view) {
 20         if (view.getId() == R.id.read_file_btn) {
 21             String result = readFile();
 22             mResultTv.setText(result);
 23 
 24         } else if (view.getId() == R.id.write_file_btn) {
 25             writeFile();
 26         }
 27     }
 28 
 29     // 读取文件
 30     private String readFile() {
 31         String filePath = Environment.getExternalStorageDirectory().getPath() + "/abc/";
 32         String fileName = "xyz.txt";
 33 
 34         File file = new File(filePath, fileName);
 35 
 36         BufferedReader br = null;
 37         try {
 38             br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
 39 
 40             String result = "";
 41             String line = "";
 42 
 43             while ((line = br.readLine()) != null) {
 44                 result += line;
 45             }
 46             return result;
 47 
 48 
 49         } catch (Exception e) {
 50             e.printStackTrace();
 51         } finally {
 52             if (br != null) {
 53                 try {
 54                     br.close();
 55                 } catch (IOException e) {
 56                     e.printStackTrace();
 57                 }
 58             }
 59         }
 60         return null;
 61     }
 62 
 63     // 写入文件
 64     private void writeFile() {
 65 
 66         // 外部存储私有路径:Android文件夹
 67 //        String privatePath = getExternalFilesDir(null).getPath();// 私有路径不分类为null
 68 //        String filePath = privatePath + "/abc/";
 69 
 70         // 外部存储公共路径:DICM,DOWNLOAD,MUSIC等系统提供的文件夹
 71 //        String publicPath = Environment
 72 //                .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
 73 //                .getPath();
 74 //        String filePath = publicPath + "/abc/";
 75 
 76         // 自定义文件路径
 77         String rootPath = Environment.getExternalStorageDirectory().getPath();// 外部存储路径(根目录)
 78         String filePath = rootPath + "/abc/";
 79 
 80         String fileName = "xyz.txt";
 81 
 82         File file = new File(filePath, fileName);
 83 
 84         FileOutputStream fos = null;
 85         try {
 86             fos = new FileOutputStream(file);
 87             fos.write("asdasdas".getBytes());
 88             Toast.makeText(this, "成功", Toast.LENGTH_SHORT).show();
 89         } catch (Exception e) {
 90             e.printStackTrace();
 91             Log.d("1507", "error: " + e.getMessage());
 92         } finally {
 93             if (fos != null) {
 94                 try {
 95                     fos.close();
 96                 } catch (IOException e) {
 97                     e.printStackTrace();
 98                 }
 99             }
100         }
101 
102     }
103 
104     private void initView() {
105         mReadFileBtn = (Button) findViewById(R.id.read_file_btn);
106         mReadFileBtn.setOnClickListener(FileActivity.this);
107         mWriteFileBtn = (Button) findViewById(R.id.write_file_btn);
108         mWriteFileBtn.setOnClickListener(FileActivity.this);
109         mResultTv = (TextView) findViewById(R.id.result_tv);
110     }
111 }

权限:

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

posted @ 2017-11-23 09:36  钱大少丶  阅读(221)  评论(0)    收藏  举报