本帖最后由 bowen-xiao 于 2015-1-20 08:38 编辑
程序需求:
遍历D盘目录下,所有的文件夹,找到.java文件,统一复制到D:盘test目录。
问题:注意查看输出结果Test目录,删除下面文件,再次运行,看两次输出结果。
- /**
- * ClassName:CopyFileDemo2 <br/>
- * Function: 遍历D盘目录下,所有的文件夹,找到.java文件,统一复制到D:盘test目录。
- * Reason: 注意查看输出结果
- * Date: 2015-1-12 下午1:05:31 <br/>
- * @author Bowen
- * @version
- * @since JDK 1.6
- * @see
- */
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.OutputStreamWriter;
- public class CopyFileDemo2 {
- public static void main(String[] args) throws IOException {
- File srcForder = new File("d:\\");
- File destForder = new File("d:\\Test\\");
- isFile(srcForder, destForder);
- }
- private static void isFile(File srcForder, File destForder) throws IOException {
- if (!destForder.exists())
- destForder.mkdir();
- File[] file = srcForder.listFiles();
- if (file != null) {
- for (File srcFile : file) {
- if (srcFile.isDirectory()) {
- isFile(srcFile, destForder);
- } else {
- if (srcFile.getName().endsWith(".java")) {
-
- File destFile = new File (destForder,srcFile.getName()) ;
- copyFile(srcFile, destFile);
- }
- }
- }
- }
- }
- private static void copyFile(File srcFile, File destFile) throws IOException {
- BufferedReader br = new BufferedReader(new FileReader(srcFile));
- BufferedWriter bw = //new BufferedWriter(new FileWriter(destFile)) ;
-
- new BufferedWriter(new OutputStreamWriter(new FileOutputStream(destFile, true), "UTF-8"));
- String line = null ;
- while ( (line = br.readLine())!= null) {
- if( !line.isEmpty()){
- System.out.println( br.readLine());
- bw.write(line);
- bw.newLine();
- bw.flush();
- }
- }
- br.close();
- bw.close();
- }
- }
复制代码
|
|