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?