java复制文件到指定目录_java复制文件到指定文件夹

/**

* 将InputStream写入本地文件

* 输入流

*

* @throws IOException

*/

private void writeToLocal(String fileName, InputStream input)

throws IOException {

String destDirName = "E:/temp1";

createDir(destDirName);

String dirFile = destDirName + "/" + fileName;

int index;

byte[] bytes = new byte[1024];

FileOutputStream downloadFile = new FileOutputStream(dirFile);

while ((index = input.read(bytes)) != -1) {

downloadFile.write(bytes, 0, index);

downloadFile.flush();

}

downloadFile.close();

input.close();

}

/**

* 创建文件夹

* @param destDirName

* @return

*/

public boolean createDir(String destDirName) {

File dir = new File(destDirName);

if (dir.exists()) {

System.out.println("创建目录" + destDirName + "失败,目标目录已经存在");

return false;

}

if (!destDirName.endsWith(File.separator)) {

destDirName = destDirName + File.separator;

}

//创建目录

if (dir.mkdirs()) {

System.out.println("创建目录" + destDirName + "成功!");

return true;

} else {

System.out.println("创建目录" + destDirName + "失败!");

return false;

}

}

/**

* 截取图片后缀名

*/

public String getSuffixal(String name) {

String str = name.substring(name.length() - 4, name.length());

return str;

}

//测试代码

File file = new File("e:/QQ截图20170503093317.png");

FileInputStream fis = new FileInputStream(file);

int random = new Random().nextInt(100);

writeToLocal(random+".png", fis);

System.out.print("成功");

2152b61a6ae1

IO流读取.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值