java中InputStream的用法

来源:互联网 发布:电脑解压缩软件 编辑:程序博客网 时间:2024/06/05 17:54
/* *  * 功能:InputStream的用法 */package com.test2;import java.io.*;public class Demo2 {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubFile f=new File("d:\\hsp.txt");FileInputStream fis=null;try {//因为File没有读写的功能,所以需要使用InputStreamfis=new FileInputStream(f);//定义一个字节数组,相当于缓存byte []bytes=new byte[1024];int n=0;//得到实际读取到的字节数//循环读取try {while((n=fis.read(bytes))!=-1){//把字节转成StringString s=new String(bytes,0,n);System.out.println(s);}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} finally{//关闭文件流必须放这里try {fis.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}

0 0
原创粉丝点击