Skip to content

Instantly share code, notes, and snippets.

@Viacheslav77
Last active January 29, 2016 13:03
Show Gist options
  • Select an option

  • Save Viacheslav77/a76a74da70624d182374 to your computer and use it in GitHub Desktop.

Select an option

Save Viacheslav77/a76a74da70624d182374 to your computer and use it in GitHub Desktop.
/*Написать программу для копирования всех файлов из одного каталога в другой.*/
package CopyFile;
/*Написать программу для копирования всех файлов из одного каталога в другой.*/
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
public class MyClass {
public static void copyFile(String src, String dest) throws Exception {
System.out.print("Копируем файл: " + src + "... в файл: " + dest + ".");
byte[] buf;
try (
FileInputStream in = new FileInputStream(src);
FileOutputStream out = new FileOutputStream(dest)
){
buf = new byte[1000000]; // 1 Mb
int r;
do {
System.out.print(".");
r = in.read(buf, 0, buf.length);
if (r > 0)
out.write(buf, 0, r);
} while (r > 0);
}
System.out.println();
}
static class MyFileFilter implements FilenameFilter {
private String ext;
public MyFileFilter(String ext) {
this.ext = ext;
}
public boolean accept(File dir, String name) {
return name.endsWith(ext);
}
}
private static void findFiles(String srcPath, ArrayList<String> list) throws IOException {
File dir = new File(srcPath);
System.out.println("----------------------------------");
System.out.println("Список файлов в дирректории: " + srcPath);
System.out.println("----------------------------------");
File[] files = dir.listFiles();
for( File f: files){
if(f.isFile())
list.add(f.getCanonicalPath());
}
for (String s : list)
System.out.println(s);
System.out.println("----------------------------------");
}
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<String>();
try {
findFiles("d:\\1",list);
} catch (IOException e) {
e.printStackTrace();
}
for (int i=0; i< list.size(); i++)
try {
copyFile(list.get(i), list.get(i).replace("1", "2"));
} catch (Exception e) {
e.printStackTrace();
}
ArrayList<String> list1 = new ArrayList<String>();
try {
findFiles("d:\\2",list1);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment