文件写入操作:
private void write(String path,String value){
FileOutputStream fos = null;
try {
File file = new File(path);
if(!file.exists()){
file.createNewFile();
file.setReadable(true, false);
}
fos = new FileOutputStream(file);
fos.write(value.getBytes());
} catch (Exception e) {
e.printStackTrace();
} finally {
if(fos!=null){
try{
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
文件读取操作
public String readFile(File file) {
if (file == null || !file.exists() || !file.canRead()) {
return null;
}
final StringBuffer sb = new StringBuffer();
BufferedReader reader = null;
String temp = "";
try {
reader = new BufferedReader(new FileReader(file));
while ((temp = reader.readLine()) != null) {
sb.append(temp);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
return sb.toString();
}