用java在D盘根目录下创建文件名为iotest5.txt,完成下列功能。 ① 输出该文件的名称. ②输出该文件的路径. ③ 输出该文件的大小.
时间: 2024-03-06 11:47:56 浏览: 20
好的,我知道了。以下是Java代码实现:
```java
import java.io.*;
public class FileDemo {
public static void main(String[] args) {
String fileName = "D:/iotest5.txt";
File file = new File(fileName);
try {
if (file.createNewFile()) {
System.out.println("文件名称:" + file.getName());
System.out.println("文件路径:" + file.getAbsolutePath());
System.out.println("文件大小:" + file.length() + "字节");
} else {
System.out.println("文件创建失败,该文件可能已经存在!");
}
} catch (IOException e) {
System.out.println("文件创建失败,出现异常:" + e.getMessage());
}
}
}
```
这段代码会在D盘根目录下创建一个名为`iotest5.txt`的文件,并输出该文件的名称、路径和大小信息。注意,如果文件已经存在,则不会再次创建。