大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺
以下是一些将InputStream
转换为File
Java示例
- 手动将
InputStream
复制到FileOutputStream
- Apache Commons IO –
FileUtils.copyInputStreamToFile
- Java 1.7 NIO
Files.copy
1. FileOutputStream
1.1我们必须将数据从InputStream
手动复制到OutputStream
。
package com.mkyong;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
public class InputStreamToFile {
private static final String FILE_TO = "d:\\download\\google.txt";
public static void main(String[] args) throws IOException {
URI u = URI.create("https://www.google.com/");
try (InputStream inputStream = u.toURL().openStream()) {
File file = new File(FILE_TO);
copyInputStreamToFile(inputStream, file);
}
}
// InputStream -> File
private static void copyInputStreamToFile(InputStream inputStream, File file)
throws IOException {
try (FileOutputStream outputStream = new FileOutputStream(file)) {
int read;
byte[] bytes = new byte[1024];
while ((read = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
// commons-io
//IOUtils.copy(inputStream, outputStream);
}
}
}
2. Apache Commons IO
2.1 FileUtils.copyInputStreamToFile
在Apache Commons IO中可用
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
package com.mkyong;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
public class InputStreamToFile2 {
private static final String FILE_TO = "d:\\download\\google.txt";
public static void main(String[] args) throws IOException {
URI u = URI.create("https://www.google.com/");
try (InputStream inputStream = u.toURL().openStream()) {
File file = new File(FILE_TO);
// commons-io
FileUtils.copyInputStreamToFile(inputStream, file);
}
}
}
3. Java 1.7 NIO
3.1如果只想将inputStream
保存到某个文件中,请尝试使用Java 1.7 NIO Files.copy
package com.mkyong;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Paths;
public class InputStreamToFile3 {
private static final String FILE_TO = "d:\\download\\google.txt";
public static void main(String[] args) throws IOException {
URI u = URI.create("https://www.google.com/");
try (InputStream inputStream = u.toURL().openStream()) {
//Java 1.7
Files.copy(inputStream, Paths.get(FILE_TO));
}
}
}
4.旧时光
4.1在过去的Java 1.7之前,我们必须手动关闭所有资源。
package com.mkyong;
import java.io.*;
public class InputStreamToFile4 {
public static void main(String[] args) {
InputStream inputStream = null;
OutputStream outputStream = null;
try {
// read this file into InputStream
inputStream = new FileInputStream("/Users/mkyong/holder.js");
// write the inputStream to a FileOutputStream
outputStream = new FileOutputStream(new File("/Users/mkyong/holder-new.js"));
int read = 0;
byte[] bytes = new byte[1024];
while ((read = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
System.out.println("Done!");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
5.将文件转换为InputStream
这很容易:
File file = new File("d:\\download\\google.txt");
InputStream inputStream = new FileInputStream(file);
注意
您可能对此String的InputStream感兴趣
参考文献
翻译自: https://mkyong.com/java/how-to-convert-inputstream-to-file-in-java/
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/190970.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...