字节输入流

java.lang.Object

java.io.InputStream

通过InputStream可以从文件中或其他地方把需要的东西读取到缓存中

Method Summary

int available()
Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream.
void close()
Closes this input stream and releases any system resources associated with the stream.
void mark(int readlimit)
Marks the current position in this input stream.
boolean markSupported()
Tests if this input stream supports the mark and reset methods.
abstract int read()
Reads the next byte of data from the input stream.
int read(byte[] b)
Reads some number of bytes from the input stream and stores them into the buffer array b.
int read(byte[] b, int off, int len)
Reads up to len bytes of data from the input stream into an array of bytes.
void reset()
Repositions this stream to the position at the time the mark method was last called on this input stream.
long skip(long n)
Skips over and discards n bytes of data from this input stream.

public class ByteArrayInputStream

A ByteArrayInputStream contains an internal buffer that contains bytes that may be read from the stream. An internal counter keeps track of the next byte to be supplied by the read method.
Closing a ByteArrayInputStream has no effect. The methods in this class can be called after the stream has been closed without generating an IOException.

FileInputStream

将file文件中的内容读取到FileInputStream中

InputStream in =new FileInputStream(file);
in.read(byte)//Reads some number of bytes from the inputstream and stores them into the buffer array b. 
input.close

ByteArrayInputStream

Field Summary

protected byte[] buf
An array of bytes that was provided by the creator of the stream.
protected int count
The index one greater than the last valid character in the input stream buffer.
protected int mark
The currently marked position in the stream.
protected int pos
The index of the next character to read from the input stream buffer.

Constructor Summary

ByteArrayInputStream(byte[] buf)
Creates a ByteArrayInputStream so that it uses buf as its buffer array.
ByteArrayInputStream(byte[] buf, int offset, int length)
Creates ByteArrayInputStream that uses buf as its buffer array.

Method Summary
int available()
Returns the number of remaining bytes that can be read (or skipped over) from this input stream.
void close()
Closing a ByteArrayInputStream has no effect.
void mark(int readAheadLimit)
Set the current marked position in the stream.
boolean markSupported()
Tests if this InputStream supports mark/reset.
int read()
Reads the next byte of data from this input stream.
int read(byte[] b, int off, int len)
Reads up to len bytes of data into an array of bytes from this input stream.
void reset()
Resets the buffer to the marked position.
long skip(long n)
Skips n bytes of input from this input stream.

e.g

import java.io.ByteArrayInputStream;
import java.io.UnsupportedEncodingException;

public class dsa {
    public static void main(String[] args) throws Throwable {
        String s = "魔兽世界";
        byte[] b = s.getBytes("utf-8");
        byte[] b1 = new byte[1024];
        ByteArrayInputStream in = new ByteArrayInputStream(b);
        System.out.println(b);
        System.out.println(b1);
        System.out.println(in);
        in.read(b1);
        for (int i = 0; i < b.length; i++) {
            System.out.println(b[i]);
        }
        System.out.println(in);
        System.out.println(b1);
        for (int i = 0; i < b1.length; i++) {
            if (b1[i] != 0)
                System.out.println(b1[i]);
        }
        System.out.println(new String(b1, "utf-8"));
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值