采用openFileOutput获取输出流

 1 package com.example.login.service;
 2 
 3 import java.io.BufferedReader;
 4 import java.io.File;
 5 import java.io.FileInputStream;
 6 import java.io.FileNotFoundException;
 7 import java.io.FileOutputStream;
 8 import java.io.InputStreamReader;
 9 import java.util.HashMap;
10 import java.util.Map;
11 
12 import android.annotation.SuppressLint;
13 import android.content.Context;
14 
15 /*
16  * mode 1私有、2可读、3可写、4可读写
17  */
18 public class LoginService {
19     public static boolean savaUserInfo(Context context, String username, String password,int mode)
20     {
21         try{
22             //File file = new File("/data/data/com.example.login/info.txt");
23             //File file = new File(context.getFilesDir(),"info.txt");
24             //FileOutputStream fos = new FileOutputStream(file);
25             //FileOutputStream fos = context.openFileOutput("private.txt", context.MODE_PRIVATE);
26             FileOutputStream fos = null;
27             switch(mode)
28             {
29             case 1:
30                 fos = context.openFileOutput("private.txt", context.MODE_PRIVATE);
31                 break;
32             case 2:
33                 fos = context.openFileOutput("readable.txt", context.MODE_WORLD_READABLE);
34                 break;
35             case 3:
36                 fos = context.openFileOutput("writeable.txt", context.MODE_WORLD_WRITEABLE);
37                 break;
38             case 4:
39                 fos = context.openFileOutput("public.txt", context.MODE_WORLD_READABLE + context.MODE_WORLD_WRITEABLE);
40                 break;
41             }
42             fos.write((username + "##" + password).getBytes());
43             fos.close();
44             return true;
45         }catch(Exception e){
46             e.printStackTrace();
47             return false;
48         }
49     }
50 
51 }

 

 

 

2、关于在另外项目测试文件的读写权限

 1 package com.example.other;
 2 
 3 import java.io.BufferedReader;
 4 import java.io.File;
 5 import java.io.FileInputStream;
 6 import java.io.FileOutputStream;
 7 import java.io.InputStreamReader;
 8 
 9 import android.os.Bundle;
10 import android.app.Activity;
11 import android.view.Menu;
12 import android.view.View;
13 import android.widget.Toast;
14 
15 public class MainActivity extends Activity {
16 
17     @Override
18     protected void onCreate(Bundle savedInstanceState) {
19         super.onCreate(savedInstanceState);
20         setContentView(R.layout.activity_main);
21     }
22     
23     public void readPrivate(View view)
24     {
25         try
26         {
27             File file = new File("/data/data/com.example.login/files/private.txt");
28             FileInputStream fis = new FileInputStream(file);
29             BufferedReader br = new BufferedReader(new InputStreamReader(fis));
30             String result = br.readLine();
31             Toast.makeText(this, result, 0).show();
32         }catch(Exception e){
33             e.printStackTrace();
34             Toast.makeText(this, "读取私有文件失败!", 0).show();
35         }
36     }
37     
38     public void writePrivate(View view)
39     {
40         try
41         {
42             File file = new File("/data/data/com.example.login/files/private.txt");
43             FileOutputStream fos = new FileOutputStream(file);
44             fos.write("aaa".getBytes());
45             fos.close();
46         }catch(Exception e){
47             e.printStackTrace();
48             Toast.makeText(this, "写入私有文件失败!", 0).show();
49         }
50     }
51     
52     public void readPublic(View view)
53     {
54         try
55         {
56             File file = new File("/data/data/com.example.login/files/public.txt");
57             FileInputStream fis = new FileInputStream(file);
58             BufferedReader br = new BufferedReader(new InputStreamReader(fis));
59             String result = br.readLine();
60             Toast.makeText(this, result, 0).show();
61         }catch(Exception e){
62             e.printStackTrace();
63             Toast.makeText(this, "读取私有文件失败!", 0).show();
64         }
65     }
66     
67     public void writePublic(View view)
68     {
69         try
70         {
71             File file = new File("/data/data/com.example.login/files/public.txt");
72             FileOutputStream fos = new FileOutputStream(file);
73             fos.write("aaa##bbb".getBytes());
74             fos.close();
75         }catch(Exception e){
76             e.printStackTrace();
77             Toast.makeText(this, "写入私有文件失败!", 0).show();
78         }
79     }
80 
81     @Override
82     public boolean onCreateOptionsMenu(Menu menu) {
83         // Inflate the menu; this adds items to the action bar if it is present.
84         getMenuInflater().inflate(R.menu.main, menu);
85         return true;
86     }
87 
88 }

 

posted @ 2016-04-13 11:28  zhongyinghe  阅读(452)  评论(0)    收藏  举报