0

i write a program that will search for files with the given extension in my PC. now i want to add one more thing to it. i wish that my program will copy these files into a specific location on my PC. here is my code sample :-

Finder(String pattern) 
    {
        matcher = FileSystems.getDefault().getPathMatcher("glob:" + pattern);
   }

    // Compares the pattern against
    // the file or directory name.
    void find(Path file) {
        Path name = file.getFileName();
        if (name != null && matcher.matches(name)) {
            System.out.println(file);
            String s = new String(name.toString());
            //System.out.println(s);
            File f = new File(s);
            //System.out.println(f.getAbsolutePath());
            FileInputStream fileInputStream = null;
Vicky
  • 23
  • 1
  • 3
  • 9
  • What exactly is the question here? – Matthias Aug 02 '13 at 06:45
  • Check [this thread](http://stackoverflow.com/questions/1146153/copying-files-from-one-directory-to-another-in-java) – Luca Basso Ricci Aug 02 '13 at 06:46
  • i want to copy all the text files that resides in my PC, to my pendrive. my program is successfully searching for all text files and now i want to copy these.. how i can do this?? – Vicky Aug 02 '13 at 06:47
  • Check thread posted in comment above yours: you have only to choice the preferred way (based on you request about time, semplicity of code and so on). [http://stackoverflow.com/questions/1146153/copying-files-from-one-directory-to-another-in-java](http://stackoverflow.com/questions/1146153/copying-files-from-one-directory-to-another-in-java) – Luca Basso Ricci Aug 02 '13 at 06:55

4 Answers4

0

Once you have the FileInputStreams for the files you want to copy, you can create FileOutputStreams outputting into the place you want to copy the files. Then, use a loop like the following to copy the file:

byte temp;
while ((temp = fileInputStream.read()) != -1)
    fileOutputStream.write(temp);
tbodt
  • 16,609
  • 6
  • 58
  • 83
0

For a quick way to do it you can use the method FileUtils.copyFileToDirectory from Apache Commons IO.

growlingchaos
  • 96
  • 2
  • 8
0

There are new great ways to walk the file tree with the FileVistitor interface. Click here. After approaching a file that matches your criteria, move it using some high performanced new io:

public static void copyFile(File sourceFile, File newDirectory) throws IOException {
    File destFile = new File(newDirectory, sourceFile.getName());
    if(!destFile.exists()) {
        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();
        }
    }
}

Define the directory you want to move the files too in the parameter "newDirectory"

everydave
  • 81
  • 1
  • 1
  • 5
0

There is no question in your question! If you ask how to copy files, you have options:

  • Use java.nio.file.Files.copy() method. This is preferred since you do not have to copy data yourself, can use Path and it is in standard library
  • Use streams and copy data yourself as suggested by other answers

  • There are other ways like invoking command on system to copy

Piro
  • 1,367
  • 2
  • 19
  • 40