String FileContent = ""; // 文件很长的话建议使用StringBuffer
try {
FileInputStream fis = new FileInputStream("d:\\input.txt");
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null) {
FileContent += line;
FileContent += "\r\n"; // 补上换行符
}
} catch (Exception e) {
e.printStackTrace();
}
**************************************读取*******************
String FileContent = "文件内容";
try {
FileOutputStream fos = new FileOutputStream("d:\\output.txt");
OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
osw.write(FileContent);
osw.flush();
} catch (Exception e) {
e.printStackTrace();
}
************************************写入***********************
本文提供了一个使用Java进行文件读写的示例代码,包括如何读取文本文件内容到字符串变量,并将字符串变量的内容写入到另一个文件中,采用的是标准的UTF-8编码。

被折叠的 条评论
为什么被折叠?



