码迷,mamicode.com
首页 > 移动开发 > 详细

Android中的文件存储练习

时间:2014-12-16 14:46:39      阅读:252      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   ar   io   color   os   sp   java   

  1 package com.example.filedemo1;
  2 
  3 import java.io.BufferedReader;
  4 import java.io.BufferedWriter;
  5 import java.io.ByteArrayOutputStream;
  6 import java.io.File;
  7 import java.io.FileInputStream;
  8 import java.io.FileNotFoundException;
  9 import java.io.FileOutputStream;
 10 import java.io.FileReader;
 11 import java.io.FileWriter;
 12 import java.io.IOException;
 13 
 14 import android.os.Bundle;
 15 import android.app.Activity;
 16 import android.util.Log;
 17 import android.view.Menu;
 18 import android.view.View;
 19 import android.view.View.OnClickListener;
 20 import android.widget.Button;
 21 import android.widget.EditText;
 22 import android.widget.TextView;
 23 import android.widget.Toast;
 24 
 25 public class MainActivity extends Activity {
 26 //    private BufferedWriter bufferedWriter;
 27 //    private BufferedReader bufferedReader;
 28     private EditText edt;
 29     private Button bt;
 30     private TextView tv;
 31     @Override
 32     protected void onCreate(Bundle savedInstanceState) {
 33         super.onCreate(savedInstanceState);
 34         setContentView(R.layout.activity_main);
 35 //        File file = new File("/mnt/sdcard/text");
 36 //        if(!file.exists()){
 37 //            try {
 38 //                file.createNewFile();
 39 //                Toast.makeText(MainActivity.this, "创建文件成功!", Toast.LENGTH_SHORT).show();
 40 //            } catch (IOException e) {
 41 //                // TODO Auto-generated catch block
 42 //                e.printStackTrace();
 43 //            }
 44 //        }else{
 45 //            Toast.makeText(MainActivity.this, "文件已存在!", Toast.LENGTH_SHORT).show();
 46 //        }
 47 ////        file.delete();
 48 //        try {
 49 //            bufferedWriter = new BufferedWriter(new FileWriter("/mnt/sdcard/text",true));
 50 //            bufferedWriter.write("第一行!");
 51 //            bufferedWriter.newLine();
 52 //            bufferedWriter.write("第二行!");
 53 //            bufferedWriter.flush();
 54 //            bufferedWriter.close();
 55 //            bufferedReader = new BufferedReader(new FileReader("/mnt/sdcard/text"));
 56 //            Log.i("info",bufferedReader.readLine());
 57 //            Log.i("info",bufferedReader.readLine());
 58 //        } catch (IOException e) {
 59 //            // TODO Auto-generated catch block
 60 //            e.printStackTrace();
 61 //        }
 62 //        File file = this.getFilesDir();
 63 //        Log.i("info", file.toString());
 64 //        File file = this.getCacheDir();
 65 //        Log.i("info", file.toString());
 66 //        File file = this.getDir("imooc", MODE_PRIVATE);
 67 //        Log.i("info", file.toString());
 68 //        File file = this.getDir("mirror", MODE_PRIVATE);
 69 //        Log.i("info", file.toString());
 70 //        File file = this.getExternalCacheDir();
 71 //        Log.i("info", file.toString());
 72         edt = (EditText) findViewById(R.id.editText1);
 73         bt = (Button) findViewById(R.id.write);
 74         tv = (TextView) findViewById(R.id.contentvalue);
 75 //        try {
 76 //            bufferedWriter = new BufferedWriter(new FileWriter("/mnt/sdcard/text"));
 77 //            bufferedReader = new BufferedReader(new FileReader("/mnt/sdcard/text"));
 78 //            bt.setOnClickListener(new OnClickListener() {
 79 //                
 80 //                @Override
 81 //                public void onClick(View v) {
 82 //                    // TODO Auto-generated method stub
 83 //                    String str = edt.getText().toString();
 84 //                    Log.i("info", str);
 85 //                    if(str != null) {                
 86 //                            try {
 87 //                                bufferedWriter.write("a");
 88 //                                bufferedWriter.flush();
 89 //                                bufferedWriter.close();
 90 //                                str = bufferedReader.readLine();
 91 //                                Log.i("info", str);
 92 //                                if(str!= null){
 93 //                                    Log.i("info", str);
 94 //                                    tv.append(str);
 95 //                                    Log.i("info", str);
 96 //                                    str = bufferedReader.readLine();
 97 //                                }
 98 ////                                Log.i("info", str);
 99 //                                bufferedReader.close();
100 //                            } catch (IOException e) {
101 //                                // TODO Auto-generated catch block
102 //                                e.printStackTrace();
103 //                            }                
104 //                    }
105 //                }
106 //            });
107 //        } catch (IOException e) {
108 //            // TODO Auto-generated catch block
109 //            e.printStackTrace();
110 //        }
111         bt.setOnClickListener(new OnClickListener() {
112             
113             @Override
114             public void onClick(View v) {
115                 // TODO Auto-generated method stub
116                 try {
117                     writeFiles(edt.getText().toString());
118                     tv.setText(readFiles());
119                 } catch (IOException e) {
120                     // TODO Auto-generated catch block
121                     e.printStackTrace();
122                 }
123             }
124         });
125     }
126     
127     public void writeFiles(String content) throws IOException {
128         FileOutputStream fos = openFileOutput("a.txt", MODE_PRIVATE);
129         fos.write(content.getBytes());
130         fos.close();
131     }
132     
133     public String readFiles() throws IOException {
134         String content = null;
135         FileInputStream fis = openFileInput("a.txt");
136         ByteArrayOutputStream baos = new ByteArrayOutputStream();
137         byte[] buffer = new byte[1024];
138         int len = 0;
139         while((len = fis.read(buffer)) != -1) {
140             baos.write(buffer,0,len);
141         }
142         content = baos.toString();
143         fis.close();
144         baos.close();
145         return content;    
146     }
147 }

xml布局中有一个EditText,一个Button和一个TextView。自己在看教程之前先写了一个,失败。下面是教程中给出的方法。中间因为搞错了组件id,浪费了很多时间。

Android中的文件存储练习

标签:android   style   blog   ar   io   color   os   sp   java   

原文地址:http://www.cnblogs.com/qingzhebaiya/p/4166951.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!