【JavaIO流之实战演练】

实战演练

1. 练前热身:

  • 概念:

File类:文件和目录路径名的抽象形式。

  • 文件的创建方法:
public class FileController {


   public static void fileTest() throws IOException {
//        File f1 = new File("D://备份//test4//demo//mo");
       File f = new File("D://备份//test4");
       if (!f.exists()){
           //以多重目录创建
//            f1.mkdirs();
           //创建单层目录
//            f.mkdir();
           //以文件形式创建
           f.createNewFile();
       }


       File f2 = new File("D://备份//test");
       //父路径
       String parent = f2.getParent();
       System.out.println(parent);
       //打印文件绝对路径
       System.out.println("绝对路径:"+f2.getAbsolutePath());
   }


   public static void main(String[] args) throws IOException {
       fileTest();
   }
}

2.练习题目

题目一:
  文件复制,将文件复制到新的文件里,一边读一边写。

代码展示:

public static void fileCopy() {
    FileInputStream inputStream = null;
    FileOutputStream outputStream = null;
    try {
        //读取文件内容
        inputStream = new FileInputStream("C:\\Users\\Administrator\\Downloads\\Video\\f0.mp4");
        outputStream = new FileOutputStream("D:\\备份\\f2.mp4", true);

        byte[] bytes = new byte[1024];
        int readCount = 0;
        while ((readCount = inputStream.read(bytes)) != -1) {
            //写入
            outputStream.write(bytes, 0, readCount);
        }

        //刷新
        outputStream.flush();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        //不能同时try,会影响到下一个流的关闭
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (outputStream == null) {
            //关闭流
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

题目二:
 目录copy,将文件或者文件夹从当前目录复制到新的目录下。

代码展示:

/**
     * 目录拷贝
     */
    public static void copyMkdir(File targetPath, File resourcePath) {
        if (resourcePath == null) {
            return;
        }
        if (resourcePath.isFile()) {
            String copyTarfPath = targetPath.getAbsolutePath().concat(resourcePath.getAbsolutePath().substring(3));
            File file = new File(copyTarfPath);
            File parentFile = file.getParentFile();
            //当前文件下是文件,需要判断父目录是否存在,不存在新建
            if (!parentFile.exists()) {
                parentFile.mkdirs();
            }
            //如果是文件进行读写操作
            fileContentInput(file, resourcePath);
            return;
        }
        File[] reFiles = resourcePath.listFiles();
        for (File reFile : reFiles) {
            //如果是目录
            if (reFile.isDirectory()) {
                //给目标目录建立相应的文件夹
                String tarPath = targetPath.getAbsolutePath();
                //目标文本的目录
                String copyTarfPath = tarPath.concat(reFile.getAbsolutePath().substring(3));
                System.out.println(copyTarfPath);
                //判断目标目录是否存在
                File tarFileDir = new File(copyTarfPath);
                if (!tarFileDir.exists()) {
                    tarFileDir.mkdirs();
                }
            }
            //递归调用
            copyMkdir(targetPath, reFile);


        }


    }


    public static void fileContentInput(File tarFilePath, File reFilePath) {
        //定义输出流
        FileOutputStream out = null;
        //定义输入流
        FileInputStream ios = null;


        byte[] bytes = new byte[1024 * 1024];  //每次读取1兆
        int readCount = 0;
        try {
            ios = new FileInputStream(reFilePath);
            out = new FileOutputStream(tarFilePath);
            if ((readCount = ios.read(bytes)) != -1) {
                //一边读一边写
                out.write(bytes, 0, readCount);
            }
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (ios != null) {
                try {
                    ios.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }


    public static void main(String[] args) throws IOException {

        File targetPath = new File("C://");
        File resourcePath = new File("D://备份");
        copyMkdir(targetPath, resourcePath);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值