Java的字节流和字符流

InputStream用法

package io;

import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

/**
 * InputStream是字节流,抽象类,实现了Closeable接口
 * Closeable继承了 AutoCloseable 接口。 就可以使用 try-with-resource 语句进行关闭资源。
 * 学习子类:FileInputStream、ByteArrayInputStream
 *
 */
public class InputStreamTest {
    /**
     * 学习FileInputStream
     */
    public static void fileInputStreamTest() throws IOException {
        /* FileInputStream有三种构造函数
           1.FileInputStream(String name):会去new File(name)
           2.FileInputStream(File file)
           3.FileInputStream(FileDescriptor fdObj)
        */
        String fileName = "src/io/a.txt";
        // try() 中的资源可以自动关闭
        // int read() 读一个字节存储到返回值中, 正常范围是0-255,-1表示结束
        System.out.println("read():");
        try (FileInputStream fileInputStream = new FileInputStream(fileName);) {
            int a = 0;
            while ((a = fileInputStream.read()) != -1) {
                System.out.print((char)a);
            }
        }
        System.out.println("\nread(b):");
        // int read(byte b[]) 最多读b.length()的字节到b中,返回值表示读取到的字节数,-1表示结束
        try (FileInputStream fileInputStream = new FileInputStream(fileName);) {
            int len = 0;
            byte[] b = new byte[1024];
            while ((len = fileInputStream.read(b)) != -1) {
                System.out.print(new String(b, 0, len));
            }
        }
        System.out.println("\nread(b, off, len):");
        // int read(byte b[], int off, int len) 读最多len个字节到b中,从off开始存储,返回值表示读取到的字节数,-1表示结束
        try (FileInputStream fileInputStream = new FileInputStream(fileName);) {
            int len = 0;
            byte[] b = new byte[1024];
            while ((len = fileInputStream.read(b, 0, 1024)) != -1) {
                System.out.print(new String(b, 0, len));
            }
        }
        System.out.println("\nskip(n):");
        //long skip(long n)
        try (FileInputStream fileInputStream = new FileInputStream(fileName);) {
            int len = 0;
            byte[] b = new byte[3];
            while ((len = fileInputStream.read(b, 0, 3)) != -1) {
                System.out.print("[len:" + len + "]" + new String(b, 0, len));
                System.out.print("[" + fileInputStream.skip(2) + "]");
                System.out.println("[available:" + fileInputStream.available() + "]");
            }
        }
        //final FileDescriptor getFD()
        //FileChannel getChannel()
    }
    public static void byteArrayInputStreamTest() throws IOException {
        // 两种构造函数
        // ByteArrayInputStream(byte buf[])
        // ByteArrayInputStream(byte buf[], int offset, int length)
        // buf:存储数据的数组,pos:当前位置,count:总个数
        System.out.println("read():");
        byte[] b = new byte[]{'a', 'b', 'c'};
        try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(b)) {
            int a = 0;
            while((a = byteArrayInputStream.read()) != -1 ) {
                System.out.print((char)a);
            }
        }
        System.out.println("\nread(byte[] b):");
        byte[] dest = new byte[1024];
        try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(b)) {
            int len = 0;
            while((len = byteArrayInputStream.read(dest)) != -1 ) {
                System.out.print(new String(dest, 0, len));
            }
        }
        System.out.println("\nreadAllBytes:");
        try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(b)) {
            dest = byteArrayInputStream.readAllBytes();
            System.out.print(new String(dest, 0, dest.length));
            System.out.println("\nreadNBytes:");
            byteArrayInputStream.readNBytes(dest, 0, dest.length);
            System.out.print(new String(dest, 0, dest.length));
        }
        //long transferTo(OutputStream out)
        //skip 同上
        //available 同上
        //mask
        //reset
    }

    public static void main(String[] args) throws IOException {
        //fileInputStreamTest();
        byteArrayInputStreamTest();
    }
}

OutputStream用法

public class OutputStreamTest {
    public static void fileOutputStreamTest() throws IOException {
        String fileName = "src/io/b.txt";
        try (FileOutputStream fileOutputStream = new FileOutputStream(fileName);) {
            fileOutputStream.write('a');
            byte[] b = new byte[]{'b', 'c'};
            fileOutputStream.write(b);
        }
    }
    public static void main(String[] args) throws IOException {
        fileOutputStreamTest();
    }
}

Reader用法

public static void fileReaderTest() throws IOException {
        String fileName = "src/io/a.txt";
        try (FileReader fileReader = new FileReader(fileName)) {
            int a = 0;
            while ((a = fileReader.read()) != -1) {
                System.out.print((char)a);
            }
        }
        System.out.println("");
        try (FileReader fileReader = new FileReader(fileName)) {
            int len = 0;
            char[] b = new char[1024];
            while ((len = fileReader.read(b)) != -1) {
                System.out.print(new String(b, 0, len));
            }
        }

    }

Writer用法

public static void fileWriterTest() throws IOException {
        String fileName = "src/io/b.txt";
        try (FileWriter fileWriter = new FileWriter(fileName);) {
            fileWriter.write('a');
            char[] b = new char[]{'b', 'c'};
            fileWriter.write(b);
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值