JAVA 把指定的文件 复制到目标文件夹下 怎么写啊?

2024-12-28 11:30:40
推荐回答(2个)
回答1:

// 由绝对路径得到输出流
//path源路径
//fileCopeToPath 目标路径
String path="D:\我的文档\My Pictures\1.jpg ";
String fileCopeToPath=" D:\我的文档\test ";
File file =new File(path);
if(file.exists){
FileInputStream fis = new FileInputStream(file);
FileOutputStream fos= new FileOutputStream( fileCopeToPath+ File.separator +path.subString(path.lastIndexOf("/\"),path.length));
byte[] b = new byte[fis.available()];
int len = 0;
while ((len = fis.read(b)) != -1)
{
fos.write(b, 0, len);
fos.flush();
}
fos.close();
fis.close();
}

回答2:

try {
InputStream in = new FileInputStream(new File("D:\我的文档\My Pictures\1.jpg"));
OutputStream out = new FileOutputStream(new File("D:\我的文档\test\1.jpg"));
byte[] b = new byte[100];
while (in.read(b) != -1) {
out.write(b);
}
in.close();
out.flush();
out.close();

} catch (Exception e) {
e.printStackTrace();
}