我叫字节流,我是没有感情的机器!

字节流

在这里插入图片描述

FileOutStream

构造方法

FileOutputStream​(File file) 创建文件输出流以写入由指定的 File对象表示的文件。
FileOutputStream​(FileDescriptor fdObj) 创建要写入指定文件描述符的文件输出流,该文件描述符表示与文件系统中实际文件的现有连接。
FileOutputStream​(File file, boolean append) 创建文件输出流以写入由指定的 File对象表示的文件。
FileOutputStream​(String name) 创建文件输出流以写入具有指定名称的文件。
FileOutputStream​(String name, boolean append) 创建文件输出流以写入具有指定名称的文件。

常用方法

void close() 关闭此文件输出流并释放与此流关联的所有系统资源。
FileChannel getChannel() 返回与此文件输出流关联的唯一FileChannel对象。
FileDescriptor getFD() 返回与此流关联的文件描述符。
void write​(byte[] b) 将指定字节数组中的 b.length字节写入此文件输出流。
void write​(byte[] b, int off, int len) 将从偏移量 off开始的指定字节数组中的 len字节写入此文件输出流。
void write​(int b) 将指定的字节写入此文件输出流。

具体实现

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

构造器

FileInputStream​(File file) 通过打开与实际文件的连接来创建 FileInputStream
该文件由文件系统中的 File对象 file命名。 FileInputStream​(FileDescriptor fdObj)
使用文件描述符 fdObj创建 FileInputStream ,该文件描述符表示与文件系统中实际文件的现有连接。
FileInputStream​(String name) 通过打开与实际文件的连接来创建 FileInputStream
该文件由文件系统中的路径名 name命名。

常用方法

int available() 返回可以从此输入流中读取(或跳过)的剩余字节数的估计值,而不会被下一次调用此输入流的方法阻塞。
void close() 关闭此文件输入流并释放与该流关联的所有系统资源。
FileChannel getChannel() 返回与此文件输入流关联的唯一FileChannel对象。
FileDescriptor getFD() 返回 FileDescriptor对象,该对象表示与此 FileInputStream正在使用的文件系统中的实际文件的连接。
int read() 从此输入流中读取一个字节的数据。
int read​(byte[] b) 从此输入流 b.length最多 b.length字节的数据读 b.length字节数组。
int read​(byte[] b, int off, int len) 从此输入流 len最多 len字节的数据读入一个字节数组。
long skip​(long n) 跳过并从输入流中丢弃 n字节的数据。

实现方法:

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();
    }
}

一组读更加靠谱

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();
    }
}

在这里插入图片描述
废话时间:

如果客官觉得食用合适可不可以给一个免费的赞!谢谢谢了!慢走客官!建议打包收藏,下次再来。店小二QQ:309021573,欢迎骚扰!

猜你喜欢

转载自blog.csdn.net/AzirBoDa/article/details/113571439