Java IO流

Java中I/O操作主要是指使用java.io包下的内容,进行输入、输出操作。输入也叫做读取数据,输出也叫做写出数据。

IO的分类

根据数据的流向分为输入流和输出流:

输入流:把数据从其他设备上读取到内存中的流;

输出流:把数据从内存中写出到其他设备上的流。

格局数据的类型分为字节流和字符流:

字节流:以字节为单位,读写数据的流;

字符流:以字符为单位,读写数据的流。

字节流

一切皆为字节

一切文件数据在存储时,都是以二进制数字的形式保存,都是一个一个的字节,那么传输时一样如此。所以,字节流可以传输任意文件数据。在操作流的时候,无论使用什么样的流对象,底层传输的始终为二进制数据。

字节输出流【OutputStream】

java.io.OutputStream抽象类是表示字节输出流的所有类的超类,将指定的字节信息写出到目的地。它定义了字节输出流的基本共性功能方法。

浅写一下代码(使用File对象创建流对象):

File f1 = new File("d:/清秋.txt");
f1.createNewFile();

OutputStream out = new FileOutputStream(f1);

写出字节方法:

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

    File f1 = new File("d:/aa/bb/cc");
    File f2 = new File(f1,"c.txt");
    f1.mkdirs();
    f2.createNewFile();

    OutputStream out = new FileOutputStream(f2);
    out.write(97);

}

数据追加续写:

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

    File f1 = new File("d:/清秋.txt");
    f1.createNewFile();

    OutputStream out = new FileOutputStream(f1,true);

    String msg ="清秋。";
    byte[] bytes = msg.getBytes();
    out.write(bytes);
    out.close();
}

字节输入流【Inputstream】

java,io.Inputstream 抽象类是表示字节输入流的所有类的超类,可以读取字节信息到内存中。它定义了字节输入流的基本共性功能方法。

浅写一下代码(读取指定字节):

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

    File f1 = new File("D:/a.txt");

    InputStream in = new FileInputStream(f1);
    byte[] bytes = new byte[10];
    int n;
    while ((n=in.read(bytes)) !=-1){
        String s = new String(bytes,0,n);
        System.out.println(s);
    }
}

综合案例:完成文件的复制:

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

    File f1 = new File("D:/a.txt");
    File f2 = new File("D:/b.txt");

    f2.createNewFile();

    InputStream in = new FileInputStream(f1);
    OutputStream out= new FileOutputStream(f2);

    byte[] bytes = new byte[30];

    int n;
    while ((n=in.read(bytes)) != -1){

        out.write(bytes,0,n);
    }
}

字符流

当使用字节流读取文本文件时,可能会有一个小问题。就是遇到中文字符时,可能不会显示完整的字符,那是因为中文可能占用多个字节存储。所以Java提供一些字符流类,以字符为单位读写数据,专门用于处理文本文件。

字符输入流【Reader】

java.io.Reader抽象类是表示用于读取字符流的所有类的超类,可以读取字符信息到内存中。它定义了字符输入流的基本共性功能方法。

File f1 = new File("d:/a.txt");

Reader in = new FileReader(f1);

浅写一下代码:

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

    File f1 = new File("d:/a.txt");

    //定义字符输入流
    Reader in = new FileReader(f1);
    int n1 = in.read();
    System.out.println((char) n1);

    int n2 = in.read();
    System.out.println((char) n2);
}

每次读取缓冲区:

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

        File f1 = new File("d:/a.txt");

        //定义字符输入流
        Reader in = new FileReader(f1);
//        int n1 = in.read();
//        System.out.println((char) n1);
//
//        int n2 = in.read();
//        System.out.println((char) n2);

        char[] buffer = new char[10];

        int n1 = in.read(buffer);
        String s1 = new String(buffer,0,n1);
        System.out.println(s1);

        int n2 = in.read(buffer);
        String s2 = new String(buffer,0,n2);
        System.out.println(s2);

        in.close();
    }

字符输出流【Writer】

java.io.Writer抽象类是所有写出字符流的所有类的超类,将指定的字符信息写出到目的地。它定义了字符输出流的基本共性功能方法。

浅写一下代码:

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

    File f1 = new File("d:/b.txt");
    if (!f1.exists()){
        f1.createNewFile();
    }

    //定义字符输出流
    Writer out = new FileWriter(f1);

    out.write(97);
    out.write((int)'清');

    char[]ch = {'a','b','c','d'};
    out.write(ch,0,3);

    out.write("昨日之日不可留");

    out.flush();//字符流必须刷新缓冲区

    out.close();
}

综合案例:

完成文件的复制:

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

        File f1 = new File("D:/a.txt");
        File f2 = new File("D:/b.txt");
        if (!f2.exists()){
            f2.createNewFile();
        }

        FileReader fr = new FileReader(f1);
        FileWriter fw = new FileWriter(f2);

//        int n;
//        while ((n = fr.read()) != -1){
//            fw.write(n);
//        }
//
//        fw.close();
//        fr.close();

        char[] buffer = new char[10];
        int n;
        while ((n = fr.read(buffer)) != -1){
            fw.write(buffer,0,n);
        }

        fw.close();
        fr.close();
        
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值