在当前目录下创建一个文件,命名为学号.txt,判定文件的存在性,如果没有,创建物理文件,同时读取文件的各种属性信息。 java
时间: 2023-06-01 15:05:53 浏览: 39
好的,我了解了您的要求。以下是您需要的Java代码:
```
import java.io.File;
public class Main {
public static void main(String[] args) {
// 获取学号
String studentId = "这里替换成您的学号";
// 创建文件对象
File file = new File("./" + studentId + ".txt");
// 判断文件是否存在
if (!file.exists()) {
// 如果文件不存在,创建物理文件
try {
file.createNewFile();
} catch (Exception e) {
// 创建文件失败,输出错误信息
e.printStackTrace();
}
}
// 读取文件的各种属性信息
System.out.println("文件名:" + file.getName());
System.out.println("路径:" + file.getPath());
System.out.println("绝对路径:" + file.getAbsolutePath());
System.out.println("父目录:" + file.getParent());
System.out.println("是否可读:" + file.canRead());
System.out.println("是否可写:" + file.canWrite());
System.out.println("文件长度:" + file.length() + " 字节");
}
}
```
请注意,该代码只是一个示例,您需要将其中的“这里替换成您的学号”修改为您的真实学号。同时,如果您的操作系统为Windows,文件路径中的斜杠“/”需要修改为反斜杠“\\”。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)