java.io输入流怎么转成输出流

如题所述

第1个回答  2017-04-07
/**
* 文件输入流转文件输出
*
* @param inputStream
* 文件输入流
* @param outputFileName
* 文件输出路径
* @return 返回值
* @throws IOException
* 异常
*/
public static boolean createFileByInputStream(InputStream inputStream, String outputFileName) throws IOException {
// 创建文件夹
createDirs(outputFileName);
OutputStream out = new FileOutputStream(outputFileName) ;
// 判断输入或输出是否准备好
if (inputStream != null && out != null) {
int temp = 0;
// 开始拷贝
while ((temp = inputStream.read()) != -1) {
// 边读边写
out.write(temp);
}
// 关闭输入输出流
inputStream.close();
out.close();
return true;
} else {
return false;
}
}

/**
* 创建文件夹
*
* @param targetZipFilePath
* 目标文件夹路径
*/
private static void createDirs(String targetFilePath) {
// 将路径中的/或者\替换成\\
String path = Pattern.compile("[\\/]").matcher(targetFilePath).replaceAll(File.separator);
int endIndex = path.lastIndexOf(File.separator);
path = path.substring(0, endIndex);
File f = new File(path);
f.mkdirs();
}本回答被网友采纳

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网