字节流
读取文件内容
public static String readFileByStream(String filePath) {
try (FileInputStream fileInputStream = new FileInputStream(filePath);
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream)) {
return IOUtils.toString(bufferedInputStream, StandardCharsets.UTF_8);
} catch (IOException e) {
log.error("Failed to read file");
}
return null;
}
写入文件(FileOutputStream无缓冲区)
public static void writeFileByFileOutputStream(String filePath, String context) {
try (FileOutputStream fileOutputStream = new FileOutputStream(filePath)) {
//无缓冲区
byte[] bytes = context.getBytes(StandardCharsets.UTF_8);
int i = 0;
while (i < bytes.length - 1024) {
fileOutputStream.write(Arrays.copyOfRange(bytes, i, i + 1024));
i += 1024;
}
fileOutputStream.write(Arrays.copyOfRange(bytes, i, bytes.length));
fileOutputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
写入文件(BufferedOutputStream有缓冲区)
public static void writeFileByBufferedOutputStream(String filePath, String context) {
try (FileOutputStream fileOutputStream = new FileOutputStream(filePath);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream)) {
//有缓冲区
byte[] bytes = context.getBytes(StandardCharsets.UTF_8);
int i = 0;
while (i < bytes.length - 1024) {
fileOutputStream.write(Arrays.copyOfRange(bytes, i, i + 1024));
i += 1024;
}
bufferedOutputStream.write(Arrays.copyOfRange(bytes, i, bytes.length));
bufferedOutputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
测试
@Test
public void test_file_stream() {
URL dataFileUrl = this.getClass().getClassLoader().getResource("files");
String data = StreamUtil.readFileByStream(dataFileUrl.getPath() + File.separator + "data.txt");
System.out.println(data);
StreamUtil.writeFileByFileOutputStream(dataFileUrl.getPath() + File.separator + "data1.txt", data);
StreamUtil.writeFileByBufferedOutputStream(dataFileUrl.getPath() + File.separator + "data2.txt", data);
}
字符流
读取文件(BufferedReader)
public static String readFileByFileReader(String filePath) {
try (FileReader fileReader = new FileReader(filePath);
BufferedReader bufferedReader = new BufferedReader(fileReader)) {
return bufferedReader.lines().collect(Collectors.joining("\n"));
} catch (IOException e) {
log.error("Failed to read file");
}
return null;
}
逐行读取文件
public static String readFileByFileReaderEachLine(String filePath) {
try (FileReader fileReader = new FileReader(filePath);
BufferedReader bufferedReader = new BufferedReader(fileReader)) {
int len = 0;
StringBuffer sb = new StringBuffer();
while ((len = bufferedReader.read()) != -1) {
sb.append(bufferedReader.readLine());
sb.append("\n");
}
return sb.toString();
} catch (IOException e) {
log.error("Failed to read file");
}
return null;
}
写入文件(BufferedWriter)
public static void writeFileByFileWriter(String filePath, String context) {
try (FileOutputStream stream = new FileOutputStream(filePath);
OutputStreamWriter writer = new OutputStreamWriter(stream, StandardCharsets.UTF_8);
BufferedWriter bufferedWriter = new BufferedWriter(writer)) {
bufferedWriter.write(context);
bufferedWriter.newLine();//另起一行
bufferedWriter.write("success");
bufferedWriter.flush();//写入文件
} catch (IOException e) {
log.error("Failed to write into file", e);
}
}
测试
@Test
public void test_file_reader() {
URL dataFileUrl = this.getClass().getClassLoader().getResource("files");
String data = StreamUtil.readFileByFileReader(dataFileUrl.getPath() + File.separator + "data.txt");
System.out.println(data);
StreamUtil.writeFileByFileWriter(dataFileUrl.getPath() + File.separator + "data3.txt", data);
}