IO流

流是一种抽象概念,它代表了数据的无结构化传递。按照流的方式进行输入输出,数据被当成无结构的字节序或字符序列。从流中取得数据的操作称为提取操作,而向流中添加数据的操作称为插入操作。用来进行输入输出操作的流就称为IO流。换句话说,IO流就是以流的方式进行输入输出。

在这里插入图片描述

1、 输入流

在Java中,能够读取一个字节序列的对象就称作一个输入流。

1.1、字节输入流

  • InputStream类是字节输入流的抽象类,是所有字节输入流父类。
public abstract class InputStream implements Closeable {
  • 实现Closeable接口,继承了close方法。
 private static final int MAX_SKIP_BUFFER_SIZE = 2048;
  • MAX_SKIP_BUFFER_SIZE用于确定最大缓冲区大小
  • 跳过时使用。
public abstract int read() throws IOException;
  • 读取输入流中下一个字节,返回读取到的字节,如果输入流到底了,就返回 - 1。
public int read(byte b[]) throws IOException {
        return read(b, 0, b.length);
    }
  • 读取输入流中的字节,将字节写入字节数组b
  • 返回实际读取的字节数。
  • 如果输入流读到底了,就返回 - 1。
public int read(byte b[], int off, int len) throws IOException {
        if (b == null) {
            throw new NullPointerException();
        } else if (off < 0 || len < 0 || len > b.length - off) {
            throw new IndexOutOfBoundsException();
        } else if (len == 0) {
            return 0;
        }

        int c = read();
        if (c == -1) {
            return -1;
        }
        b[off] = (byte)c;

        int i = 1;
        try {
            for (; i < len ; i++) {
                c = read();
                if (c == -1) {
                    break;
                }
                b[off + i] = (byte)c;
            }
        } catch (IOException ee) {
        }
        return i;
    }
  • 读取输入流中第off位置的字节开始
  • 读取len个字节
  • 存储到字节数组 b 中
  • 返回实际读取的字节数
 public long skip(long n) throws IOException {

        long remaining = n;
        int nr;

        if (n <= 0) {
            return 0;
        }

        int size = (int)Math.min(MAX_SKIP_BUFFER_SIZE, remaining);
        byte[] skipBuffer = new byte[size];
        while (remaining > 0) {
            nr = read(skipBuffer, 0, (int)Math.min(size, remaining));
            if (nr < 0) {
                break;
            }
            remaining -= nr;
        }

        return n - remaining;
    }
  • 从输入流的当前位置往后跳 n 个字节
  • 返回的是实际跳了多少字节,因为也许输入流中剩下的字节数小于n,意味着跳过的字节就无法获取了,除非用mark 和 reset
 public int available() throws IOException {
        return 0;
    }
  • 返回下一次从此输入流中可以不阻塞地读取多少字节的数据
  • 不同的输入流类有不同的实现,有些是返回输入的字节总数
 public void close() throws IOException {}
  • 关闭输入流方法
public synchronized void mark(int readlimit) {}
  • 待续
   public synchronized void reset() throws IOException {
        throw new IOException("mark/reset not supported");
    }
  • 待续
public boolean markSupported() {
        return false;
    }
  • 判断是否支持 mark() 和 reset() 功能
  • 支持返回true,否则false

1.2、代码

  • 新建一个txt文件,读此文件,并打印
  InputStream fileInputStream = new FileInputStream("src/main/java/io1.txt");
  //返回读取数据的长度
  int available = fileInputStream.available();
  char[] chars = new char[available];
  for(int i=0;i<available;i++){
      chars[i]= (char)fileInputStream.read();}
  for (char a:chars) {
        System.out.print(a); }
  fileInputStream.close();
  • 数据用字符数据接收。若是中文则乱码
  • 解决方案:将接受的数组转化为String类型,并打印
InputStream fis = new FileInputStream("src/main/java/io.txt");
byte[] b = new byte[1024];
int len ;
len= fis.read(b);
String s = new String(b, 0, len);
System.out.println(s);
fis.close();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值