java 将目录下文件写到另一个目录中
时间: 2024-05-08 14:19:40 浏览: 10
这个问题是技术问题,我可以回答。可以使用Java中的IO流,将目录下的文件读入内存,然后再将其写入到另一个目录中。使用FileInputStream和FileOutputStream可以实现文件的读写操作。代码如下:
```java
public class CopyFiles {
public static void main(String[] args) {
File srcFolder = new File("D:\\src");
File destFolder = new File("D:\\dest");
if (!destFolder.exists()) {
destFolder.mkdirs();
}
try {
File[] files = srcFolder.listFiles();
for (File file : files) {
FileInputStream fis = new FileInputStream(file);
String destFilePath = destFolder.getAbsolutePath() + "\\" + file.getName();
FileOutputStream fos = new FileOutputStream(destFilePath);
byte[] buffer = new byte[1024];
int len;
while ((len = fis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fis.close();
fos.close();
}
System.out.println("Files copied successfully!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
```