gpt4 book ai didi

java - 无法将 csv 文件从一个目录移动到另一目录

转载 作者:行者123 更新时间:2023-12-01 12:26:47 25 4
gpt4 key购买 nike

我正在尝试在读取 csv 文件的内容并将其存储在数据库中后对其进行存档。我正在这样做

String filename = file.getName();
File destiantion = new File(getAttribute(ST_ARCHIVE_FOLDER) + "/"+ filename);
boolean fileArchived = file.renameTo(destiantion);

其中文件=“D:\Users\400220260.INDCORP\Desktop\ParameterMargin20141009.csv”
和目的地 =“D:\Users\400220260.INDCORP\Desktop\Archive\ParameterMargin20141009.csv”

请帮忙提前致谢

最佳答案

您拥有 File 对象,但现在您必须将旧文件复制到新位置。仅对文件进行重命名并不能达到您想要的效果。这可以这样完成:

public static void copyFile(File sourceFile, File destFile) throws IOException {
destFile.createNewFile();
FileChannel source = null;
FileChannel destination = null;

try {
source = new FileInputStream(sourceFile).getChannel();
destination = new FileOutputStream(destFile).getChannel();
destination.transferFrom(source, 0, source.size());
}
finally {
if(source != null) {
source.close();
}
if(destination != null) {
destination.close();
}
}
}

使用 Apache Commons IO 中 FileUtils 的 copyFile 方法会更简单,但因此您必须在项目中添加该库:

FileUtils.copyFile(File srcFile, File destFile)

关于java - 无法将 csv 文件从一个目录移动到另一目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26272100/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com