字节输入流:
1.FileInputStream
用途:从文件系统中的文件获得输入字节,常用于读取图像、声音等原始字节流,读取字符流可考虑使用FileReader
详细构造函数与常用方法可参考API文档,网上已经有中文版的API了,我是个E文盲,伤不起
这里介绍一个最常见的方法:
read(byte[] b, int off, int len)
从此输入流中将最多 len 个字节的数据读入一个 byte 数组中。
->off:b字节数组中的偏移量
小知识:数组偏移量,比如a[1,2,3,4,5]数组,默认数组第一个应该指向a[0],若偏移量为2,则指向a[1]
案例:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
int b;
// 创建输入流
FileInputStream readfile = new FileInputStream("E:\\test.txt");
byte buffer[] = new byte[2500];// 创建字节数组
// 从输入流中读取字节并存入buffer数组中,最长读取2500个字节,返回值b为实际读取的长度
b = readfile.read(buffer, 1, 2000);// 1为buffer数组中的起始偏移量
String str = new String(buffer, 0, b, "Default");
System.out.println(str);
System.out.println(b);
readfile.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
java中常见的输入输出流案例学习,布布扣,bubuko.com
原文地址:http://blog.csdn.net/u012453619/article/details/38647669