Java移动文件与某些文件扩展名

Java移动文件与某些文件扩展名

本文介绍了Java移动文件与某些文件扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我正在一个简单的程序和设置的程序,我需要程序来检查一个目录中的zip文件和任何zip文件需要被移动到另一个文件夹。



比方说,我有folder1,它包含6个zip文件,然后我有另一个文件夹称为folder2,我需要所有的拉链,只有在folder1的拉链移动到folder2



谢谢任何帮助,这个问题。

顺便说一句,我是一个菜鸟,所以任何代码示例将不胜感激

解决方案

对于 folder1 中的每个文件,使用查看文件名以。zip结尾。如果是,将其移动到 folder2 。 提供了一个很好的方法来做到这一点(尽管这不是必要的)。



它看起来像这样(未测试):

 文件f1 =新文件(/ path / to / folder1); 
文件f2 =新文件(/ path / to / folder2);
$ b $ FilenameFilter filter = new FilenameFilter()
{
@Override public boolean accept(File dir,String name)
{
return name.endsWith 。压缩);
}
};
$ b $ for(File f:f1.listFiles(filter))
{
// TODO move to folder2
}


Hi i'm working on a simple program and for the set up of the program i need the program to check a directory for zip files and any zip files in there need to be moved into another folder.

Lets say i have folder1 and it contains 6 zip files and then i have another folder called folder2 that i need all the zips and only the zips in folder1 moved to folder2

Thank you for any help one this problem.

Btw i'm a noob so any code samples would be greatly appreciated

解决方案

For each file in folder1, use String#endsWith() to see if the file name ends with ".zip". If it does, move it to folder2. FilenameFilter provides a nice way to do this (though it's not strictly necessary).

It would look something like this (not tested):

File f1 = new File("/path/to/folder1");
File f2 = new File("/path/to/folder2");

FilenameFilter filter = new FilenameFilter()
{
    @Override public boolean accept(File dir, String name)
    {
        return name.endsWith(".zip");
    }
};

for (File f : f1.listFiles(filter))
{
    // TODO move to folder2
}

这篇关于Java移动文件与某些文件扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 19:13