package com.example.login.service; import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map; import android.annotation.SuppressLint;
import android.content.Context; /*
* mode 1私有、2可读、3可写、4可读写
*/
public class LoginService {
public static boolean savaUserInfo(Context context, String username, String password,int mode)
{
try{
//File file = new File("/data/data/com.example.login/info.txt");
//File file = new File(context.getFilesDir(),"info.txt");
//FileOutputStream fos = new FileOutputStream(file);
//FileOutputStream fos = context.openFileOutput("private.txt", context.MODE_PRIVATE);
FileOutputStream fos = null;
switch(mode)
{
case 1:
fos = context.openFileOutput("private.txt", context.MODE_PRIVATE);
break;
case 2:
fos = context.openFileOutput("readable.txt", context.MODE_WORLD_READABLE);
break;
case 3:
fos = context.openFileOutput("writeable.txt", context.MODE_WORLD_WRITEABLE);
break;
case 4:
fos = context.openFileOutput("public.txt", context.MODE_WORLD_READABLE + context.MODE_WORLD_WRITEABLE);
break;
}
fos.write((username + "##" + password).getBytes());
fos.close();
return true;
}catch(Exception e){
e.printStackTrace();
return false;
}
} }
2、关于在另外项目测试文件的读写权限
package com.example.other; import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader; import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Toast; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
} public void readPrivate(View view)
{
try
{
File file = new File("/data/data/com.example.login/files/private.txt");
FileInputStream fis = new FileInputStream(file);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String result = br.readLine();
Toast.makeText(this, result, 0).show();
}catch(Exception e){
e.printStackTrace();
Toast.makeText(this, "读取私有文件失败!", 0).show();
}
} public void writePrivate(View view)
{
try
{
File file = new File("/data/data/com.example.login/files/private.txt");
FileOutputStream fos = new FileOutputStream(file);
fos.write("aaa".getBytes());
fos.close();
}catch(Exception e){
e.printStackTrace();
Toast.makeText(this, "写入私有文件失败!", 0).show();
}
} public void readPublic(View view)
{
try
{
File file = new File("/data/data/com.example.login/files/public.txt");
FileInputStream fis = new FileInputStream(file);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String result = br.readLine();
Toast.makeText(this, result, 0).show();
}catch(Exception e){
e.printStackTrace();
Toast.makeText(this, "读取私有文件失败!", 0).show();
}
} public void writePublic(View view)
{
try
{
File file = new File("/data/data/com.example.login/files/public.txt");
FileOutputStream fos = new FileOutputStream(file);
fos.write("aaa##bbb".getBytes());
fos.close();
}catch(Exception e){
e.printStackTrace();
Toast.makeText(this, "写入私有文件失败!", 0).show();
}
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} }