把一个文件夹复制到另一个路径下,经过仔细的筛选整理的,希望对大家有用。。。- [hide]public static void main(String[] args)throws Exception {
- // TODO Auto-generated method stub
- File file = new File("D:\\Java--String");
- File newfile = new File("E:\\Java--String副本");
- ArrayList<File> al = new ArrayList<>();
- fileTolist(file,al);
- copyTodir(al,newfile);
- }
- //统计,分类添加
- public static void fileTolist(File dir,ArrayList<File> al)
- {
- File[] files = dir.listFiles();
- for(File file:files)
- {
- if(file.isDirectory())
- {
- al.add(file);
- fileTolist(file,al);
- }
- else{
- al.add(file);
- }
- }
- }
- //创建目录及拷贝文件
- public static void copyTodir(ArrayList<File> al,File newfile)throws Exception
- {
- newfile.mkdirs();
- for(int i = 0;i<al.size();i++)
- {
- if(al.get(i).isDirectory())
- {
- String str = al.get(i).getAbsolutePath().replace("D:\\Java--String", "E:\\Java--String副本");
- new File(str).mkdirs();
- }
- else
- {
- copyfile(al.get(i));
- }
- }
- }
- //拷贝文件
- public static void copyfile(File file)throws Exception
- {
- BufferedReader bufr = new BufferedReader(
- new InputStreamReader(new FileInputStream(file)));
- BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter
- (new FileOutputStream(file.toString().replace("D:\\Java--String", "E:\\Java--String副本"))));
- String line = null;
- while((line=bufr.readLine())!=null)
- {
- bufw.write(line);
- bufw.newLine();
- bufw.flush();
- }
- bufr.close();
- bufw.close();
- }[/hide]
复制代码
|
|