文件讀寫,字符串持久化
public String readFile2String(String fileName) {
StringBuffer sb = new StringBuffer();
try {
FileInputStream fin = new FileInputStream(fileName);
byte[] buffer = new byte[1024*4];
int len = 0 ;
while((len=fin.read(buffer))!=-1){
sb.append(EncodingUtils.getString(buffer, 0, len,"UTF-8"));
}
fin.close();
} catch (Exception e) {
e.printStackTrace();
}
return sb.toString();
}
private void writeString2File(String jsondata , String filename) {
try {
FileOutputStream fos = new FileOutputStream(filename);
fos.write(jsondata.getBytes("UTF-8"));
fos.close();
} catch (Exception e) {
e.printStackTrace();
Log.e("",
"file e :" + e.toString());
}
}
StringBuffer sb = new StringBuffer();
try {
FileInputStream fin = new FileInputStream(fileName);
byte[] buffer = new byte[1024*4];
int len = 0 ;
while((len=fin.read(buffer))!=-1){
sb.append(EncodingUtils.getString(buffer, 0, len,"UTF-8"));
}
fin.close();
} catch (Exception e) {
e.printStackTrace();
}
return sb.toString();
}
private void writeString2File(String jsondata , String filename) {
try {
FileOutputStream fos = new FileOutputStream(filename);
fos.write(jsondata.getBytes("UTF-8"));
fos.close();
} catch (Exception e) {
e.printStackTrace();
Log.e("",
"file e :" + e.toString());
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.