方便以后直接使用
public void writeFileData(String fileName,String message){
try{
FileOutputStream fout =openFileOutput(fileName, MODE_PRIVATE);
byte [] bytes = message.getBytes();
fout.write(bytes);
fout.close();
}
catch(Exception e){
e.printStackTrace();
}
}
public String readFileData(String fileName){
String res="always on = 0";
String FILESPATH = getFilesDir().getPath() + "//";
Log.d(TAG, "readFileData FILESPATH = " + FILESPATH);
File alwaystxt = new File(FILESPATH + fileName);
if(alwaystxt.exists()){
try{
FileInputStream fin = openFileInput(fileName);
int length = fin.available();
byte [] buffer = new byte[length];
fin.read(buffer);
res = EncodingUtils.getString(buffer, "UTF-8");
fin.close();
}
catch(Exception e){
e.printStackTrace();
}
Log.d(TAG, "readFileData res = " + res);
}
else{
Log.d(TAG, "readFileData file is not exists ");
}
return res;
}
write是保存一个带数字的字符串
read 是得到保存的字符串里面数字的值是多少
String fileName = "always.txt";
String filepath = "always.txt";
String fileStrAlwaysOn_false = "always on = 0";
String fileStrAlwaysOn_true = "always on = 1";
writeFileData(fileName,fileStrAlwaysOn_true);
getNumbers(readFileData(fileName));
public int getNumbers(String content) {
Pattern pattern = Pattern.compile("\\d+");
Matcher matcher = pattern.matcher(content);
while (matcher.find()) {
Log.d(TAG, "getNumbers = " + matcher.group(0));
String Number = matcher.group(0);
return Integer.parseInt(Number);
}
return 0;
}