0
点赞
收藏
分享

微信扫一扫

FileInputStream的读取操作(Java IO相关内容)

春意暖洋洋 2022-02-22 阅读 55
import org.junit.jupiter.api.Test;

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

public class q13{
    public static void main(String[] args) {
        String path = "D:\\Demo1.txt";
        File file = new File(path);
        try{
            file.createNewFile();
        }catch (Exception e){
            e.printStackTrace();
        }


    }
    @Test
    void Dic(){
        String path = "D://C";
        File file = new File(path);
        if (!file.exists()){
            file.mkdirs();
        }
    }

    @Test
    void DeleteDic(){
        String path = "D://C";
        File file = new File(path);
        if (file.exists()){
            file.delete();
            System.out.println("删除成功");

        }    }
    @Test
    void FlieInputStreamTest(){
        String path = "D:\\Demo1.txt";
        FileInputStream fileInputStream = null;
        int read = 0;
        byte[] b =new byte[2];
        try {
            //这个地方需要注意,存到byte的数组中,那么每次要取走数组中新进入的值,比如第一次取满。
            // 但是第二次取一半,那么后半段的数组内容是没有更新的,就不应该读取
            fileInputStream = new FileInputStream(path);
            while((read = fileInputStream.read(b))!=-1){//这个read是有一个指针的,每次都会后防,所以读字符只能每个字符读一次;
                System.out.println( new String(b,0,read) );
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally{
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
举报

相关推荐

0 条评论