15.3.1 InputStream和Reader

来源:互联网 发布:linux内核设计艺术pdf 编辑:程序博客网 时间:2024/06/10 19:51
import java.io.FileInputStream;import java.io.IOException;public class FileInputStreamTest {public static void main(String[] args) throws IOException {//创建字节输入流FileInputStream fis=new FileInputStream("abc.txt");//创建一个长度为1024的“竹筒"byte[] bbug=new byte[1024];//用于保存实际读取的字节数int hasRead=0;//存放读取的字节流String str=new String();//使用循环来重复“取水”过程while((hasRead=fis.read(bbug))>0){//取出“竹筒”中的水滴(字节),将字节数组转换称字符串输入System.out.println(new String(bbug, 0, hasRead));str=str+new String(bbug, 0, hasRead); }System.out.println("----------------------------------------- -字符串------------");System.out.println(str);//关闭文件输入流,放在finally块里更安全fis.close();}}

原创粉丝点击