一、输入流(InputStream)的API
// 读取一个字节,并将它返回
- int read()
// 将数据读入一个字节数组,同时返回读取的字节数
- int read(byte[] buffer)
// 将数据读入一个字节数组,放到数组的offset指定的位置开始,并用length来指定读取的最大字节数
- int read(byte[] buffer, int offset, int length)
// 关闭流
- void close()
// 返回可以从中读取的字节数
- int available()
// 在输入流中跳过n个字节,将实际跳过的字节数返回
- long skip(long n)
二、编程实战
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
* User: 祁大聪
*/
public class S25 {
public static void main(String[] args) {
try {
FileInputStream fis = new FileInputStream("E://25.txt");
System.out.println(fis.available());
byte[] buffer = new byte[1024];
// fis.read(buffer, 20, 10);
// System.out.println(buffer[19]);
// System.out.println(new String(buffer));
fis.skip(10);
while(fis.read(buffer) != -1){
System.out.println(new String(buffer));
}
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}