java IO-搜索目录所有子文件、复制文件到指定地方

6 篇文章 0 订阅
2 篇文章 0 订阅

JAVA I/O 系统(一)


一:搜索制定目录的所有文件:


package itest01;

import java.io.File;

public class FileTest01 {

    public static void main(String[] args) {
        String fileName = "C:\\Users";
        File f = new File(fileName);
        printAllFile(f);
    }

    public static void printAllFile(File f) {
        if (f != null) {
            if (f.isDirectory()) {
                File[] fileArray = f.listFiles();
                if (fileArray != null) {
                    for (File file : fileArray) {
                        printAllFile(file);
                    }
                }
            } else {
                System.out.println(f);
            }
        }
    }
}

二:复制文件到指定地方,并打印到控制台:

package itest01;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

public class FileTest02 {
    String file1path;
    String file2path;
    File file1;
    File file2;

    public FileTest02(String file1path, String file2path) {
        this.file1path = file1path;
        this.file2path = file2path;
        this.file1 = new File(file1path);
        this.file2 = new File(file2path);
        if (!file1.exists()) {
            System.out.println("被复制文件不存在,请重新输入");

        }

    }
//复制文件
    public void copyFile() {

        try {
            BufferedInputStream burInput = new BufferedInputStream(new FileInputStream(file1));

            BufferedOutputStream bufOut = new BufferedOutputStream(new FileOutputStream(file2));

            if (bufOut != null && burInput != null) {
                int temp = 0;
                while ((temp = burInput.read()) != -1) {
                    bufOut.write(temp);

                }
                bufOut.flush();
                bufOut.close();
            }

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
     //打印到控制台
    public void printFile() {
        try {
            BufferedReader bReader = new BufferedReader(new FileReader(file2));
            String s = null;
            StringBuilder sBuilder = new StringBuilder();
            while ((s = bReader.readLine()) != null) {
                sBuilder.append(s + "\n");

            }
            System.out.println(sBuilder.toString());
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO: handle exception
        }

    }

    public static void main(String[] args) {
        FileTest02 fTest02 = new FileTest02("C:\\aa.txt", "C:\\cc.txt");
        fTest02.copyFile();
        fTest02.printFile();

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值