(1)一般的java代码
@Test
public void whenConvertingToFile_thenCorrect()
throws IOException {
InputStream initialStream = new FileInputStream(
new File("src/main/resources/sample.txt"));
byte[] buffer = new byte[initialStream.available()];
initialStream.read(buffer);
File targetFile = new File("src/main/resources/targetFile.tmp");
OutputStream outStream = new FileOutputStream(targetFile);
outStream.write(buffer);
}
(2)java8中操作
@Test
public void whenConvertingAnInProgressInputStreamToFile_thenCorrect2()
throws IOException {
InputStream initialStream = new FileInputStream(
new File("src/main/resources/sample.txt"));
File targetFile = new File("src/main/resources/targetFile.tmp");
java.nio.file.Files.copy(
initialStream,
targetFile.toPath(),
StandardCopyOption.REPLACE_EXISTING);
IOUtils.closeQuietly(initialStre
java实现输入流写入文件
最新推荐文章于 2024-05-05 20:29:22 发布