zoukankan      html  css  js  c++  java
  • I/O流的学习

    一.I/O流

      1.判定是输入还是输出我们应该站在程序的立场;

      2.判断传输的是字节还是字符,从而决定管道的大小,字节传递是根本,可以传递所有的数据类型,字符传递专门用来传递文本数据,字节主要用来传递二进制数据。

      3.Java流的四大父类:

        ①字节流Inputstream和Outputstream

        ②字符流Reader 和 Writer

    二.主要的使用文件的拷贝

     例:

    package com.lovo.test;
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    
    public class TestStream {
        public static void main(String[] args) {//功能:将D:/test.avi  拷贝到  F:/wudi.avi
            FileInputStream fis = null;
            FileOutputStream fos = null;
            try {
                //1、建立管道
                fis = new FileInputStream("D:/test.avi");
                fos = new FileOutputStream("F:/wudi.avi");
                
                //2、操作管道
                byte[] b = new byte[1024];
                int length = 0;//记录读取了多少个有效字节数
                while((length = fis.read(b)) != -1){
                    fos.write(b,0,length);
                    fos.flush();//强制刷出缓冲区的内容
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally{
                //3、关闭管道
                if(fis != null){
                    try {
                        fis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if(fos != null){
                    try {
                        fos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
  • 相关阅读:
    【算法】CDQ分治 -- 三维偏序 & 动态逆序对
    【题解】ZJOI2007报表统计
    【乱七八糟】兰陵王
    【题解】NOI2017游戏
    【题解】JSOI2010满汉全席
    【题解】NOI2014动物园
    【题解】HNOI2010合唱队
    【题解】SDOI2010地精部落
    cf 843 D Dynamic Shortest Path [最短路+bfs]
    cf 843 B Interactive LowerBound [随机化]
  • 原文地址:https://www.cnblogs.com/qq1083735206/p/6242252.html
Copyright © 2011-2022 走看看