主要目的练习File类与FileWrite类的应用
- import java.io.*;
- class CopyDirectory
- {
- public static void main(String[] args)
- {
- File srcFile = new File("E:\\test");
- File tarFile = new File("F:\\");
- copyDirectory(srcFile,tarFile);
- }
- private static void copyDirectory(File srcFile ,File tarFile)
- {
- try
- {
- if(srcFile.isDirectory())
- {
- tarFile = new File(tarFile,srcFile.getName());
- tarFile.mkdir();
- }
- File [] files = srcFile.listFiles();
- if(files == null || files.length == 0)
- return;
- for(int i=0;i<files.length;i++)
- {
- File temFile = files[i];
- if(temFile.isDirectory())
- copyDirectory(temFile,tarFile);
- else if(temFile.isFile())
- {
- FileReader fr = new FileReader(temFile);
- String sFileName = temFile.getName();
- File temTarFile = new File(tarFile,sFileName);
- FileWriter fw = new FileWriter(temTarFile);
- char [] bytes = new char[1024];
- int len = 0;
- while((len = fr.read(bytes)) != -1)
- {
- fw.write(bytes);
- }
- //if(temTarFile.getName().endsWith(".java"))
- //System.out.println(temTarFile.renameTo(new File(temTarFile.getAbsoluteFile().getName().replace(".java",".txt"))));
- try
- {
- if(fr != null)
- {
- fr.close();
- }
- if(fw != null)
- {
- fw.close();
- }
- }
- catch (IOException ex)
- {
- }
- }
- }
- }
- catch (IOException ex)
- {
- ex.getMessage();
- }
- }
- }
复制代码 |
|