IO流

1.流:是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象。即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输特性将流抽象为各种类,方便更直观的进行数据操作。
2.IO流的分类
•根据处理数据类型的不同分为:字符流和字节流
•根据数据流向不同分为:输入流和输出流
3.字节流和字符流的区别:
•读写单位不同:字节流以字节(8bit)为单位,字符流以字符为单位,根据码表映射字符,一次可能读多个字节。
•处理对象不同:字节流能处理所有类型的数据(如图片、avi等),而字符流只能处理字符类型的数据。
结论:只要是处理纯文本数据,就优先考虑使用字符流。 除此之外都使用字节流。
4.输入和输出流
从磁盘将a.txt文件内容“abc” Ctrl+C 即input到内存中,Ctrl+V即从内存中output到磁盘上。
这里写图片描述
5.Java流类图结构
这里写图片描述
6.这里写图片描述
7.Input读入代码

public class T_1_FileInput {

    public void readFile(String path) {

        File file = new File(path);
        if (file.exists()) {
            if (file.isFile()) {
                // 验证:是一个文件了
                try {
                    FileInputStream fis = new FileInputStream(file);
                    /** 第一种 **/
                    //read返回的是int型,可知返回的是ascoii值
                    // int asciiCode = fis.read();
                    // while (asciiCode == -1) {
                    // asciiCode = fis.read();
                    // }
                    /** 第二种 **/
                    //ascoii值强转回原来的字符(char)
                    int asciiCode = -1;
                    while ((asciiCode = fis.read()) != -1) {
                        System.out.println("" + asciiCode + " "
                                + (char) asciiCode);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }
    }

    public void readFile_bytes(String path) {
        File file = new File(path);
        if (file.exists()) {
            if (file.isFile()) {
                // 验证:是一个文件了
                FileInputStream fis = null; // init
                try {
                    fis = new FileInputStream(file);
                    byte[] b = new byte[1024]; // 1k 缓存数组
                    int length = -1;
                    while ((length = fis.read(b)) != -1) {
                        System.out.println(b.toString());
                        // byte[] -> String
                        System.out.println("长度:" + length);
                        String result = new String(b, 0, length);
                        System.out.println(result);
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    try {
                        if (fis != null)
                            fis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                }

            }
        }
    }
}

8.OutputStream输出流

public class T_2_FileOutputStream {

    public void write(String path, String content) {
        this.write(path, content, false);
    }

    public void write(String path, String content, boolean isAppend) {
        FileOutputStream fos = null;
        try {

            fos = new FileOutputStream(path, isAppend);
            // string -> byte[] new String(b)
            byte[] bytes = content.getBytes();
            fos.write(bytes);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (fos != null)
                    fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

}

9.copy文件的拷贝

public class T_3_FileCopy {
    /**
     * @Description 文件的拷贝
     * @param srcPath
     *            源文件 aa.txt
     * @param destPath
     *            目标文件 a/b/cc.txt
     */
    public void copyFile(String srcPath, String destPath) {
        File fileSrc = new File(srcPath);
        File fileDest = new File(destPath);
        /** 第一步: 目标文件有没有 **/
        if (fileSrc.exists()) {
            /** 第二步: 目标文件不存在,在文件outpus避免路径找不到:把文件夹生成 **/
            // fileoutputStream能帮我们生成文件(txt),但是不能帮我们生成文件夹
            if (!fileDest.exists()) {
                String parentPath = fileDest.getParent();
                new File(parentPath).mkdirs();// 创建文件夹
            }
            /** 第三步: 拷贝 **/
            FileInputStream fis = null;
            FileOutputStream fos = null;
            try {
                fis = new FileInputStream(fileSrc);
                fos = new FileOutputStream(fileDest);
                byte[] b = new byte[1024];
                int length = -1;
                while ((length = fis.read(b)) != -1) {
                    System.out.println(new String(b, 0, length));
                    fos.write(b, 0, length);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (fis != null)
                    try {
                        fis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                if (fos != null)
                    try {
                        fos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
            }

        }

    }

    /**
     * @Description
     * @param srcPath
     *            E:\资料安装包\文档\课程计划\机器人\新建文件夹
     * @param destPath
     *            E:\资料安装包\文档\课程计划\机器人\新建文件夹2
     */
    public void copyFolder(String srcPath, String destPath) {
        File fileSrc = new File(srcPath);
        File fileDest = new File(destPath);
        if (fileSrc.exists()) {
            if (fileSrc.isFile()) {
                copyFile(srcPath, destPath);
            } else {
                File[] listFiles = fileSrc.listFiles();
                for (int i = 0; i < listFiles.length; i++) {
                    File childFile = listFiles[i];
                    /** 递归 **/
                    // E:\资料安装包\文档\课程计划\机器人\新建文件夹\xxx\aaa.txt
                    String srcPahtChild = childFile.getAbsolutePath();
                    String child = childFile.getName();// xxx
                    // E:\资料安装包\文档\课程计划\机器人\新建文件夹2/xxx/aaa.txt
                    String destPathChild = destPath + "/" + child;
                    copyFolder(srcPahtChild, destPathChild);
                }
            }
        }
    }

}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值