java 查看文件的大小的方法

场景:查看文件的大小的方法(参考)

代码:

public class FileSizeTest {

    public static void main(String[] args) {
        String fileName = "E:\\TEXT.dat";
        File file = new File(fileName);
        long size = file.length();
        System.out.println("第一种方式获取文件的大小(字节):" + size);

        InputStream is = null;
        try {
            is = new FileInputStream(fileName);
            int fileSzie = is.available();
            System.out.println("第二种方式获取文件的大小(字节):" + fileSzie);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally{
            try {
                if(is != null){
                    is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

 

posted @ 2020-03-11 15:24  凌晨四点的少年  阅读(1106)  评论(0编辑  收藏  举报