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

android .txt文件的写入,读取,还有复制图片

时间:2014-10-20 19:09:33      阅读:178      评论:0      收藏:0      [点我收藏+]

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

txt文件的写入:

 1 private void save() {
 2         FileOutputStream fos = null;
 3         String state = Environment.getExternalStorageState();// sd状态
 4         if (state.equals(Environment.MEDIA_MOUNTED)) {// 判断sd卡是否可用
 5             File root = Environment.getExternalStorageDirectory();
 6             File targeDir = new File(root, super.getPackageName());// 要把文件写相应在activity下
 7 
 8             if (!targeDir.exists()) {// 判断目录是否存在
 9                 targeDir.mkdir();// 创建
10             }
11             // String newFile=file.toString()+"/"+"user.txt";
12             // IO操作
13             //
14             try {
15                 // 括号里是路径
16                 fos = new FileOutputStream(new File(targeDir, "userinfo.txt"));// 输出流
17                 String content = name;
18                 /*
19                  * for(int i=0;i<content.length();i++){
20                  * fos.write(content.charAt(i));//一个个字节输 }
21                  */
22                 byte[] data = content.getBytes();
23                 fos.write(data);
24 
25             } catch (FileNotFoundException e) {
26                 // TODO Auto-generated catch block
27                 e.printStackTrace();
28             } catch (IOException e) {
29                 // 输入输出都会引发IO异常
30                 e.printStackTrace();
31             } finally {
32                 // 打开流就需要关闭
33                 try {
34                     fos.close();
35                 } catch (IOException e) {
36                     // TODO Auto-generated catch block
37                     e.printStackTrace();
38                 }
39             }
40 
41         }
42     }

txt文件的读取:

 1 private void read() {
 2         FileInputStream fis = null;
 3         String state = Environment.getExternalStorageState();
 4         if (state.equals(Environment.MEDIA_MOUNTED)) {
 5             File root = Environment.getExternalStorageDirectory();
 6             File file = new File(root, super.getPackageName() + "/userinfo.txt");
 7             if (file.exists()) {
 8                 try {
 9                     fis = new FileInputStream(file);
10                     int len = 0;
11                     StringBuilder builder = new StringBuilder();
12                     byte[] buffer = new byte[1024];
13 
14                     while ((len = fis.read(buffer)) != -1) {
15                         builder.append(new String(buffer, 0, len));
16                         // builder.append(buffer);//乱码
17                     }
18                     /*
19                      * while((len=fis.read())!=-1){ builder.append((char)len); }
20                      */
21                     Toast.makeText(getApplicationContext(), builder.toString(),
22                             Toast.LENGTH_SHORT).show();
23                     text2.setText(builder.toString());
24 
25                 } catch (FileNotFoundException e) {
26                     // TODO Auto-generated catch block
27                     e.printStackTrace();
28                     Log.i("read", e.getMessage());
29                 } catch (IOException e) {
30                     // TODO Auto-generated catch block
31                     e.printStackTrace();
32                 } finally {
33                     try {
34                         fis.close();
35                     } catch (IOException e) {
36                         // TODO Auto-generated catch block
37                         e.printStackTrace();
38                     }
39                 }
40             }
41         }
42     }

图片的复制:

先在assets文件夹下建一个image文件夹,里面放一张图片

 1 private void copy() {
 2         FileOutputStream output = null;
 3         InputStream source = null;
 4         String state = Environment.getExternalStorageState();
 5         if (state.equals(Environment.MEDIA_MOUNTED)) {
 6             // inputstream=super.getResources().openRawResource(R.drawable.ic_launcher);
 7 
 8             try {
 9                 AssetManager manager = super.getAssets();
10                 source = manager.open("image/ic_launcher.png");
11                 File target = new File(
12                         Environment.getExternalStorageDirectory() + "/"
13                                 + super.getPackageName() + "/mycopyimg.png");
14                 output = new FileOutputStream(target);
15 
16                 int len = 0;
17                 byte[] buffer = new byte[1024];
18                 while ((len = source.read(buffer)) != -1) {
19                     output.write(buffer, 0, len);
20                 }
21             } catch (IOException e) {
22                 // TODO Auto-generated catch block
23                 e.printStackTrace();
24                 Log.i("111", e.getMessage());
25             } finally {
26                 try {
27                     source.close();
28                     output.close();
29                 } catch (IOException e) {
30                     // TODO Auto-generated catch block
31                     e.printStackTrace();
32                 }
33             }
34         }
35     }

 

android .txt文件的写入,读取,还有复制图片

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

原文地址:http://www.cnblogs.com/jiuqing/p/4038292.html

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