定义:InputStream这个抽象类是所有基于字节的输入流的超类,抽象了Java的字节输入模型。
第一个方法:public int available() throws IOException
方法解释:它一般用于在读入或者跳过之间先探测一下有多少可用字节。
白话理解:比如现在有一个.txt文件里面有adcd四个字节的话那么我们调用这个方法的话:返回值就是5
InputStream is = null;
is=new InputStream(".txt")
is.available()
返回的值就是5.
第二个方法: public abstract int read() throws IOException
方法解释:读取输入流的下一个字节。这是一个抽象方法,不提供实现,子类必须实现这个方法。该方法读取下一个字节,返回一个0-255之间的int类型整数。如果到达流的末端,返回-1.
InputStream is = null;
is=new InputStream(".txt")
is.read()
值返回一个0-255之间的int类型整数
第三个方法: public int read(byte b[]) throws IOException
方法解释:试图读入多个字节,存入字节数组b,返回实际读入的字节数。
如果传递的是一个空数组(注意数组长度可以为0,即空数组。比如 byte[] b = new byte[0]; 或者byte[] b = {};)那么什么也没读入,返回0.
如果到达流尾部,没有字节可读,返回-1
第四个方法:public int read(byte[] b,int off,int len) throws IOException
方法解释:这个方法跟上一个功能类似,除了读入的数据存储到b数组是从off开始。len是试图读入的字节数,返回的是实际读入的字节数。
如果len=0,则什么也不读入,返回0;如果遇到流尾部,返回-1.否则至少读入一个字节。
例如:
InputStream is = null;
byte[] buffer = new byte[6];
try {
is = new FileInputStream("test.txt");
is.read(buffer, 1, 3);
//System.out.println("available: " + is.available());
for (byte b : buffer) {
System.out.println((char)b);
}
解释一下为什么read()方法要重载这么多,我们在读出流的时候读一个字节写一个字节效率不高,如果我们可以将字节写入到一个字节缓存区的话,再等缓冲区满了去将缓冲区的数据读出,这样效率就提高了不少,所以说这就是为什么read有这么多重载。
有兴趣的话可以研究一下read源码:
public int read(byte b[], int off, int len) throws IOException {
if (b == null) { // 检测参数是否为null
throw new NullPointerException();
} else if (off < 0 || len < 0 || len > b.length - off) {
throw new IndexOutOfBoundsException(); // 数组越界检测
} else if (len == 0) {
return 0; //如果b为空数组,返回0
}
int c = read(); // 调用read()方法获取下一个字节
if (c == -1) {
return -1;
} // 遇到流尾部,返回-1
b[off] = (byte)c; //读入的第一个字节存入b[off]
int i = 1; // 统计实际读入的字节数
try {
for (; i < len ; i++) { // 循环调用read,直到流尾部
c = read();
if (c == -1) {
break;
}
b[off + i] = (byte)c; // 一次存入字节数组
}
} catch (IOException ee) {
}
return i; // 返回实际读入的字节数
}
最后一个方法:public void close() throws IOException
关闭当前流,释放与该流相关的资源,防止资源泄露。在带资源的try语句中将被自动调用。关闭流之后还试图读取字节,会出现IOException异常。
定义:OutputStream这个抽象类是所有基于字节的输出流的超类,抽象了Java的字节输出模型。
public abstract class OutputStream
顾名思义这个类也是一个抽象类,抽象类是不能被继承的.
public void close) throws IOException 关闭输出流;
public void flush() throws I0Exception 刷新缓冲区;
public void write(byte[] b)throws IOException 将一个byte数组写入数据流;
public void write(byte[]b ,int,off,int len)throws IOException 将一个指定范围的byte数组
方法与Inputstream方法类似这里就不作详细解释
在文件操作中和网络请求中通常会使用到流例子:
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(4000);
conn.setRequestProperty("Content-Type", "text/html; charset=UTF-8");
conn.setRequestMethod("GET");
conn.setRequestProperty("Range", "bytes=" + startW + "-" + endW);
Log.e("下载响应码" + threadId, conn.getResponseCode() + "");
InputStream in = conn.getInputStream();//获取网络数据流
RandomAccessFile raf = new RandomAccessFile(file, "rwd");//新建一个随机访问文件类
raf.seek(startW);
int length = 0;
byte[] buffer = new byte[1024];
int total = 0;
while (flag && (length = in.read(buffer)) != -1) {
RandomAccessFile rafMemory = new RandomAccessFile(filer, "rwd");
raf.write(buffer, 0, length);
total += length;
System.out.println("线程:" + threadId + "total:" + total+flag);
rafMemory.write(String.valueOf(total + startW).getBytes());
rafMemory.close();
synchronized (FileActivity.this) {
currentProgress += length;
spv.setProgress(currentProgress);
}
}
if (type.equals("4")) {
deletePath.setFileSide(currentProgress);
dbUtil.saveItem(deletePath);
}else {
deleteFile.setFileSide(currentProgress);
dbUtil.saveItem(deleteFile);
}
raf.close();
in.close();
System.out.println("线程" + threadId + "下载完毕");
} catch (IOException e) {
e.printStackTrace();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}