开发工具与关键技术:MyEclipse 10 开发工具 Java
作者:罗中贤
撰写时间:2019-04-18
只是InputStream读取的是字节,使用的参数是byte数组(byte[])
package com.IO.demo;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
public class IOInputStreamDemo {
public static void main(String[] args) throws IOException {
try {
//创建流
InputStream in =new FileInputStream("E:\\test\\test.txt");
//创建缓存数据组
byte[] bs =new byte[1026];//临时存放读取数据
//存在读取的字节数
int count=0;
//读取数据
while ((count=in.read(bs,0,bs.length))!=-1) {
System.out.println(new String(bs,0,count,Charset.forName("UTF-8")));
}
//用完流关闭
in.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}