My name is byte stream, I am a machine without emotions!

Byte stream

Insert picture description here

FileOutStream

Construction method

FileOutputStream​(File file) Create a file output stream to write the file represented by the specified File object.
FileOutputStream​(FileDescriptor fdObj) creates a file output stream to be written to the specified file descriptor, which represents an existing connection to the actual file in the file system.
FileOutputStream​(File file, boolean append) Create a file output stream to write the file represented by the specified File object.
FileOutputStream​(String name) Create a file output stream to write a file with the specified name.
FileOutputStream​(String name, boolean append) Create a file output stream to write a file with the specified name.

Common method

void close() Close this file output stream and release all system resources associated with this stream.
FileChannel getChannel() Returns the unique FileChannel object associated with this file output stream.
FileDescriptor getFD() Returns the file descriptor associated with this stream.
void write​(byte[] b) Write the b.length bytes in the specified byte array to this file output stream.
void write​(byte[] b, int off, int len) Write the len bytes in the specified byte array starting at offset off to this file output stream.
void write​(int b) Write the specified byte to this file output stream.

Implementation

package work.february.two.daily;

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

/**
 * @Author: 小浪
 * @Description:
 * @Date Created in 2021-02-02 17:32
 * @Modified By:
 */
public class Demo4 {
    
    
    public static void main(String[] args) throws IOException {
    
    
        FileOutputStream fileOutputStream =new FileOutputStream("D://a.txt");
        //int 类型
        fileOutputStream.write(65);
        //byte []类型
        byte [] bytes ={
    
    65,66,67,68};
        fileOutputStream.write(bytes);
        fileOutputStream.write(bytes,1,2);
        System.out.println("已经写出");
        fileOutputStream.close();
        //当不是同一个对象时 需要追加 true是追加
        FileOutputStream fileOutputStream1 =new FileOutputStream("D://a.txt",true);
        fileOutputStream1.write(100);
        fileOutputStream1.close();
    }
}

FileInputStream

Constructor

FileInputStream​(File file) creates a FileInputStream by opening a connection with the actual file.
The file is named by the File object file in the file system. FileInputStream​(FileDescriptor fdObj)
uses the file descriptor fdObj to create a FileInputStream, which represents an existing connection to the actual file in the file system.
FileInputStream​(String name) creates a FileInputStream by opening a connection with the actual file.
The file is named by the path name name in the file system.

Common method

int available() returns an estimate of the number of remaining bytes that can be read (or skipped) from this input stream without being blocked by the next method calling this input stream.
void close() Close this file input stream and release all system resources associated with the stream.
FileChannel getChannel() Returns the unique FileChannel object associated with this file input stream.
FileDescriptor getFD() returns the FileDescriptor object, which represents the connection to the actual file in the file system being used by this FileInputStream.
int read() Reads one byte of data from this input stream.
int read​(byte[] b) Read the b.length byte array from this input stream b.length up to b.length bytes of data.
int read​(byte[] b, int off, int len) Read data from the input stream len up to len bytes into a byte array.
long skip​(long n) skip and discard n bytes of data from the input stream.

Implementation:

package work.february.two.daily;

import java.io.FileInputStream;
import java.io.IOException;

/**
 * @Author: 小浪
 * @Description:
 * @Date Created in 2021-02-02 18:25
 * @Modified By:
 */
public class Demo5 {
    
    
    public static void main(String[] args) throws IOException {
    
    
        FileInputStream fileInputStream =new FileInputStream("D:\\a.txt");
        //一个字节一个字节读 读到末尾是 -1
        while (true){
    
    
            byte b = (byte) fileInputStream.read();
            if(b == -1){
    
    
                break;
            }else {
    
    
                System.out.println((char)b);
            }

        }
        fileInputStream.close();
    }
}

A set of reading is more reliable

package work.february.two.daily;

import java.io.FileInputStream;
import java.io.IOException;

/**
 * @Author: 小浪
 * @Description:
 * @Date Created in 2021-02-02 19:27
 * @Modified By:
 */
public class Demo6 {
    
    
    public static void main(String[] args) throws IOException {
    
    
        //一组字节 读取
        FileInputStream fileInputStream =new FileInputStream("D://a.txt");
        byte [] bytes =new byte[5];
        int len =fileInputStream.read(bytes);
        System.out.println(new String(bytes,0,len));
         len =fileInputStream.read(bytes);
        System.out.println(new String(bytes,0,len));
        fileInputStream.close();
    }
}

Insert picture description here
Nonsense time:

If the customer thinks that the food is appropriate, can you give a free like! Thank you! Slow walker officer! It is recommended to pack and collect and come again next time. Shop Xiaoer QQ: 309021573, welcome to harass!

Guess you like

Origin blog.csdn.net/AzirBoDa/article/details/113571439