JAVA-IO流-FileInputStream

//每次读取一个字节
try {
    //文件输入流的对象一旦生成,那么这个流就是打开的。
    FileInputStream fis = new FileInputStream("1.txt");
    //每次只能读取一个字节 也有一个类似游标的东西。
    //如果read 方法返回的是-1 表明该流读到末尾了。
    byte temp[] = new byte[4];
    int result = fis.read();
    //len 指的是 read 方法一次读取到的字节的数量。读取到末尾返回-1
//          int len = fis.read(temp);

    while (result != -1){
        System.out.print((char)result);
        result = fis.read();
    }

    fis.close();

} catch (Exception e) {
    e.printStackTrace();
}


//每次读取多个字节
public static void main(String[] args) {
    try {
        //文件输入流的对象一旦生成,那么这个流就是打开的。
        FileInputStream fis = new FileInputStream("1.txt");
        //每次只能读取一个字节 也有一个类似游标的东西。
        //如果read 方法返回的是-1 表明该流读到末尾了。
        byte temp[] = new byte[4];
//          char result = (char)fis.read();
        //len 指的是 read 方法一次读取到的字节的数量。读取到末尾返回-1
        int len = fis.read(temp);

        while (len != -1){
            System.out.print(new String(temp,0,len));
//              result = (char)fis.read();
            len = fis.read(temp);
        }

        fis.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
}




猜你喜欢

转载自blog.csdn.net/qq_37131111/article/details/79678251