栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java 字节流 读取 写入 文件复制 文件夹复制

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java 字节流 读取 写入 文件复制 文件夹复制

package io;

import org.junit.Test;

import java.io.*;


public class StudyFileByte {

    
    @Test
    public void inputStreamTest() {
        InputStream is = null;
        try {
            //创建源
            File file = new File("test.txt");
            //选择流
            is = new FileInputStream(file);
            //读取文件
            int len = 0;
            byte[] dates = new byte[1024];
            while ((len = is.read(dates)) != -1) {
                String str = new String(dates, 0, len);
                System.out.println(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //释放资源
            try {
                if (null != is) {
                    is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    
    @Test
    public void outputStreamTest() {
        OutputStream os = null;
        try {
            //创建源
            File file = new File("test2.txt");
            //选择流,第二个参数当为true时表示在文件中数据末尾添加数据,为false时覆盖文件中的数据进行添加
            os = new FileOutputStream(file, false);
            //写入文件
            String str = "Hello world";
            byte[] dates = str.getBytes();
            os.write(dates, 0, dates.length);
            os.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != os) {
                    os.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    public void copyTest() {
        copyFile("test.txt", "test2.txt");
    }

    
    public void copyFile(String souFile, String tarFile) {
        InputStream is = null;
        OutputStream os = null;
        try {
            File file1 = new File(souFile);
            File file2 = new File(tarFile);
            is = new FileInputStream(file1);
            os = new FileOutputStream(file2);
            int len = -1;
            byte[] date = new byte[1024];
            while ((len = is.read(date)) != -1) {
                os.write(date, 0, len);
            }
            os.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != os) {
                    os.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (null != is) {
                    is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    
    @Test
    public void copyDirTest() {
        StudyFileByte s = new StudyFileByte();
        s.copy("D:\Code\IDEAWorkspace\ProjectMaven\SXTTest\testDir1", "D:\Code\IDEAWorkspace\ProjectMaven\SXTTest\testDir2");
    }

    public void copy(String oneFile, String twoFile) {
        File file1 = new File(oneFile);
        File file2 = new File(twoFile);
        copyDir(file1, file2);
    }

    public void copyDir(File file1, File file2) {

        if (file1.isFile()) {
            InputStream is = null;
            OutputStream os = null;
            try {
                is = new FileInputStream(file1);
                os = new FileOutputStream(file2);
                int len = -1;
                byte[] date = new byte[1024];
                while ((len = is.read(date)) != -1) {
                    os.write(date, 0, len);
                }
                os.flush();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (null != os) {
                        os.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    if (null != is) {
                        is.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        if (file1.isDirectory()) {
            file2.mkdir();
            File[] list = file1.listFiles();
            for (File f : list) {
                copyDir(f, new File(file2, f.getName()));
            }
        }
    }

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/352083.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号