Loading

[Java手撕]读取指定字节的数据

public class Main {

    public static void main(String[] args) throws InterruptedException, FileNotFoundException {

        try(FileInputStream file = new FileInputStream("src/example.txt")) {
            byte[] buffer = new byte[1024];
            int bytes = -1;
            while((bytes = file.read(buffer)) != -1){
                System.out.println("本次读取到"+bytes+"字节");
                for(int i = 0;i<bytes;i++){
                    System.out.println((char)buffer[i]);
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
posted @ 2024-09-28 22:18  Duancf  阅读(39)  评论(0)    收藏  举报