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();
            }
        }
    }
}
原文地址:https://www.cnblogs.com/liangxiaojin/p/12462643.html