Java File Class long length()方法与示例

文件类长length() (File Class long length())

  • This method is available in package java.io.File.length().

    软件包java.io.File.length()中提供了此方法。

  • This method is used to return the length of the file in bits which is represented by the filepath.

    此方法用于返回文件长度(以文件路径表示)。

  • This method returns the length of the file and in case if the file does not exist or empty file or exception occurs then it will return 0L.

    此方法返回文件的长度,如果文件不存在或为空文件或发生异常,则它将返回0L 。

Syntax:

句法:

    long length(){
    }

Parameter(s):

参数:

We don't pass any object as a parameter in the method of the File.

我们不会在File方法中将任何对象作为参数传递。

Return value:

返回值:

The return type of this method is long, which is the length of the file in bits.

此方法的返回类型为long ,这是文件的长度(以位为单位)。

Java程序演示length()方法的示例 (Java program to demonstrate example of length() method)

// import the File class because we will use File class methods
import java.io.File;

// import the Exception class because it may raise an exception 
// when working with files
import java.lang.Exception;
public class FileLength {
    public static void main(String[] args) {
        try {
            // Specify the path of file and we use double slashes to 
            // escape '\' character sequence for windows otherwise 
            // it will be considerable as url.
            File file1 = new File("C:\\Users\\computer clinic\\OneDrive\\Articles\\myjava.txt");
            File file2 = new File("C:\\Users\\computer clinic\\OneDrive\\Articles\\java.txt");

            // By using length() method return the length of the file 1 in bits 
            // because here file exists. 
            System.out.println("The Length Of The File1 is " + " " + file1.length());

            // By using length() method return 0L because here file does not exists. 
            System.out.println("The Length Of The File2 is " + " " + file2.length());
        } catch (Exception e) {
            System.out.println("An error occurred.");
            e.printStackTrace();
        }
    }
}

Output

输出量

D:\Programs>javac FileLength.java

D:\Programs>java FileLength
The Length Of The File1 is  45
The Length Of The File2 is  0


翻译自: https://www.includehelp.com/java/file-class-long-length-method-with-example.aspx

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值