java基础:java字节流、字符流。

java字节流

  计算机中,文本、音频、视频等文件都是以二进制的形式存在,对于字节的输入、输出IO流提供了一系列的流,统称为字节流。
  根据数据传输方向可分为字节输入流、字节输出流。

输 入
输 出
磁 盘
内 存

两个抽象类:InputStream、OutputStream
  它们是字节流的顶级父类(抽象类不能被实例化)。
  所有的字节输入流都继承InputStream。
  所有的字节输出流都继承OutputStream。

InputStream
OutputStream
程序
程序
目标

一、输入流相关读写方法:

int read()	//从输入流读取一个8位的字节,转换成0~255之间的整数。

int read(byte [] b) //从输入流读取若干字节,保存到字节数组中,返回的整数表示读取字节的数目

//从输入流读取若干字节,保存到指定的字节数组中,
//off指定字节数组开始保存数据的起始下标, len表示读取的字节数目
int read(byte[] b,int off,int len)

void close()//关闭此输入流并释放与该流关联的所有系统资源

InputStream相关子类:

InputStream
ByteArrayInputStream
FileInputStream
FilterInputStream
PipedInputStream
SequenceInputStream
ObjectInputStream
BufferedInputStream
PrintStream
DataInputStream

  常用:FileInputStream、BufferedInputStream

输入流基本使用案例:

  1. 字节输入流:FileInputStream:
    public static void main(String[] args) throws IOException {
        FileInputStream in = null;
        try {
            in = new FileInputStream("src\\a.txt");
            int b = 0;
            while(true){
                b = in.read();//一次读一个字节
                if (b == -1){//没有数据了返回-1
                    break;
                }
                System.out.print((char)b);
            }
        } catch (Exception e){
            e.printStackTrace();
        } finally {
            if (in != null){
                in.close();
            }
        }
    }
  1. 字符输入流:FileReader

public static void main(String[] args) throws IOException {
//字符输入流读数
FileReader fr = new FileReader(“src/a.txt”);

int len ;

while((len = fr.read()) != -1){
    System.out.print((char)len);
}
fr.close();

}
3. 带缓冲区的字符输入流:BufferedReader

    public static void main(String[] args) throws IOException {
        FileReader fr = new FileReader("src/a.txt");
        BufferedReader br = new BufferedReader(fr);
        String line;
        while ( (line = br.readLine())!=null){
            System.out.println(line);
        }

        br.close();
    }

二、输出流相关读写方法:

void write(int b)//向输出流写入一个字节。

void write(byte[] b)//把参数b指定的字节数组的所有字节写到输出流。

void write(byte [] b,int off,int len)//将指定byte数组从偏移量off开始的len个字节写入输出流。

void flush()//刷新此输出流并强制写出所有缓冲的输出字节。

void close()//关闭此输出流并释放与此流相关的所有系统资源。

OutputStream相关子类:

OutputStream
ByteArrayOutputStream
FileOutputStream
FilterOutputStream
PipedOutputStream
ObjectOutputStream
BufferedOutputStream
PrintStream
DataOutputStream

  常用:FileOutputStream 、BufferedOutputStream
输出流基本使用案例:

  1. 字节输出流FileOutputStream:
public static void main(String[] args) throws IOException {//此处抛出异常,一般会用try catch处理
    //创建 FileOutputStream 对象,文件不存在则创建
    FileOutputStream out = new FileOutputStream("example.txt",true);//带true参数是追加,不带是覆盖
    String str = "world";

    byte[] b = str.getBytes();//转成字节数组
    for (int i = 0;i<b.length;i++){
        out.write(b[i]);
    }
    out.close();
}
  1. 字符输出流:FileWriter
public static void main(String[] args) throws IOException {
    //字符输出流
    FileWriter fw = new FileWriter("c.txt",true);//true追加
    fw.write("hello 耶");
    fw.write("\r\n");
    fw.close();
}
  1. 带缓冲区的字符输出流:
public static void main(String[] args) throws IOException {
    Writer writer = new FileWriter("d.txt");

    BufferedWriter bw = new BufferedWriter(writer);

    bw.write("hello 耶1");
    bw.newLine();
    bw.newLine();
    bw.write("hello 耶2");
    bw.flush();
    bw.close();
}

三、输出、输出流结合使用:

  1. 字节流复制文件:InputStream、OutputStream
    一次操作一个字节(输入流读,输出流写)
	    public static void main(String[] args) throws IOException {
        //文件复制,一次一个字节(输入流读,输出流写)
        InputStream in = new FileInputStream("src/a.txt");
        OutputStream out = new FileOutputStream("target/b.txt");

        int len;//记录读取到的数据
        long bTime = System.currentTimeMillis();
//        while(true){
//            len = in.read();
//            if (len!=-1){
//                out.write(len);
//            }else {
//                break;
//            }
//        }
        while ((len = in.read()) != - 1){
            out.write(len);
        }

        long eTime = System.currentTimeMillis();
        System.out.println("Time:"+(eTime-bTime)+"毫秒");

        in.close();
        out.close();

    }

使用缓冲区,一次一个字节数组,比读取一个字节效率更高。
定义一个缓冲字节数组接收。

public static void main(String[] args) throws IOException {
    //文件复制,一次一个字节数组。比读取一个字节效率更高
    InputStream in = new FileInputStream("src/a.txt");
    OutputStream out = new FileOutputStream("target/b.txt");

    byte[] buff = new byte[1024];//承当缓冲区,一般1024整数倍
    long bTime = System.currentTimeMillis();
    //int len = in.read(brr);//读取到的有效字节个数,没有数据返回-1
    int len;
    while ((len  = in.read(buff)) != -1){
        out.write(buff,0,len);
    }
    long eTime = System.currentTimeMillis();
    System.out.println("Time:"+(eTime-bTime)+"毫秒");
    in.close();
    out.close();

}
  1. 字节缓冲流复制文件:BufferedInputStream、BufferedOutputStream(读写数据时已经提供了缓冲功能)。
public static void main(String[] args) throws IOException {
    //字节缓冲流
    BufferedInputStream bis = new BufferedInputStream(new FileInputStream("src/a.txt"));
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("target/c.txt"));
    long bTime = System.currentTimeMillis();

    //进行读写
    int len;
    while ((len = bis.read()) != -1){
        bos.write(len);
    }

    long eTime = System.currentTimeMillis();
    System.out.println("Time:"+(eTime-bTime)+"毫秒");

    bis.close();
    bos.close();
}
  1. 字符流复制文件:FileReader 、Writer 、BufferedReader 、BufferedWriter
public static void main(String[] args) throws IOException {
    FileReader fr = new FileReader("src/a.txt");
    BufferedReader br = new BufferedReader(fr);

    Writer writer = new FileWriter("d.txt");

    BufferedWriter bw = new BufferedWriter(writer);

    String str;
    while((str = br.readLine()) != null){
        bw.write(str);
        bw.newLine();
    }

    br.close();
    bw.close();
}
  1. 转换流复制文件:InputStreamReader 、OutputStreamWriter
    转换流:字节流与字符流之间的转换。
public static void main(String[] args) throws IOException {
        //转换流
        FileInputStream in = new FileInputStream("src/a.txt");
        InputStreamReader isr = new InputStreamReader(in);

        FileOutputStream out = new FileOutputStream("d.txt");
        OutputStreamWriter osw = new OutputStreamWriter(out);

        int ch ;
        while ((ch = isr.read())!=-1){
            osw.write(ch);
        }
        isr.close();
        osw.close();

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值