@Test
public void test01(){
try {
copyFile(new File("D:\\线下"),new File("G:\\线下"));
System.out.println("成功");
} catch (IOException e) {
e.printStackTrace();
}
}
public void copyFile(File file,File fileNew) throws IOException {
fileNew.mkdirs();
if(file != null){
File[] files = file.listFiles();
if(files != null){
for (File file1 : files) {
if(file1.isFile()){
InputStream inputStream = new FileInputStream(file1);
OutputStream outputStream = new FileOutputStream(fileNew.getAbsolutePath()
+ "\\" + file1.getName());
byte [] bytes = new byte[1024*1024];
int len =0;
while((len = inputStream.read(bytes)) != -1){
outputStream.write(bytes,0,len);
}
inputStream.close();
outputStream.close();
}else {
copyFile(file1,new File(fileNew.getAbsolutePath()
+ "\\" + file1.getName()));
}
}
}
}
}
IO流的复制(文件夹)
最新推荐文章于 2022-07-24 21:16:28 发布