aws可以通过java代码实现把文件夹复制到另外一个文件夹吗?(java代码)
时间: 2024-02-03 07:02:48 浏览: 137
C#实现复制文件夹中文件到另一个文件夹的方法
5星 · 资源好评率100%
是的,AWS可以通过Java代码实现将一个文件夹中的内容复制到另一个文件夹中。下面是一个Java示例代码:
```
import java.io.*;
public class CopyFolder {
public static void main(String[] args)
{
File srcFolder = new File("C:\\Users\\test\\Desktop\\folder1");
File destFolder = new File("C:\\Users\\test\\Desktop\\folder2");
//make sure source folder exists
if(!srcFolder.exists()){
System.out.println("Source folder does not exist.");
return;
}else{
try{
copyFolder(srcFolder,destFolder);
}catch(IOException e){
e.printStackTrace();
//error, just exit
System.exit(0);
}
}
System.out.println("Done");
}
public static void copyFolder(File src, File dest)
throws IOException{
if(src.isDirectory()){
//if directory not exists, create it
if(!dest.exists()){
dest.mkdir();
System.out.println("Directory copied from "
+ src + " to " + dest);
}
//list all the directory contents
String[] files = src.list();
for (String file : files) {
//construct the src and dest file structure
File srcFile = new File(src, file);
File destFile = new File(dest, file);
//recursive copy
copyFolder(srcFile,destFile);
}
}else{
//if file, then copy it
//Use bytes stream to support all file types
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dest);
byte[] buffer = new byte[1024];
int length;
//copy the file content in bytes
while ((length = in.read(buffer)) > 0){
out.write(buffer, 0, length);
}
in.close();
out.close();
System.out.println("File copied from " + src + " to " + dest);
}
}
}
```
您可以将`srcFolder`和`destFolder`的路径更改为您要复制的文件夹路径。程式会将源文件夹中的所有内容复制到目的文件夹中。
阅读全文