views:

214

answers:

5

I have a code which at first search for some files that have the "java" extension and then I want to copy the content of one file to these files which has the "java" extension but I don't know that why this code doesn't work?

 public static void main(String[] args) {
    if (args.length > 0) {
        FileInputStream in = null;

        File sourceFile;
        File directory;
        try {
            directory = new File(args[0]);
            sourceFile = new File(args[1]);

            FilenameFilter filterObj = new FileFilter("java");
            String[] fileName = directory.list(filterObj);
            System.out.println("number of files " + fileName.length + "");
            System.out.println("names of file : ");
            System.out.println("-----------------------");

            for (int i = 0; i < fileName.length; i++) {
                System.out.println(fileName[i]);
                List<File> file= (List<File>) new File(fileName[i]);//EDIT:I have `deleted it`

                Test.copy(sourceFile,directory);
            }
        } finally {
            try {
                in.close();
            } catch (IOException ex) {
                Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    } else {
        System.out.println("no arguments were given!");
    }
}

static void copy(File src, File dst) {
    InputStream in = null;
    try {
        in = new FileInputStream(src);
        OutputStream out = new FileOutputStream(dst);
        // Transfer bytes from in to out
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
    } catch (IOException ex) {
        Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        try {
            in.close();
        } catch (IOException ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

the way of copying a file to another is correct?

+1  A: 

This line won't work:

List<File> file= (List<File>) new File(fileName[i]);

You can't cast a File to a List<File>. Since you don't access file any further, you could simply delete this line.

Your for loop should read like this:

for ( String oneFilename : fileName ) {
    System.out.println(oneFilename);

    Test.copy(sourceFile, new File( oneFilename ) );
}

This will copy the content of your source file to all files found in your target directory.

tangens
yes thanks I delete it from my code but still it doesn't work!!
Johanna
no I want to copy one file which is sourceFile to the other files which has java extension.
Johanna
OK, I edited my answer accordingly.
tangens
+3  A: 

Does it have to be done in that manner? Why don't you simply copy it this way:

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class ...

  public static void main(String[] args) throws IOException {
   if (!args.length > 0) return;//or any other handling

    File src = new File("input.file");
    File dst = new File("output.file");

    FileReader in = new FileReader(src);
    FileWriter out = new FileWriter(dst);
    int c;

    while ((c = in.read()) != -1) out.write(c);

    in.close();
    out.close();
  }
}
Vuk
+4  A: 

FileUtils.copyFile(originalFile, destinationFile); (from Apache commons-io) would be simpler.

If you don't use it, you can still look at its source code.

Bozho
And even if she doesn't use it, she should still look at it to see how experienced developers handled it.
extraneon
good idea. I linked the source.
Bozho
thanks for your nice answer!
Johanna
+1  A: 

Is it me or are you actually trying to copy a file (example: a.java) to directory (let's say b) and expect it to become b/a.java?

What you seem to call is a copy(filename, directory), but the implementation of copy suggests copy(from_filename, to_filename).

It's the name "directory" and the way you use it that makes me suspicious.

You should copy from a file to a file, and that means creating the name of the file (probably based on the name of the input file).

By the way - that problem would result in a FileNotFoundException when creating the FileOutputStream. So if you don't see any exceptions (you did check the output of the logger and stdout?) this might not be the problem.

And you seem to copy sourceFile to directory over and over; you create in the for loop a variable "file" but you actually copy sourceFile, and not file.

And I'd love to help out but I still haven't got a clue what it SHOULD do.

extraneon
+1  A: 
FilenameFilter filterObj = new FileFilter("java");

This line doesn't compile at all. FileFilter is an interface.

Further there are several flaws in the code, you're declaring a FileInputStream in main, but never using it and still trying to close it in finally without nullchecking. In the copy method the OutputStream is also incorrectly handled and you're incorrectly closing inside the try block instead of the finally block alone.

You should in the future really be more clear about the occurred problem. I don't believe that the code has actually run, because it doesn't compile at all. How are you developing and running this code? Inside an IDE? Is the stderr well configured? And the Logger?

BalusC