在Java中获取文件夹的属性可以通过使用File
类或者Files
类来实现。以下是两种方法的示例:
- 使用
File
类:
import java.io.File;
public class GetFolderProperties {
public static void main(String[] args) {
File folder = new File("path/to/folder");
if (folder.isDirectory()) {
System.out.println("Folder name: " + folder.getName());
System.out.println("Folder path: " + folder.getAbsolutePath());
System.out.println("Folder size: " + folder.length());
System.out.println("Last modified: " + folder.lastModified());
} else {
System.out.println("Not a valid folder path.");
}
}
}
- 使用
Files
类:
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.io.IOException;
public class GetFolderProperties {
public static void main(String[] args) {
Path folderPath = Paths.get("path/to/folder");
try {
System.out.println("Folder name: " + folderPath.getFileName());
System.out.println("Folder path: " + folderPath.toAbsolutePath());
System.out.println("Folder size: " + Files.size(folderPath));
System.out.println("Last modified: " + Files.getLastModifiedTime(folderPath));
} catch (IOException e) {
System.out.println("Error accessing folder properties: " + e.getMessage());
}
}
}
请注意,以上示例中的path/to/folder
需要替换为实际文件夹的路径。这些示例将打印出文件夹的名称、绝对路径、大小和最后修改时间等属性。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,请发送邮件至 55@qq.com 举报,一经查实,本站将立刻删除。转转请注明出处:https://www.szhjjp.com/n/949460.html