I/O输入及输出

输入输出(I/O)
输入:把电脑硬盘中的数据读入到程序中,即为输入,input,进行数据的read操作。
输出:把程序中的数据写出外部设备,即为输出,output,进行数据的write操作。

输入输出的整个过程如上图所示;

字节流和字符流

根据流(java提供的读写文件的类)的读写单位分为字节流和字符流

字节流:每次读写是以字节为单位(计算机中的所有数据存储都是以字节为单位),可以读取任意文件。

字符流:每次读写都是以字符为单位,只能读写纯文本文件。

输入流和输出流

流按着数据的传输方向分为:
输入流:往程序中读叫输入流。
输出流:从程序中往外写叫输出流。
 InputStream和OutputStream的子类都是字节流 可以读写二进制文件,主要处理音频、图片、歌曲、字节流,处理单元 为1个字节。
字节流中常用类
字节输入流 FileInputStream
字节输出流 FileOutputStream

 Reader和Writer的子类都是字符流 主要处理字符或字符串,字符流处理单元为1个字符。 字节流将读取到的字节数据,去指定的编码表中获取对应文字。
字符流中常用类
字符输入流 FileReader
字符输出流 FileWriter

字节流:

1、InputStream的基本方法:
读取一个字节并以整数的形式返回(0~255),如果返回-1已到输入流的末尾。
int read() throws IOException
读取一系列字节并存储到一个数组buffer, 返回实际读取的字节数,如果读取前已到输入流的末尾返回-1
int read(byte[] buffer) throws IOException
关闭流释放内存资源
void close() throws IOException
2、OutputStream的基本方法:
向输出流中写入一个字节数据,该字节数据为参数b的低8位
void write(int b) throws IOException
将一个字节类型的数组中的从指定位置(off)开始的
len个字节写入到输出流
void write(byte[] b, int off, int len) throws IOException
关闭流释放内存资源
void close() throws IOException
举例说明:
        FileInputStream in=new FileInputStream("A:/Demo.txt");//读入
        FileOutputStream outputStream=new FileOutputStream("A:/Demo.txt");
        int b=0;
        while ((b=in.read())!=-1){//每次读取一个字节,并输出
            System.out.println(b);
        }
        in.close();
        outputStream.close();

        FileInputStream  in1=new FileInputStream("A:/8IO.pdf");
        FileOutputStream out1=new FileOutputStream("A:/8IO.pdf");
        byte [] c=new byte[1024];//利用byte数组来进行读写,效率更快
        int size;
        while((size=in1.read(c))!=-1){
            out1.write(c,0, size);
        }
        in1.close();
        out1.close();
        

3、值得注意的是:在写这些程序的时候,总是会产生一些异常,上述程序都只是简单的将其抛出,下面演示这些异常的处理(利用try、catch、finally):

public static void main(String[] args) {
        FileInputStream a=null;
        FileOutputStream b=null;
        try {
            a=new FileInputStream("A:/Demo.txt");
            b=new FileOutputStream("A:/Demo.txt");
            int c=a.read();
            b.write(c);
        }
         catch (FileNotFoundException e) {
             System.out.println("文件找不到异常!");
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("传输中断异常!");
            e.printStackTrace();
        }
        finally {
            try {
                if(a!=null){
                    a.close();
                }
                if (b!=null){
                    b.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
4、流根据封装类型还分为:节点流和处理流
节点流:如果流封装的是某种特定的数据源,如文件、字符串、字符串数组等,则称为节点流。节点流直接对数据进行读和写操作。
处理流:如果流封装的是其它流对象,称为处理流。处理流提供了缓冲功能,提高读写效率,同时增加了一些新的方法。
下面程序是对字节流的节点流和处理流进行区分:
FileInputStream in=new FileInputStream("A:/Demo.txt");//节点流
FileOutputStream out=new FileOutputStream("A:/Demo.txt");
BufferedInputStream bin=new BufferedInputStream(in);//处理流,参数是节点流
BufferedOutputStream bout=new BufferedOutputStream(out); 

处理流的操作和方法与节点流差别不大。 但需要注意的是,利用完缓冲流后,要使用b.flush()方法刷新缓冲区。

字符流

字符流:每次读写都是以字符为单位,只能读写纯文本文件。

和字节流类似,只是读写单位有多改变,一次读一个字符。

    public static void main(String[] args) throws IOException {
        FileReader reader=new FileReader("A:/test.txt");
        FileWriter writer=new FileWriter("A:/test1.txt");
        /*
        一个字一个字读写
         */
        int c;
        while ((c= reader.read())!=-1){
            System.out.println(c);
            System.out.println((char) c);
            writer.write(c);
        }

        /*
        几个字几个字读写
         */
        char[]cs=new char[10];
        int size=0;
        while ((size= reader.read(cs))!=-1){
            writer.write(cs,0,size);
        }
        reader.close();
        writer.close();
    }

字符流的缓冲流:

    public static void main(String[] args) throws IOException {
        //节点流:
        FileReader reader=new FileReader("A:/test.txt");
        FileWriter writer=new FileWriter("A:/test2.txt",true);//内容追加,不会将原数据覆盖,一直向其中添加
        //处理流:
        BufferedReader breader=new BufferedReader(reader);
        BufferedWriter bwriter=new BufferedWriter(writer);
        String line=null;
        while ((line=breader.readLine())!=null){
            bwriter.write(line);
            bwriter.newLine();//插入一个换行符
        }
        reader.close();
        writer.close();
        bwriter.flush();

    }
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值