Excel文件成功移动到指定文件夹中
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class ExcelFileMoveExample {
public static void main(String[] args) {
String sourceFilePath = "path/to/excel.xlsx"; // Excel文件的源路径
String destinationFolderPath = "path/to/destination/folder"; // 目标文件夹的路径
try {
File sourceFile = new File(sourceFilePath);
File destinationFolder = new File(destinationFolderPath);
// 检查目标文件夹是否存在,如果不存在则创建
if (!destinationFolder.exists()) {
destinationFolder.mkdirs();
}
// 创建目标文件的路径
String destinationFilePath = destinationFolderPath + File.separator + sourceFile.getName();
// 将源文件复制到目标文件夹中
copyFile(sourceFile, new File(destinationFilePath));
System.out.println("Excel文件成功移动到指定文件夹中!");
} catch (IOException e) {
e.printStackTrace();
}
}
private static void copyFile(File sourceFile, File destinationFile) throws IOException {
FileInputStream inputStream = new FileInputStream(sourceFile);
FileOutputStream outputStream = new FileOutputStream(destinationFile);
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}
inputStream.close();
outputStream.close();
}
}
本文来自博客园,作者:年少轻狂请多指教,转载请注明原文链接:https://www.cnblogs.com/HAILAER/p/19004562
浙公网安备 33010602011771号