JAVA single row diary -2020 / 1/23-IO stream - byte stream

Here Insert Picture Description

1.IO Flow Overview

Here Insert Picture Description

2. The OutputStreamoutput stream of bytes

OutputStreamClass is abstract and can not be used directly, the need to create subclasses

2.1 FileOutputStreamfile output stream of bytes

Here Insert Picture Description

  1. Role: theRAMThe databyteThe way to writeHard disk filein
  2. Construction method:

effect:

  • Create an FileOutputStreamObject
  • The method of construction of the file / path, creating an empty file
  • We will FileOutputStreamobject points to good empty file creation
FileOutputStream(String name) 写入数据的目的地是一个文件的路径
FileOutputStream(File file)  写入数据的目的地是一个文件
  • Step output stream of bytes used
  1. Create a create a FileOutputStreamobject constructor to fill in the destination of write data
FileOutputStream fos01 = new FileOutputStream("G:\\Java\\测试文件夹\\a.txt");
  1. Create a call FileOutputStreamobject writemethods, writes the data to a file
fos01.write(12);
  1. Release resources
fos01.close();

Here Insert Picture Description

  • Time to write more bytes methods

Method a: write the entire byte array

byte[] b = {97,98,99,100};
fos01.write(b);

Here Insert Picture Description

If the first byte is a positive number (0-127), then the time will display the ASCII table query
if the first byte is negative, then the first byte and the second byte will form a Chinese, when will query the system displays the default code table

Method two: writing a portion of a byte array

byte[] b = {97,98,99,100,121};
fos01.write(byte[] b,int off,int leng); 写入字节数组b的从off开始,长度为leng的数据
package FileOutputStream;

import java.io.FileOutputStream;
import java.io.IOException;

public class Demo01 {
    public static void main(String[] args) throws IOException {
        FileOutputStream fos01 = new FileOutputStream("G:\\Java\\测试文件夹\\b.txt");
        byte[] b = {97,98,99,100,121};
        fos01.write(b);
        fos01.write(b,1,3);
        fos01.close();
    }
}

Here Insert Picture Description
Act III:

Using the getBytes()method, the string into a byte array

package FileOutputStream;

import java.io.FileOutputStream;
import java.io.IOException;

public class Demo02 {
    public static void main(String[] args) throws IOException {
        FileOutputStream fos = new FileOutputStream("G:\\Java\\测试文件夹\\c.txt");
        byte[] b = "你好".getBytes();
        fos.write(b);
        fos.close();
    }
}

Here Insert Picture Description

  • Additional writing data (writing) and line feed

  • Writing
    the construction method:

FileOutputStream(String name,boolean append) 写入数据的目的地是一个文件的路径,append为true则不会覆盖原文件,继续在原文件中续写,append为false则覆盖原文件
FileOutputStream(File file,boolean append)  写入数据的目的地是一个文件,append为true则不会覆盖原文件,继续在原文件中续写,append为false则覆盖原文件
  • Wrap: Write a newline

windows:\r\n
Linux:/n
Mac:/r

3. The InputStreaminput stream of bytes

InputStreamClass is abstract and can not be used directly, the need to create subclasses

3.1 FileInputStreamFile input stream of bytes

Here Insert Picture Description

  1. Role: theHard disk fileThe databyteThe way to readRAMin
  2. Construction method:

effect:

  • Create an FileInputStreamObject
  • Will FileInputStreamobject to a file read
FileInputStream(String name) 读取文件的数据源是一个文件的路径
FileInputStream(File file)  读取文件的数据源是一个文件
  • Byte input stream using the steps
  1. Create a create a FileInputStreamobject constructor to fill in the data source to read the file
FileInputStream fos = new FileInputStream("G:\\Java\\测试文件夹\\c.txt");
  1. Create a call FileInputStreamobject read()methods, read the file
fos.read();   读取文件中的一个字节并返回,并且将指针后移一位,读取到文件的末尾返回-1
  1. Release resources
