码迷,mamicode.com
首页 > 编程语言 > 详细

java读写文件(可当工具类保存。解决乱码)

时间:2014-07-16 17:15:53      阅读:318      评论:0      收藏:0      [点我收藏+]

标签:java读写文件

//读文件
public static String ReadFile(String path) {
File file = new File(path);
BufferedReader reader = null;
String laststr = "";
try {
reader = new BufferedReader(new FileReader(file));
String tempString = null;
while ((tempString = reader.readLine()) != null) {
laststr += tempString;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
return laststr;
}

//写文件
public static void writeFile(String path,String fileContent) {
try {
File filename=new File(path);
OutputStreamWriter write = new OutputStreamWriter(
new FileOutputStream(filename), "UTF-8");
BufferedWriter writer = new BufferedWriter(write);
writer.write(fileContent);
writer.close();
} catch (Exception e) {
System.out.println("写文件内容操作出错");
e.printStackTrace();
}
}

java读写文件(可当工具类保存。解决乱码),布布扣,bubuko.com

java读写文件(可当工具类保存。解决乱码)

标签:java读写文件

原文地址:http://blog.csdn.net/maskdfe/article/details/37821975

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!