使用存储的arraylist中的文件名将文件移动到不同的目录中

时间:2015-03-15 00:15:38

标签: java

我搜索了一个关键字" MISSING RESIDUES"来自目录中的文件并将文件名存储在Arraylist中。现在我想将这些过滤的文件名从这个目录移动到另一个目录。任何人都可以帮助我吗?

public class Filter_missingres_files {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here
    File dir = new File("D:\\iso_upd"); // directory = target directory.
    if(dir.exists()) // Directory exists then proceed.
    { 
        Pattern p = Pattern.compile("MISSING RESIDUES"); // keyword = keyword to search in files.
        ArrayList<String> list = new ArrayList<String>(); // list of files.

        for(File f : dir.listFiles())
        {
            if(!f.isFile()) continue;
                try
                {
                    FileInputStream fis = new FileInputStream(f);
                    byte[] data = new byte[fis.available()];
                    fis.read(data);
                    String text = new String(data);
                    Matcher m = p.matcher(text);
                    if(m.find())
                    {
                        list.add(f.getName()); // add file to found-keyword list.
                    }
                    fis.close();
                } 
                catch(Exception e)
                {
                    System.out.print("\n\t Error processing file : "+f.getName());
                }

        }
    System.out.print("\n\t List : "+list); // list of files containing keyword.
    } // IF directory exists then only process.
            else
        {
            System.out.print("\n Directory doesn't exist.");
        }
}

}

3 个答案:

答案 0 :(得分:1)

对于每个文件,使用Files.move:

    public static void main(String[] args) throws Exception {       
        Files.move(new File("fileName").toPath(), new File("newDirectory/fileName").toPath(), StandardCopyOption.REPLACE_EXISTING);
    }

答案 1 :(得分:0)

尝试这样做,而不是存储文件名,存储文件对象,然后对于存储的每个文件,移动到新目的地。 myFile.renameTo(新文件(“/ / / new / place”));

  // TODO code application logic here
    File dir = new File("D:\\iso_upd"); // directory = target directory.
    if(dir.exists()) // Directory exists then proceed.
    { 
        Pattern p = Pattern.compile("MISSING RESIDUES"); // keyword = keyword to search in files.
        ArrayList<File> list = new ArrayList<File>(); // list of files.

        for(File f : dir.listFiles())
        {
            if(!f.isFile()) continue;
            try
            {
                FileInputStream fis = new FileInputStream(f);
                byte[] data = new byte[fis.available()];
                fis.read(data);
                String text = new String(data);
                Matcher m = p.matcher(text);
                if(m.find())
                {
                        list.add(f); // add file to found-keyword list.
                    }
                    fis.close();
                } 
                catch(Exception e)
                {
                    System.out.print("\n\t Error processing file : "+f.getName());
                }

            }

        }  
    String detinationPath= "D:\\destinationdir"
    File destinatiorDir = new File(detinationPath); // directory = destination directory.
    if(destinatiorDir.exists()) // Directory exists then proceed.
    { 
       for(File f :list ){
        f.renameTo(new File(detinationPath+ f.getName()));
    }
}else{
    System.out.print("\n Directory doesn't exist.");
}

答案 2 :(得分:0)

你可以像下面这样做

//具有旧名称的文件(或目录)     文件文件=新文件(&#34; oldname&#34;);

// File (or directory) with new name
File file2 = new File("newname");
if(file2.exists()) throw new java.io.IOException("file exists");

// Rename file (or directory)
boolean success = file.renameTo(file2);
if (!success) {
    // File was not successfully renamed
}

Rename a file using Java

复制