1、读取本地文件内容
public String readFileContent(String fileName) {
File file = new File(fileName);
BufferedReader reader = null;
StringBuffer sbf = new StringBuffer();
try {
reader = new BufferedReader(new FileReader(file));
String tempStr;
while ((tempStr = reader.readLine()) != null) {
sbf.append(tempStr);
}
reader.close();
return sbf.toString();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
return sbf.toString();
}
2、往本地写内容
public void write() {
File dir = new File("D:/test");
if (!dir.exists()) {
dir.mkdir();
}
File file=new File("D:/test/test.txt");
OutputStreamWriter osw = null;
if(file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
String str = userservice.findAllUser().toString();
osw=new OutputStreamWriter(new FileOutputStream(file));
osw.write(str);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
osw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}