Java对于文件的读取或是写入一般都是使用stream流的方式,例如FileInputStream
或是FileOutputStream
。方式有很多种,每种方式都有自己的应用场景。
文件读取
一般情况下,我们使用Java读取文件内容使用的是以下方式:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| public String readFromFile(File file) { try (FileReader reader = new FileReader(file)) { char[] b = new char[1024]; StringBuilder sb = new StringBuilder(); int length; while ((length = reader.read(b)) > 0) { sb.append(b, 0, length); } return sb.toString(); } catch (IOException e) { e.printStackTrace(); } return null; }
|
这种方式对于编码处理会有一些问题,使用的是FileReader
的默认编码格式,具体是什么格式我还没研究过,之前我处理UTF-8的文件时出现了乱码的问题。后来我去查了下,要限定编码格式,可以通过以下方式:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| public String readFromFile(File file) { try (BufferedReader reader = new BufferedReader( new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8))) { char[] b = new char[1024]; StringBuilder sb = new StringBuilder(); int length; while ((length = reader.read(b)) > 0) { sb.append(b, 0, length); } return sb.toString(); } catch (IOException e) { e.printStackTrace(); } return null; }
|
这里其实就只是把FileReader
换成了BufferedReader
,不过核心是InputStreamReader
,因为其可以设定读取文件的InputStream
编码格式。
文件写入
文件写入也可以使用配套的FileWrite
:
1 2 3 4 5 6
| public void writeToFile(String content, File target) throws IOException { try (FileWriter writer = new FileWriter(target)) { writer.write(content); writer.flush(); } }
|
同样的,FileWriter
也是使用的默认的文件编码,可能会让中文字符变成乱码。这里也可以使用
1 2 3 4 5 6 7 8
| public void writeToFile(String content, File target) throws IOException { try (BufferedWriter writer = new BufferedWriter( new OutputStreamWriter(new FileOutputStream(target), StandardCharsets.UTF_8))) { writer.write(content); writer.flush(); } }
|
不过在写入文件时,如果文件已存在,那么Writer
会根据文件编码来自动匹配编码。