仅当大小、上次修改日期、文件名或文件内容在java中发生更改时才复制文件
我在不同的网络驱动器上有2个文件夹Q:和K: 每天我都会传递一个文件夹名,带或不带修订号,程序会在Q:drive中搜索该文件,如果没有修订号,它会将整个文件夹复制到K:drive的文件夹,如果没有修订号,它会在主文件夹中搜索该修订文件夹,并仅将该文件夹复制到K:drive的文件夹 现在我正在复制,没有比较两个文件夹中的文件大小或上次修改日期文件。在修改的情况下,如果未选中文件大小,则代码会复制文件夹中的所有文件,这会浪费磁盘上的空间(因为已经有大约20000个文件夹,100 GB大小的文件夹) 如何检查文件大小或上次修改日期,如果有更改,则复制文件夹 例如,如果我想复制名为123456的文件夹而不进行修改,代码会检查目标上是否已经存在该文件夹(不检查文件大小等),然后询问是否要覆盖该文件夹,然后复制文件 如果文件夹名称为123456,修订号为2,则代码首先在源文件夹中搜索文件夹123456,然后在修订2文件夹中搜索文件夹(文件夹名称本身为修订2),然后复制(如果目标文件夹中存在,则要求覆盖)然后复制。文件夹修正案0、修正案1和修正案2中的某些文件保持不变。只有少数文件会发生更改 我不希望总是复制普通文件,它们必须仅位于根文件夹中,并且只有更改的文件才能复制到修订文件夹中 我只使用java来实现这一点仅当大小、上次修改日期、文件名或文件内容在java中发生更改时才复制文件,java,file,Java,File,我在不同的网络驱动器上有2个文件夹Q:和K: 每天我都会传递一个文件夹名,带或不带修订号,程序会在Q:drive中搜索该文件,如果没有修订号,它会将整个文件夹复制到K:drive的文件夹,如果没有修订号,它会在主文件夹中搜索该修订文件夹,并仅将该文件夹复制到K:drive的文件夹 现在我正在复制,没有比较两个文件夹中的文件大小或上次修改日期文件。在修改的情况下,如果未选中文件大小,则代码会复制文件夹中的所有文件,这会浪费磁盘上的空间(因为已经有大约20000个文件夹,100 GB大小的文件夹)
public static void copyFolder(File src, File dest, File destf) throws IOException
{
if(src.isDirectory())
{
//if directory does not exist, 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);
File dst = new File(destf, file);
if(((dst.lastModified() - srcFile.lastModified()) == 0) || ((dst.length() - srcFile.length()) == 0))
{
int op = JOptionPane.showConfirmDialog(null, srcFile.getName() + " File exists! \n Do you want to overwrite it?", "Confirm Window", JOptionPane.YES_NO_OPTION);
if (op == JOptionPane.NO_OPTION)
JOptionPane.showMessageDialog(null, "Copy Canceled");
else
copyFolder(srcFile, destFile, dst);
}
else
copyFolder(srcFile, destFile, dst);
}
}
else
{
if(((dest.lastModified() - src.lastModified()) == 0) || ((dest.length() - src.length()) == 0))
{
int op = JOptionPane.showConfirmDialog(null, src.getName() + " File exists! \n Do you want to overwrite it?", "Confirm Window", JOptionPane.YES_NO_OPTION);
if (op == JOptionPane.NO_OPTION)
JOptionPane.showMessageDialog(null, "Copy Canceled");
else
{
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dest);
byte[] buffer = new byte[1024];
int length;
while ((length = in.read(buffer)) > 0)
{
out.write(buffer, 0, length);
}
in.close();
out.close();
System.out.println("File copied from " + src + " to " + dest);
}
}
else
{
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dest);
byte[] buffer = new byte[1024];
int length;
while ((length = in.read(buffer)) > 0)
{
out.write(buffer, 0, length);
}
in.close();
out.close();
System.out.println("File copied from " + src + " to " + dest);
}
}
}
如何检查文件大小
或上次修改日期
复制文件的不同方法(包括性能时间)的一些示例: 与此主题相关的答案: 但是您可以使用
file1.lastModified()!=file2.lastModified()
作为简单的日期检查,也可以使用.length作为长度检查
File f1 = new File("c:\test\aaa\temp.txt");
File f2 = new File("d:\test\aaa\temp.txt");
// Same modify date?
if ((f1.lastModified() != f2.lastModified()) || (f1.length() != f2.length())) {
// add to copy list
}
您需要注意上次修改的日期,因为您没有完全按照linuxcp-a
命令那样复制文件。相反,您是按照正常方式复制文件,即在文件所在位置创建一个新文件,并将内容流式传输到文件中。在本例中,.length()就足够了。您始终可以生成文件的CRC32或MD5哈希,以检查内容是否匹配。但是,如果您要复制非常大或数千个文件,这是一个缓慢的过程
编辑
由于上次修改日期对您来说是一项重要检查,并且您需要保留有关复制文件的上次修改日期属性,因此您可以通过以下方式手动设置该属性:
File f1 = new File("c:\test\aaa\temp.txt");
File f2 = new File("d:\test\aaa\temp.txt");
if (!f1.exists()) {
// throw an exception
}
// Copy the file as is, retain attributes, replace existing
CopyOption[] options = new CopyOption[] {COPY_ATTRIBUTES, REPLACE_EXISTING};
if (f2.exists()) {
if ((f1.lastModified() != f2.lastModified()) || (f1.length() != f2.length()) {
Files.copy(f1, f2, options);
}
} else {
Files.copy(f1, f2, options);
}
这依赖于JavaSE7
以下是Java站点中复制文件的示例:
你还应该看看:
及
您想检查文件是否自…何时起最后一次修改?而“修订号”是指修订号还是未进行修订?您好,谢谢您的回复。这是修订号。I,谢谢您的回复,让我检查并还原。您好,谢谢您的回复,让我检查并还原。我将在您的桌面上创建两个测试文件在尝试将其应用于文档树之前,请先进行操作并进行检查。对不起,我不明白您的意思。您能简要介绍一下吗?在硬盘上创建一个文件,复制它,编辑它,然后运行
.lastModified()
和.length()
在将其应用于完整文件复制操作之前,请检查f1!=f2
。好的,让我试试