Parsing of Java byte stream from file input and output to file

If you need to copy a picture, a word, a RAR package. You can read the file as a byte stream and then output it to the target folder.

Take copying a 4m picture as an example.

Read one byte at a time:

ch = (char)system.in.read(); //读入一个字符,返回读到的字节的int表示方式,读到末尾返回-1

When copying, read and write byte by byte is very slow. Setting a character array for buffering will make the copy process much faster (more bytes are read in each time).

It is easy to read, and the name of the class is described in Chinese

import java.io.*;
public class 字节流的缓冲区 {
  public static void main(String[] args) throws Exception {
    FileInputStream in=new FileInputStream("E:\\photo\\IMG.jpg");
    //FileOutputStream中的文件不存在,将自动新建文件
    OutputStream out=new FileOutputStream("E:\\test.jpg");
    byte[] buff=new byte[1024];
    int b;
    long beginTime=System.currentTimeMillis();
    while ((b=in.read(buff))!=-1) {
      out.write(buff,b);
    }
    long endTime=System.currentTimeMillis();
    System.out.println("运行时长为: "+(endTime-beginTime)+"毫秒");
    in.close();
    out.close();
    System.out.println("正常运行!");
  }
}

The byte array set here is 1024 bytes. The copy time is much faster than byte by byte.

//封装了FileOutputStream管道之后,三种函数参数
//write(b) 写入一个b
//write(byte[] b) 将字节数组全部写入
//write(byte[] b,int off,int len) 例如write(byteTest,len)表示数组byteTest中从0开始长度为len的字节
//一般都用第3个

Byte buffer stream

Use bufferedinputstream and bufferedoutputstream to encapsulate FileInputStream and fileoutputstream

It is easy to read, and the name of the class is described in Chinese

import java.io.*;
public class 字节缓冲流 {
  public static void main(String[] args) throws Exception {
    BufferedInputStream bis=new BufferedInputStream(new FileInputStream("E:\\photo\\IMG.jpg"));
    bufferedoutputstream bos=new bufferedoutputstream(new FileOutputStream("E:\\test.jpg"));
    int len;
    long begintime=System.currentTimeMillis();
    while((len=bis.read())!=-1) {
      bos.write(len);
    }
    long endtime=System.currentTimeMillis();
    System.out.println("运行时间为:"+(endtime-begintime)+"毫秒");
    bis.close();
    bos.close();
    System.out.println("正常运行");
  }
}

When an object of the string class is written to a file with a byte stream

import java.io.*;
public class outFile {
  public static void main(String[] args) throws Exception {
    FileOutputStream out=new FileOutputStream("example.txt");
    String str="测试";
    byte[] b=str.getBytes();
    for(int i=0;i<b.length;i++) {
      out.write(b[i]);
    }
    out.close();
    System.out.println("输出成功");
  }
}

When a file needs to be written in an additional form

FileOutputStream out=new FileOutputStream("example.txt",true);

Conversion flow

BufferedReader in = new BufferedReader(new InputStreamReader(system.in));

String x = in. read();

Inputstreamreader and outputstreamreader are conversion streams. The former converts byte stream to character stream, and the latter converts character stream to byte stream

The above is the whole content of this article. I hope it will help you in your study, and I hope you will support us a lot.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>