下面的代码是表示存储信息与读取信息。
public static boolean saveInfo(String text, String text2) {
String msg=text+"#"+text2;
File file=new File("data/data/com.example.day03_pratice/info.txt");
try {
FileOutputStream fos=new FileOutputStream(file);
fos.write(msg.getBytes());
fos.close();
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
public static String readInfo() {
File file=new File("data/data/com.example.day03_pratice/info.txt");
try {
BufferedReader br=new BufferedReader(new FileReader(file));
return br.readLine();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
之所以放在文件data/data/com.example.day03_pratice中,是因为这个文件夹是私有的文件夹,其它项目一般无法访问此项目,可以保证数据的隐密性。
使用Context获得常见目录,而不是把目录写死(容易出问题),可以理解为大工具类,可以从这里获得全局信息(系统资源),也可以访问当前私有资源和类,也可以做系统级的调用,比如开启另外一个activity 发广播 开启服务。
File getFilesDir(); 获得的目录是data/data/包名/files/ 目录对应的文件
FileOutputStream openFileOutput("info.txt", ); 可以获得输出流 ,操作getFilesDir()对应目录下的文件。
FileInputStream openFileInput("info.txt", ); 可以获得输入流 ,操作getFilesDir()对应目录下的文件。
注意:context.getFilesDir().getAbsolutePath() 后面若要跟文件则要加“/”
而若是直接写context.openFilesDir("info2.txt" , ...) ; 这里的文件不可以加“/”
public static boolean saveInfo(Context context,String text, String text2) {
String msg=text+"#"+text2;
File file=new File(context.getFilesDir().getAbsolutePath()+"/info2.txt");
try {
FileOutputStream fos=context.openFileOutput("info2.txt", Context.MODE_WORLD_WRITEABLE);
fos.write(msg.getBytes());
fos.close();
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
public static String readInfo(Context context) {
File file=new File(context.getFilesDir().getAbsolutePath()+"/info2.txt");
try {
BufferedReader br=new BufferedReader(new FileReader(file));
return br.readLine();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
若是想把信息储存在SD卡上,则需要获取权限,在 AndroidManifest.xml 中,在application标签外加上
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
public static boolean saveInfo(String text, String text2) {
String msg=text+"#"+text2;
try {
FileOutputStream fos=new FileOutputStream("mnt/sdcard/info2.txt");
fos.write(msg.getBytes());
fos.close();
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
public static String readInfo() {
File file=new File("mnt/sdcard/info2.txt");
try {
BufferedReader br=new BufferedReader(new FileReader(file));
return br.readLine();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
sdcard的位置很多情况下都不固定,直接写死路径也是万万不行的,可以先获得当前项目外部存储设备的路径。
private static String sdcard=Environment.getExternalStorageDirectory().getAbsolutePath();
public static boolean saveInfo(String text, String text2) {
String msg=text+"#"+text2;
try {
FileOutputStream fos=new FileOutputStream(sdcard+"/info2.txt");
fos.write(msg.getBytes());
fos.close();
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
public static String readInfo() {
File file=new File(sdcard+"/info2.txt");
try {
BufferedReader br=new BufferedReader(new FileReader(file));
return br.readLine();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
判断是否有sd卡插入,若有则在sd卡上保存数据,若无,则在files中存储数据。
Environment.getExternalStorageState() 等于 Environment.MEDIA_MOUNTED则插了sd卡,否则不存在sd卡。
/*
* 将一个String文件化
*/
public class Utils {
private static boolean storeInSd=false;//标记是否存入sd卡
private static String sdcard=Environment.getExternalStorageDirectory().getAbsolutePath();//获取sd卡的路径
public static boolean saveInfo(Context context,String text, String text2) {
String msg=text+"#"+text2;
try {
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
storeInSd=true;
FileOutputStream fos;
if(storeInSd)
{
fos=new FileOutputStream(sdcard+"/info2.txt");
}
else{
fos=context.openFileOutput("info2.txt", Context.MODE_WORLD_WRITEABLE);
}
fos.write(msg.getBytes());
fos.close();
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
public static String readInfo(Context context) {
File file;
if(storeInSd)
{
file=new File(sdcard+"/info2.txt");
}
else{
file=new File(context.getFilesDir().getAbsolutePath()+"/info2.txt");
}
try {
BufferedReader br=new BufferedReader(new FileReader(file));
return br.readLine();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
一共十位,第一位表示是否为目录, 接下来三位表示当前用户是否有权限(r,w,x), 同组用户是否有权限,其它用户是否有权限。
r (读) w(写) x(execute 可执行)
当前用户表示同一个项目中的其它方法,其它用户表示其它项目中的方法访问此文件。
修改文件权限:
adb shell
cd 切换到待修改文件所在路径
chmod命令可以修改权限 每三位合成一个数 比如chmod 777 info2.txt 便是把所有权限都赋给info2.txt
context.openFileOutput("file.txt", mode);
mode:1.PRIVATE(660) 2.WORLD_READABLE(664) 3.WORLD_WRITEABLE(662) 4.APPEND(660)
也就是说,WORLD_READABLE其它用户也可以读取此文件,WORLD_WRITEABLE其它用户也可以写这个文件,所以过时了。
APPEND在文件后追加,PRIVATE 再次写会覆盖掉,只有同组访问。