文件的读取

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;

//一次读取多个字节
public class Demo02InPutStream {
    public static void main(String[] args) throws IOException {
        FileInputStream fis=new FileInputStream("F:\\basic\\untitled13\\src\\it\\cast\\day15\\demo01\\1.txt");

     /* byte[] bytes=new byte[2];//起到缓冲作用,存储每次读取到的多个字节

      int len=fis.read(bytes);
        System.out.println(Arrays.toString(bytes));
        System.out.println(new String(bytes));*/
        byte[] bytes1=new byte[1024];
        int len=0;
        while ((len=fis.read(bytes1))!=-1){
            
            System.out.println(new String(bytes1,0,len));//读取有效的几个
        }


        fis.close();
    }
}

 

 

import java.io.FileInputStream;
import java.io.IOException;

public class Demo01InPutStream {
    public static void main(String[] args) throws IOException {
        FileInputStream fis=new FileInputStream("F:\\basic\\untitled13\\src\\it\\cast\\day15\\demo01\\2.txt");
        int read=0;
        while ((read=fis.read())!=-1) {//read运行一次跳转一次
            System.out.print((char) read);
        }

        fis.close();
    }
}

 

posted @ 2020-10-06 17:14  159566  阅读(77)  评论(0编辑  收藏  举报