1. import java.io.File;
    2. public class Main {
    3. public static void main(String[] args) {
    4. File f = new File("demo05/src/test.txt");
    5. System.out.println(f);
    6. System.out.println(f.getAbsolutePath());
    7. System.out.println(f.getPath());
    8. System.out.println(f.isFile());
    9. System.out.println(f.isDirectory());
    10. System.out.println(f.canRead());
    11. }
    12. }
    1. // 创建临时文件
    2. public class Main {
    3. public static void main(String[] args) throws IOException {
    4. File f = File.createTempFile("tmp-", ".txt"); // 提供临时文件的前缀和后缀
    5. f.deleteOnExit(); // JVM退出时自动删除
    6. System.out.println(f.isFile());
    7. System.out.println(f.getAbsolutePath());
    8. }
    1. import java.io.File;
    2. import java.io.FilenameFilter;
    3. public class GetFileDir {
    4. public static void main(String[] args) {
    5. File f = new File("C:\\Windows");
    6. File[] fs1 = f.listFiles();// 列出所有的文件和子目录
    7. printFiles(fs1);
    8. File[] fs2 = f.listFiles(new FilenameFilter() {
    9. @Override
    10. public boolean accept(File dir, String name) {
    11. return name.endsWith(".exe");//返回True表示接受该文件
    12. }
    13. });
    14. printFiles(fs2);
    15. }
    16. private static void printFiles(File[] files) {
    17. System.out.println("==========================================");
    18. if (files != null) {
    19. for (File f : files) {
    20. System.out.println(f);
    21. }
    22. }
    23. System.out.println("==========================================");
    24. }
    25. }
    1. // path路径
    2. import java.io.File;
    3. import java.nio.file.Path;
    4. import java.nio.file.Paths;
    5. public class PathTs {
    6. public static void main(String[] args) {
    7. // 构造一个Path对象
    8. Path p1 = Paths.get(".", "project", "study");
    9. System.out.println(p1);
    10. //转化为绝对路径
    11. Path p2 = p1.toAbsolutePath();
    12. System.out.println(p2);
    13. //转换为规范路径
    14. Path p3 = p2.normalize();
    15. System.out.println(p3);
    16. //转换为File对象
    17. File f = p3.toFile();
    18. System.out.println(f);
    19. for (Path p : Paths.get("..").toAbsolutePath()) {
    20. System.out.println(" " + p);
    21. }
    22. }
    23. }