java 字节流 FileOutputStream FileInputStream byte类型

一、FileOutputStream

1、概念:文件输出流,从内存到硬盘

2、实例化对象

注意:每次运行都会重新产生新的文件

//1.String 文件 字符串路径
new FileOutputStream(String);
//2.File 文件file句柄
new FileOutputStream(File);

3、写入方法

// 1. write(int b), 写入单个,b char类型
// 2. write(byte b[]), 写入byte数组,数组中的值是char类型 
// 3. write(byte b[], int off, int len),写入byte数组中的一部分,数组中的值是char类型,off索引,len是长度

4、追加写入

实例化文件输出对象时,第二个参数,append值为true

new FileOutputStream(string, true);

5、案例

package com.wt.bytes;

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

public class Output {
    public static void main(String[] args) {
        // 重写
        // method();
        // 追加写入
        // method1();
        // 中文输入
        method2();
    }

    private static void method2() {
        try {
            FileOutputStream ops = new FileOutputStream("module3\\3.txt");
            byte[] bytes = "窗前明月光,\n疑是地上霜,\n举头望明月,\n低头思故乡。".getBytes();
            ops.write(bytes);
            ops.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

    private static void method1() {
        try {
            FileOutputStream ops = new FileOutputStream("module3\\2.txt", true);
            // 字符串 转
            byte[] bytes = "Hello,World".getBytes();
            ops.write(bytes);
            ops.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private static void method() {
        // 文件输出流
        try {
            FileOutputStream ops = new FileOutputStream("module3\\1.txt");
            // 1. write(int b)
            // ops.write('a');
            // 2. write(byte b[])
            byte[] bytes = {'a', 'b', 'c', 'd'};
            // 3. write(byte b[], int off, int len)
            ops.write(bytes, 1, 2);
            ops.close();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

 二、FileInputStream

1、概念

文件输入流,硬盘=>内存

2、实例化对象

new FileInputStream(String);
new FileInputStream(File);

3、方法

// read(),读取单个
// read(byte b[]),读取数组,byte数据的长度一般设置为1024的倍数

4、案例

package com.wt.bytes;

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

public class Input {
    public static void main(String[] args) {
        // 读取单个
        // method();
        // 读取byte[]
        // method1();
        method3();
    }

    private static void method3() {
        try {
            FileInputStream fis = new FileInputStream("module3\\3.txt");
            byte[] bytes = new byte[1024];
            int len;
            while ((len=fis.read(bytes))!=-1){
                System.out.println(new String(bytes, 0, len));
            }
            fis.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private static void method1() {
        try {
            FileInputStream fis = new FileInputStream("module3\\2.txt");
            // 定义 bytes的长度,即一次读取的长度
            byte[] bytes = new  byte[4];
            // 循环
            int len;
            while ((len=fis.read(bytes))!=-1){
                // bytes 读取当前这段的值
                System.out.println(new String(bytes, 0, len));
            }

            fis.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private static void method() {
        try {
            FileInputStream fis = new FileInputStream("module3\\1.txt");
            int read;
            // 循环读取
            while ((read=fis.read())!=-1){
                System.out.println((char)read );
            }
            fis.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

注意:无论是文件输出流还是文件输入流,都需要close()

 

posted @ 2025-04-19 22:20  市丸银  阅读(45)  评论(0)    收藏  举报