IO

作者: 盘丝仙仙 | 来源:发表于2019-10-31 16:33 被阅读0次

    2019-10-31

    package com.lixing.io11;

    import java.io.*;

    public class PrintDirectoryTest01 {

    //01.File

        //02.字节输入输出流InputStream OutputStream/FileInputStream FileOutputStream

        //03.字符输入输出流Reader Writer/FileReader FileWriter

        //04.内存字节流 字符流ByteArrayInputStream ByteArrayOutputStream CharArrayReader CharArrayWriter

        //05.打印流PrintStream PrintWriter

        //06.字符 字节缓冲流BufferedInputStream BufferedOutputStream BufferedReader BufferedWriter

        //1.一般可以用记事本打开的文件,我们可以看到内容不乱码的。就是文本文件,可以使用字符流。

        // 而操作二进制文件(比如图片、音频、视频)必须使用字节流

        //2.想要操作IO 但是又不希望在磁盘上产生一些文件 可以将内存当作一个临时文件 就要用到内存流

        //3.如果输出的是int Integer double Double 需要转化为字节后才可以输出 比较麻烦 就要用到打印流

        //4.用缓冲区暂存输入 输入流再一次性读取内容 可以避免中文的读错乱问题

        //5.BufferedInputStream与ByteArrayInputStream区别

        //BufferedInputStream, 源码看起来跟ByteArrayInputStream类似,

        //BufferedInputStream缓冲流有一个默认的缓存区,也可以自己定义大小,读取的时候先从缓存区读取数据,

        // 如果缓存区没有数据了,那么需要从输入源读取数据到缓存区

        //好处在于不用一次性把数据全部加载到内存,也不用频繁去请求输入源的数据,

        //6.GBK(国标扩)  包含简体中文 繁体中文

        //GB2312 只包含简体中文

        //ISO8859-1 国际编码 可以描述任何文字信息

        //UTF-8 unicode可变长度编码

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

    //OutputStream

            String path =new File("").getAbsolutePath() +"/test1031.txt";

            File file =new File(path);

            FileOutputStream output =new FileOutputStream(file);

            output.write("C:\\Users\\zhu\\0307\\2.txt \r\n".getBytes());

            output.write("现在时间是:201910311618".getBytes());

            output.close();

            //output

            //这个目录生成C:\xuexi\wenjian3\lixinghua\test1031.txt

            //内容是:

            //C:\Users\zhu\0307\2.txt

            //现在时间是:201910311618

        }

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

    //OutputStream

            String path =new File("").getAbsolutePath() +"/test1031.txt";

            File file =new File(path);

            FileWriter writer =new FileWriter(file);

            writer.write("C:\\Users\\zhu\\0307\\2.txt \r\n");

            writer.write("现在时间是:201910311624");

            writer.close();

            //ZH总结 跟上面的区别是 writer不用getBytes()

    //output

    //C:\Users\zhu\0307\2.txt

            //现在时间是:201910311624

        }

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

    String path =new File("").getAbsolutePath() +"/test1031.txt";

            File file =new File(path);

            FileInputStream fis =new FileInputStream(file);

            byte[] buf =new byte[1024];

            int count =0;

            //read()返回的是单个字节数据(字节数据可以直接专程int类型),但是read(buf)返回的是读取到的字节数,真正的数据保存在buf中

            while ((count = fis.read(buf)) >0) {

    //每次最多将1024个字节转换成字符串,这里tmp2.txt中的字符小于1024,所以一次就读完了

                //循环次数 = 文件字符数 除以 buf长度

                System.out.println(new String(buf, 0, count));

            }

    fis.close();

            //output

    //C:\Users\zhu\0307\2.txt

            //现在时间是:201910311624

        }

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

    String path =new File("").getAbsolutePath() +"/test1031.txt";

            File file =new File(path);

            FileReader reader =new FileReader(file);

            char[] buf =new char[1024];

            int hasRead =0;

            // 每个char都占两个字节,每个字符或者汉字都是占2个字节,因此无论buf长度为多少,总是能读取中文字符长度的整数倍,不会乱码

            while ((hasRead = reader.read(buf)) >0) {

    // 如果buf的长度大于文件每行的长度,就可以完整输出每行,否则会断行。

                // 循环次数 = 文件字符数 除以 buf长度

                System.out.println(new String(buf, 0, hasRead));

            }

    //output

    //C:\Users\zhu\0307\2.txt

            //现在时间是:201910311624

        }

    public static void main5(String[] args)throws FileNotFoundException {

    String path =new File("").getAbsolutePath() +"/test1031.txt";

        File file =new File(path);

        //暂时看 好像是最简单的了

        Scanner input =new Scanner(new FileInputStream(file));

        StringBuilder builder =new StringBuilder();

        while (input.hasNextLine()) {

    builder.append(input.nextLine()).append("\r\n");

        }

    System.out.println(builder.toString());

        input.close();

        //output

    //C:\Users\zhu\0307\2.txt

        //现在时间是:201910311545

    }

    }

    相关文章

      网友评论

          本文标题:IO

          本文链接:https://www.haomeiwen.com/subject/etptbctx.html