黑马程序员技术交流社区

标题: 一次一个字节 [打印本页]

作者: China_Riven    时间: 2015-7-16 13:26
标题: 一次一个字节
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class Demofile2 {

        /**
         * @param args
         * @throws IOException
         */
        public static void main(String[] args) throws IOException {
                // TODO Auto-generated method stub
                method();
                method2();
        }
        //一次一个字节
        public static void method() throws IOException {
                FileInputStream fis = new FileInputStream("dema.txt");
                //输入数据
                //定义变量,临时记录读取的这个字节
                int c;
                while((c=fis.read())!=-1){
                        System.out.println(c);
                }
                //关闭流
                fis.close();
        }

//一次一个字节数组
public static void method2() throws IOException{
        //创建流对象
        FileInputStream fis = new FileInputStream("riven.txt");
        //输入数据
        byte [] bytes = new byte[1024];
        //定义变量,临时记录读取到的字节的个数
        int len;
        while((len= fis.read(bytes))!=-1){
                String s = new String(bytes ,0 ,len);
                System.out.println(s);
               
        }
        fis.close();
}
}


作者: China_Riven    时间: 2015-7-16 13:27
自己顶一下




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2