一.File字段

在System中的方法
getProperties()
确定当前的系统属性。
getProperty(String key)
获取指定键指示的系统属性。
getProperty(String key, String def)
获取用指定键描述的系统属性。
简单说下 java中的properties文件是一种配置文件,主要用于表达配置信息,文件类型为*.properties,格式为文本文件,文件的内容是格式是”键=值”的格式,在properties

文件中,可以用”#”来作注释,properties文件在Java编程中用到的地方很多,操作很方便。
1.File类定义的字段

// 路径分隔符
String psString = File.pathSeparator;
// 名称(文件)分隔符
String s = File.separator;

2.Properties
是一个双列集合 key 对应的 value

// System的方法getProperties()
Properties prop = System.getProperties();
// 相关key
// 路径分隔符
path.separator
// 文件分隔符
file.separator
// 项目工作目录绝对路径
user.dir
// 用户根目录
user.home
// 换行
line.separator

应用场景:
getProperty(String key, String def)
获取用指定键描述的系统属性。

// 平时写一个绝对路径
// 桌面的路径
String path = "/users/xiaodong/Desktop/test";
// Properties
// 通过key值获取对应的value
String desktop = System.getProperty("user.home") + s + "Desktop";
String path = desktop + s + "test";

二.IO流

1.什么是流?

程序操作数据类似"管道输送水源" ,从源头到目标的一个不间断持续的过程,将此功能封装抽象定义出来的类,我们称之为 流

2.概述
IO流简单来说就是Input (输入 读)和Output(输出 写)流,IO流主要是用来处理设备之间的数据传输,Java对于数据的操作都是通过流实现,而java用于操作流的对象都在IO包中。
3.分类:

按操作数据分为:字节流和字符流
    字节流: 以字节作为流的操作对象 可以处理任意文本 不具有默认缓冲区
    字符流: 以字符作为流的操作对象 可以高效处理文本相关数据 有默认缓冲区

    按流向分:输入流和输出流。如:InputStream和OutputStream
    输入流: 将文件的数据输入到程序中 形成程序数据
    输出流: 将程序中已有的数据输出到文件中 形成数据持续化

4.IO流常用的基类:

InputStream        OutputStream

三.字节流

5.字节输出流
FileOutputStream()
功能: 将数据以字节的方式写入文件

