1

i've searched a keyword "MISSING RESIDUES" from files in a directory and stored the file names in Arraylist. now i want to move those filtered file names from this directory to another. can anybody help me with that?

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.");
        }
}

}

amarah
  • 23
  • 6

3 Answers3

1

For each file, uses Files.move:

    public static void main(String[] args) throws Exception {       
        Files.move(new File("fileName").toPath(), new File("newDirectory/fileName").toPath(), StandardCopyOption.REPLACE_EXISTING);
    }
Marlon Patrick
  • 2,366
  • 1
  • 18
  • 26
  • While `new File("fileName").toPath()` instead of `Paths.get("fileName)`? Why `new File("newDirectory/fileName").toPath()` instead of `Paths.get("newDirectory/fileName")`? Better yet, why not just have a `Paths.get("newDirectory")` and use `.resolve()`? – fge Mar 15 '15 at 00:32
  • Also, uh, well, why not just use a `Files.newDirectoryStream()` with an appropriate `DirectoryStream.Filter`? – fge Mar 15 '15 at 00:34
  • @fge, any specific reason for "new File().toPath". Both ways gets a Path. I confess that its format is more elegant, but usually for those who are not familiar with NIO2 is less obvious. – Marlon Patrick Mar 15 '15 at 00:36
  • there IS a difference between `new File(...).toPath()` and `Paths.get()`. Subtle but which may be important... – fge Mar 15 '15 at 00:46
  • 1
    The first, and obvious difference, is that a `File` can only refer to `Path`s issued from the default filesystem; the other and much less obvious one is that an fs entry is ultimately a sequence of bytes, not chars; which means the encoding matters. And while `File` is oblivious about encoding impossibilities, `Path` is not. [Demonstration](http://pastie.org/10026850). – fge Mar 15 '15 at 01:07
0

try that, instead to store the file name, store the file object, and then for each file stored, move to a new destination. myFile.renameTo(new File("/the/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.");
}
Ricardo Umpierrez
  • 768
  • 2
  • 11
  • 24
0

You can do it like below

// File (or directory) with old name File file = new File("oldname");

// 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
}

Copied from Rename a file using Java

Community
  • 1
  • 1
muasif80
  • 5,586
  • 4
  • 32
  • 45
  • `File`'s `.renameTo()` is unreliable. It returns `false`; OK. Then what is the error? – fge Mar 15 '15 at 01:11