java 当中的流

初识io

代码演示

     private final static String URL = "E:\\";
     private final static String READURL = "H:\\kj\\202402\\LS0205.00P";


    @Test
    public void testOutputStream() {
        long start = System.currentTimeMillis();
        try (
                InputStream in = new FileInputStream( READURL );
                OutputStream out = new FileOutputStream( URL + "FileOutputStream输出流.txt" );
        ) {
            int len;
            while ((len = in.read()) != -1) {
                out.write( (char) len );
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        System.out.println( "消耗时间" + (end - start) / 1000.0 + "秒" );
    }

    @Test
    public void testOutputStreamSz() {
        long start = System.currentTimeMillis();
        try (
                InputStream in = new FileInputStream( READURL );
                OutputStream out = new FileOutputStream( URL + "FileOutputStream输出流字节数组.txt" );
        ) {
            byte[] bytes = new byte[1024 * 80];
            int lent;
            while ((lent = in.read( bytes )) != -1) {
                out.write( bytes, 0, lent );
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        Runtime runtime = Runtime.getRuntime();
        System.out.println( "最大可用内存: " + runtime.maxMemory() / 1024 );
        System.out.println( "已分配内存: " + runtime.totalMemory() / 1024 );
        System.out.println( "可用内存: " + runtime.freeMemory() / 1024 );
        System.out.println( "已使用内存: " + (runtime.totalMemory() - runtime.freeMemory()) / 1024 );
        System.out.println( "消耗时间" + (end - start) / 1000.0 + "秒" );
         /* 最大可用内存: 253440
            已分配内存: 15872
            可用内存: 11551
            已使用内存: 4320
            消耗时间1.343秒*/
    }

    @Test
    public void testBufferedSz() {
        long start = System.currentTimeMillis();
        try (
                InputStream inputStream = new FileInputStream( READURL );
                BufferedInputStream in = new BufferedInputStream( inputStream, 1024 * 80 );  
                OutputStream outputStream = new FileOutputStream( URL + "Buffered输出流字节数组.txt" );
                OutputStream out = new BufferedOutputStream( outputStream, 1024 * 80 );
        ) {
            byte[] bytes = new byte[1024 * 80];
            int lent;
            while ((lent = in.read( bytes )) != -1) {
                out.write( bytes, 0, lent );
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        Runtime runtime = Runtime.getRuntime();
        System.out.println( "最大可用内存: " + runtime.maxMemory() / 1024 );
        System.out.println( "已分配内存: " + runtime.totalMemory() / 1024 );
        System.out.println( "可用内存: " + runtime.freeMemory() / 1024 );
        System.out.println( "已使用内存: " + (runtime.totalMemory() - runtime.freeMemory()) / 1024 );
        System.out.println( "消耗时间" + (end - start) / 1000.0 + "秒" );
        /*   最大可用内存: 253440
            已分配内存: 15872
            可用内存: 11572
            已使用内存: 4299
            消耗时间1.156秒* */
    }


    @Test
    public void testFileReaderSz() {
        long start = System.currentTimeMillis();
        try (
                Reader in = new FileReader( READURL );
                Writer out = new FileWriter( URL + "FileReaderSz输出流字符数组.txt" );
        ) {
            char[] chars = new char[1024 * 80];
            int lent;
            while ((lent = in.read( chars )) != -1) {
                out.write( chars, 0, lent );
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        Runtime runtime = Runtime.getRuntime();
        System.out.println( "最大可用内存: " + runtime.maxMemory() / 1024 );
        System.out.println( "已分配内存: " + runtime.totalMemory() / 1024 );
        System.out.println( "可用内存: " + runtime.freeMemory() / 1024 );
        System.out.println( "已使用内存: " + (runtime.totalMemory() - runtime.freeMemory()) / 1024 );
        System.out.println( "消耗时间" + (end - start) / 1000.0 + "秒" );
        /*  最大可用内存: 253440
            已分配内存: 15872
            可用内存: 11407
            已使用内存: 4464
            消耗时间5.171秒 */
    }

    @Test
    public void testBufferedReaderSz() {
        long start = System.currentTimeMillis();
        try (
                Reader reader = new FileReader( READURL );
                BufferedReader in = new BufferedReader( reader, 1024 * 80 );
                Writer writer = new FileWriter( URL + "BufferedReader输出流字符数组.txt" );
                BufferedWriter out = new BufferedWriter( writer, 1024 * 80 );
        ) {
            char[] chars = new char[1024 * 80];
            int lent;
            while ((lent = in.read( chars )) != -1) {
                out.write( chars, 0, lent );
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        Runtime runtime = Runtime.getRuntime();
        System.out.println( "最大可用内存: " + runtime.maxMemory() / 1024 );
        System.out.println( "已分配内存: " + runtime.totalMemory() / 1024 );
        System.out.println( "可用内存: " + runtime.freeMemory() / 1024 );
        System.out.println( "已使用内存: " + (runtime.totalMemory() - runtime.freeMemory()) / 1024 );
        System.out.println( "消耗时间" + (end - start) / 1000.0 + "秒" );
        /* 最大可用内存: 253440
            已分配内存: 15872
            可用内存: 11376
            已使用内存: 4495
            消耗时间5.015秒 * */
    }

    @Test
    public static void testWrit() {
        try (
                BufferedReader reader = new BufferedReader( new FileReader( "src/test/java/tjqs/hy/file.txt" ) );
                BufferedWriter writer = new BufferedWriter( new FileWriter( "src/test/java/tjqs/hy/file1.txt", true ) ); //有TRUE不会覆盖以前的数据,没有或者是FALSE就会覆盖原来的数据  不使用多态的原因是要使用BufferedInputStream这个安行读取的方法,如果不使用readLin方法就可以使用多台
        ) {
            String len;
            while ((len = reader.readLine()) != null) {
                writer.write( len, 0, len.length() );
                writer.write( "\n" );
            }
            System.out.println( "=======================" );
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    //字节转换流,当文件编码跟代码编码不一致是
    @Test
    public static void testZj() {

        try (
                //原始文件
                InputStream in = new FileInputStream( "src/test/java/tjqs/hy/file_gbk.txt" );
                //目标文件
                BufferedWriter out = new BufferedWriter( new FileWriter( "src/test/java/tjqs/hy/file_gbk1.txt" ) );
                //把原始的字节输入流按照指定的字符集编码转换成字符流编码
                Reader is = new InputStreamReader( in, "GBK" );  //in:是流通到,"GBK":是原始文件的文件编码   对应的代码是utf-8 
                //把字符集输入流包装成缓冲字符输入流
                BufferedReader bufferedReader = new BufferedReader( is );
        ) {

            String len;
            while ((len = bufferedReader.readLine()) != null) {
                out.write( len, 0, len.length() );
                out.write( "\n" );
            }

        } catch (Exception e) {
            e.printStackTrace();
        }


    }


@Test
    public static void testPrintStream() {
        //打印流输出流
        try (
                PrintStream stream = new PrintStream( "src/test/java/tjqs/hy/打印流.txt" );
        ) {
            //这一部分打印在控制台
            System.out.println( 's' );
            System.out.println( 'a' );
            //这一部分打印在src/test/java/tjqs/hy/打印流.txt文件当中
            System.setOut( stream );
            System.out.println( 'b' );
            System.out.println( 'b' );
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public static void testLog() {
        //打印流输出流
        try (
                PrintWriter stream = new PrintWriter( "src/test/java/tjqs/hy/打印流.txt" );
        ) {
            stream.write( 's' );
            stream.write( 'a' );
            stream.write( '2' );
            stream.print( '1' );
            System.out.println( stream );
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*
     * 数据流输出
     * */
    @Test
    public static void testDataOutputStream() {
        //写入文件中的数据会乱码,但是不影响他的读数据
        try (
                DataOutputStream dataOutputStream = new DataOutputStream( new FileOutputStream( "src/test/java/tjqs/hy/file_gbk.txt" ) );
        ) {
            dataOutputStream.writeByte( 'a' );
            dataOutputStream.writeBoolean( true );
            dataOutputStream.writeInt( 12 );
            dataOutputStream.writeUTF( "哈哈哈" );
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*
     * 数据流输入
     * */
    @Test
    public static void testDataInputStream() {
        try (
                DataInputStream dataInputStream = new DataInputStream( new FileInputStream( "src/test/java/tjqs/hy/file_gbk.txt" ) );
        ) {
            int b = dataInputStream.readByte();
            System.out.println( b );
            boolean readBoolean = dataInputStream.readBoolean();
            System.out.println( readBoolean );
            int i = dataInputStream.readInt();
            System.out.println( i );
            String s = dataInputStream.readUTF();
            System.out.println( s );
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*
     * io流框架
     * */
    @Test
    public static void testCommons() {
        try {
            //文件拷贝 不需要释放流,文件会自动创建
            FileUtils.copyFile( new File( "src/test/java/tjqs/hy/file_gbk1.txt" ),
                    new File( "I:/jar/commons-io-2.16.1/file_gbkkk.txt" ) );
            //文件夹进行拷贝
            FileUtils.copyDirectory( new File( "I:/jar/commons-io-2.16.1/docs" ), new File( "I:/jar/" ) );


            //删除文件夹
            FileUtils.cleanDirectory( new File( "I:/jar/commons-io-2.16.1" ) );
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*
     * io流框架
     * */
    @Test
    public static void testCommonsIo() {
        try {
            //输入流,写文件
            String s = FileUtils.readFileToString( new File( "src/test/java/tjqs/hy/file_gbk1.txt" ) );
            System.out.println( s );
            //输出流,读文件
            FileUtils.writeStringToFile( new File( "src/test/java/tjqs/hy/file_gbk11.txt" ), s, true );
            //文件拷贝
            IOUtils.copy( new FileInputStream( "src/test/java/tjqs/hy/file_gbk11.txt" ), new FileOutputStream( "src/test/java/tjqs/hy/file_gbk11_Io.txt" ) );
            //文件夹拷贝
            IOUtils.copy( new FileReader( "src/test/java/tjqs/hy/file_gbk11.txt" ), new FileWriter( "src/test/java/tjqs/hy/file_gWriter.txt" ) );

            //写数据
            IOUtils.write( s, new FileOutputStream( "src/test/java/tjqs/hy/file_gbk11_写数据.txt" ) );
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

图形解释

IO流体系流程图

流分类

 IO流-字节流

字节输入流接口:
InputStream
实现类:
new FileInputStream();

字节输出流接口:
OutputStream
实现类:
new FileOutputStream();

IO流-字符流

字符输入流接口:
Reader
实现类:
new FileReader()

字符输出流接口:
Writer 
实现类:
w=new FileWriter()

IO流-缓冲流

字节缓冲输入流:
new BufferedInputStream(new FileInputStream(""));

字节缓冲输出流:
new BufferedOutputStream(new FileOutputStream(""));


字符缓冲输写入流
new BufferedReader(new FileReader(""));

字符缓冲输写出流
new BufferedWriter(new FileWriter(""));

IO流-转换流

输入字符转换流:
new InputStreamReader();

输出字符转换流:
new OutputStreamWriter();

IO流-打印流

字符写入流:
new PrintStream()

字符写出流:
new PrintWriter()

IO流-数据流

字符输入数据流:
new DataInputStream()  //输入是a就是a 是98就是98,不会编译


字符输出数据流:
new DataOutputStream()

IO流-序列化流

字节输入序列化:
new ObjectInputStream();

字节输出序列化:
 new ObjectOutputStream();

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值