public class IO01 {
        // 步骤
    // 1.指定写入的文件存放的路径
    // 2.指定写入的文件名
    // 3.获取写入的信息(byte[] | int) 
    // 4.创建操作该文件的FileOutputStream类对象
    // 5.写入数据
    // 6.关闭流 (重点)
    public static void main(String[] args) {
        // 1.指定写入的文件存放的路径
        //   获取目标路径的File对象
        // 项目工作目录绝对路径
        String workpath = System.getProperty("user.dir");
        // 文件的分隔符
        String s = System.getProperty("file.separator");
        String pathname = workpath + s + "io01";

        File path = new File(pathname);
        //   文件夹存在与否 需要手动处理
        if (!path.exists()) 
            path.mkdirs();

        // 2.指定写入的文件名
        //   获取目标路径的File对象
        //   文件存在与否 不需要考虑 (文件流会处理)
        File file = new File(path, "1.txt");

        // 3.获取写入的信息(byte[] | int) 
        int info = 65;

        // 4.创建操作该文件的FileOutputStream类对象
        OutputStream output = null;
        try {
            output = new FileOutputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        // 5.写入数据
        try {
            output.write(info);
        } catch (IOException e) {                 
            e.printStackTrace();
        }

        // 6.关闭流 (重点)
        try {
            output.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println("文件写入成功");

    }

}

6.FileInputStream()
功能: 以字节操作读取

public static void main(String[] args) {
        // 通过文件路径创建文件File对象
        String child = "io02" + Files.s_f + "100.txt";
        File file = new File(Files.d_w, child);
        // 为该File对象创建对应的输入流
        FileInputStream input = null;
        try {
            // 创建流对象时 可能会出现FileNotFoundException
            input = new FileInputStream(file);
            // 使用流对象读取文件的过程中 可能会出现IO异常
            int res = 0;
            // input.read() 即是判断条件 又是运算结果
            // 所以将此操作上移移到循环条件 操作用()嵌套再循环条件中
            // (byte[] b)
            // 从此输入流中将最多 b.length 个字节的数据读入一个 byte 数组中
            byte[] bs = new byte[1024];
            while ((res = input.read(bs)) != -1) {
                System.out.print(new String(bs));
            }

        } catch (FileNotFoundException e) {
            System.err.println("异常 - FileNotFoundException" + e.getMessage());
        } catch (IOException e) {
            System.err.println("异常 - IOException" + e.getMessage());
        } finally {
            // 无论如何都需要考虑流关闭
            try {
                // 为了避免出现空指针异常(流创建失败 流便不存在)
                if (input != null) 
                    input.close();
            } catch (IOException e) {
                System.err.println("异常 - IOException" + e.getMessage());
            }
        }

    }

7.边读边写 (相当于复制 粘贴)

public static void main(String[] args) {
            // 边读边写
            // 获取读取的目标文件
            // Files.d_d 这里是我获取桌面路径的方法
            String pString = Files.getPath(Files.d_d);
            File file = new File(pString, "1.txt");

            // 创建写入的File对象
            String path = Files.getPath(Files.d_d);
            File file1 = new File(path, "111.txt");

            // 创建读流
            FileInputStream input = null;
            // 创建写流
            FileOutputStream output = null;
            int res = 0;
            try {
                input = new FileInputStream(file);
                output = new FileOutputStream(file1);
                byte[] bs = new byte[1024];
                while ((res = input.read(bs)) != -1) {
                    // 将读到的数据写入到文件中
                    output.write(bs);
                }
            } catch (FileNotFoundException e) {
                System.out.println("异常 - FileNotFoundException");
            } catch (IOException e) {
                System.out.println("异常 - IOException");
            } finally {
                try {
                    if (input != null)
                    input.close();
                    if (output != null) 
                    output.close();

                } catch (IOException e) {
                    System.out.println("异常 - IOException");
                }
            }
            System.out.println("程序执行完成");


    }

8.非文本文件采用字节流复制

// 非文本文件采用字节流复制
public class IO07 {
    public static void main(String[] args) throws Exception{
        // 创建读写的路径
        // 读取文件的路径 
        File source = new File(Files.d_d, "001.png");
        // 写入文件的路径
        File target = new File(Files.d_d, "002.png");
        // 读取流
        FileInputStream input = new FileInputStream(source);
        // 写入流
        FileOutputStream output = new FileOutputStream(target);
        // 读写操作
        int len = 0;
        byte[] res = new byte[1024];
        while ((len = input.read(res)) != -1) {
            // 按byte[]数组进行数据写入 从0开始 写入len中
            output.write(res,0,len);
        }
        input.close();
        output.close();

        // 删除源文件 - 剪裁操作
        source.delete();

    }

四.字符流

1.文本文件专属操作流 可以更高效的操作文本
2.拥有默认缓冲区
3.以字符为操作单位 无需考虑不同字符所占字节数

一般在操作文件流时,不管字节流还是字符流,都可以按照以下方式进行
* (1) 使用file类找到一个文件
* (2) 通过file类的对象去实例化字节流或字符流的子类。
* (3) 进行字节(字符)的读写操作。
* (4) 关闭文件流。

// 字符流顶层流 读流 写流
    // Reader | Writer
    public static void main(String[] args) {
        writeSomething();
        readeSomething();
    }

    // 写入文本文件
    private static void writeSomething() {
        // 获取目标文件夹
        String path = Files.getPath(Files.d_w,"io03");
        File file = new File(path, "a.txt");
        FileWriter writer = null;
        try {
            writer = new FileWriter(file);
            char[] info = {'1','A','一','①'};
            // 从info数组的索引1开始 截取2长度写入到文件中
            writer.write(info,1,3);
            // 书写一次 清一次缓冲区
            writer.flush();
            writer.write(

8.字节流转为字符流的桥梁

// 字节流 转 字符流

        // 创建对象
        String path = Files.getPath(Files.d_d);
        File source = new File(path, "2.txt");

        FileInputStream in = null;
        try {
            in = new FileInputStream(source);
        } catch (FileNotFoundException e) {
            System.out.println("异常 - FileNotFoundException");
        }
        // 创建字节流转字符流的对象  在字节流转字符流的的过程中 需要字节流对象
        InputStreamReader inReader = new InputStreamReader(in);

        int res = 0;
        try {
            // 多少个字节去读取
            char[] cs = new char[1024];
            while ((res = inReader.read(cs)) != -1) {
                System.out.println(new String(cs));
            }
        } catch (IOException e) {
            System.out.println("异常 - IOException");
        } finally {
            // 关闭流
                try {
                    if (inReader != null)
                    inReader.close();
                } catch (IOException e) {
                    System.out.println("异常 - IOException");
                }
        }

    }

9.带有缓冲区的字节流

public static void main(String[] args) {
        // 带有缓冲区的字节流

        // 获取文件路径 创建File对象
        String path = Files.getPath(Files.d_d);
        File source = new File(path, "2.txt");

        // 存放文件路径 
        File target = new File(path, "200.txt");
        // 边读边写

        // 读流
        FileInputStream in = null;
        // 写入流
        FileOutputStream out = null;
        try {
            in = new FileInputStream(source);
            out = new FileOutputStream(target);
        } catch (FileNotFoundException e) {

        }
        BufferedInputStream inStream = new BufferedInputStream(in);
        BufferedOutputStream oStream = new BufferedOutputStream(out);

        try {
            int res = 0;
            byte[] bs = new byte[1024];
            while ((res = inStream.read(bs)) != -1) {
                oStream.write(bs);
                oStream.flush();
            }
        } catch (IOException e) {
            System.out.println("异常 - IOException");
        } finally {
                try {
                    if (inStream != null)
                    inStream.close();
                    if(oStream != null)
                        oStream.close();
                } catch (IOException e) {
                    System.out.println("异常 - IOException");
                }

        }
        System.out.println("程序执行完毕");
    }

10.带有缓存区的字符流

public static void main(String[] args) {
        // 带有缓冲区的字符流

        // 获取文件路径 创建File对象
                String path = Files.getPath(Files.d_d);
                File source = new File(path, "2.txt");
                // 存放文件路径 
                File target = new File(path, "200.txt");
                // 边读边写

                // 读流
                FileReader in = null;
                FileWriter out = null;
                try {
                    in = new FileReader(source);
                    out = new FileWriter(target);
                } catch (FileNotFoundException e) {

                } catch (IOException e) {

                }
                BufferedReader reader = new BufferedReader(in);
                BufferedWriter writer = new BufferedWriter(out);


                try {
                    int res = 0;
                    char[] cbuf = new char[1024];
                    while ((res = reader.read(cbuf)) != -1) {
                        writer.write(cbuf, 0, res);
                        writer.newLine();
                        writer.flush();
                    }
                } catch (IOException e) {
                    System.out.println("异常 - IOException");
                } finally {

                        try {
                            if (reader != null)
                            reader.close();
                            if(writer != null)
                                writer.close();
                        } catch (IOException e) {
                            System.out.println("异常 - IOException");
                        }

                }
                System.out.println("程序执行完毕");
    }