编写一个Java程序,使用File类读取文件名、文件权限、文件长度等属性在控制台上打印。
时间: 2024-03-21 12:40:06 浏览: 23
好的,以下是Java程序的实现:
```java
import java.io.File;
public class FileProperties {
public static void main(String[] args) {
// 创建File对象
File file = new File("文件路径");
// 判断文件是否存在
if (file.exists()) {
// 打印文件名
System.out.println("文件名:" + file.getName());
// 打印文件权限
System.out.println("文件权限:");
System.out.println("可读:" + file.canRead());
System.out.println("可写:" + file.canWrite());
System.out.println("可执行:" + file.canExecute());
// 打印文件长度
System.out.println("文件长度:" + file.length() + " 字节");
} else {
System.out.println("该文件不存在!");
}
}
}
```
注意,需要将代码中的“文件路径”替换为具体的文件路径,才能正确运行程序。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![chw](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![dmg](https://img-home.csdnimg.cn/images/20210720083646.png)
![exe](https://img-home.csdnimg.cn/images/20210720083343.png)
![dmg](https://img-home.csdnimg.cn/images/20210720083646.png)