fos.close();
import java.io.FileInputStream;
import java.io.IOException;

public class Demo02 {
    public static void main(String[] args) throws IOException {
        FileInputStream fos = new FileInputStream("G:\\Java\\测试文件夹\\c.txt");

        fos.read();
        System.out.println(fos.read());

        fos.read();
        System.out.println(fos.read());

        fos.read();
        System.out.println(fos.read());

        fos.read();
        System.out.println(fos.read());

        fos.close();
    }
}

Here Insert Picture Description

  • Input stream of bytes read cycle all bytes
FileInputStream fos = new FileInputStream("G:\\Java\\测试文件夹\\a.txt");
int len = 0;
while ((len = fos.read()) != -1) {
    System.out.println(len);
}

Here Insert Picture Description

Not
the while (fos.read () = -. 1!) {
System.out.println (fos.read ());
}
since each execution fos.read (), the pointer will move after a
Here Insert Picture Description

  • The method of reading a plurality of bytes
 FileInputStream file = new FileInputStream("G:\\Java\\测试文件夹\\a.txt");
 byte[] bytes = new byte[2]; 创建一个字节数组,用于盛放多个字节
 int len = file.read(bytes); 读取字节数组容量的字节个数,返回值为字节长度
 System.out.println(len);    打印字节长度
 System.out.println(Arrays.toString(bytes));  将数组按照默认格式变成字符串
 System.out.println(new String(bytes));  创建字符串三种构造方法之一,根据字节数组的内容,来创建字符串

example:

package FileInputStream;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;

public class Demo01 {
    public static void main(String[] args) throws IOException {
        FileInputStream file = new FileInputStream("G:\\Java\\测试文件夹\\a.txt");

        byte[] bytes1 = new byte[2];
        int len1 = file.read(bytes1);

        System.out.println(len1);
        System.out.println(new String(bytes1));
        System.out.println("============================");


        byte[] bytes2 = new byte[2];
        int len2 = file.read(bytes2);

        System.out.println(len2);
        System.out.println(new String(bytes2));
        System.out.println("============================");



        byte[] bytes3 = new byte[2];
        int len3 = file.read(bytes3);

        System.out.println(len3);
        System.out.println(new String(bytes3));
        System.out.println("============================");



        byte[] bytes4 = new byte[2];
        int len4 = file.read(bytes4);

        System.out.println(len4);
        System.out.println(new String(bytes4));
    }
}

Here Insert Picture Description
Here Insert Picture Description

  • principle:
    Here Insert Picture Description

  • All bytes read cycle optimization:

byte[] bytes = new byte[1024];
int len = 0;
while ((len=file.read(bytes))!=-1){
    System.out.println(new String(bytes,0,len));
}
package FileInputStream;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;

public class Demo01 {
    public static void main(String[] args) throws IOException {
        FileInputStream file = new FileInputStream("G:\\Java\\测试文件夹\\a.txt");

/*        byte[] bytes1 = new byte[2];
        int len1 = file.read(bytes1);

        System.out.println(len1);
        System.out.println(new String(bytes1));
        System.out.println("============================");


        byte[] bytes2 = new byte[2];
        int len2 = file.read(bytes2);

        System.out.println(len2);
        System.out.println(new String(bytes2));
        System.out.println("============================");



        byte[] bytes3 = new byte[2];
        int len3 = file.read(bytes3);

        System.out.println(len3);
        System.out.println(new String(bytes3));
        System.out.println("============================");



        byte[] bytes4 = new byte[2];
        int len4 = file.read(bytes4);

        System.out.println(len4);
        System.out.println(new String(bytes4));*/
        byte[] bytes = new byte[1024];
        int len = 0;
        while ((len=file.read(bytes))!=-1){
            System.out.println(new String(bytes,0,len));
        }

    }
}

Here Insert Picture Description

Published 103 original articles · won praise 1 · views 2652

Guess you like

Origin blog.csdn.net/wangzilong1995/article/details/104074119