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();
	}
}
posted @ 2025-07-25 14:29  年少轻狂请多指教  阅读(21)  评论(0)    收藏  举报