From 2852b5f178741765d9920c233b9ef004127b77d8 Mon Sep 17 00:00:00 2001 From: seahi Date: Mon, 9 Dec 2024 08:57:54 +0800 Subject: [PATCH] =?UTF-8?q?File=E5=9F=BA=E6=9C=AC=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/chapter7/Example01.java | 58 +++++++++++++++++++++++++++++++++++++ src/chapter7/Example03.java | 16 ++++++++++ src/chapter7/Example04.java | 31 ++++++++++++++++++++ 3 files changed, 105 insertions(+) create mode 100644 src/chapter7/Example01.java create mode 100644 src/chapter7/Example03.java create mode 100644 src/chapter7/Example04.java diff --git a/src/chapter7/Example01.java b/src/chapter7/Example01.java new file mode 100644 index 0000000..f1d45f3 --- /dev/null +++ b/src/chapter7/Example01.java @@ -0,0 +1,58 @@ +package chapter7; + +import java.io.File; + +public class Example01 { + public static void main(String[] args) { + /** + * Step 1 创建普通文件 + */ + File file1 = new File("out/chapter7/file1.txt"); + if (file1.exists()) { + // 如果文件存在就删除 + System.out.println("文件file1.txt存在"); + file1.delete(); + System.out.println("文件file1.txt已删除"); + } else { + // 如果文件不存在就创建 + System.out.println("文件file1.txt不存在"); + + try { + file1.createNewFile(); + System.out.println("文件file1.txt已创建"); + } catch (Exception e) { + e.printStackTrace(); + } + } + + System.out.println("\n"); + + /** + * Step 2 创建目录 + */ + File parent = file1.getParentFile(); + if (! parent.exists()) { + // 如果父目录不存在就创建 + System.out.println("父目录不存在"); + parent.mkdirs(); + System.out.println("父目录已创建"); + } + + if (file1.exists()) { + // 如果文件存在就删除 + System.out.println("文件存在"); + file1.delete(); + System.out.println("文件已删除"); + } else { + // 如果文件不存在就创建 + System.out.println("文件不存在"); + try { + file1.createNewFile(); + System.out.println("文件已创建"); + } catch (Exception e) { + e.printStackTrace(); + } + } + + } +} diff --git a/src/chapter7/Example03.java b/src/chapter7/Example03.java new file mode 100644 index 0000000..c3eeffd --- /dev/null +++ b/src/chapter7/Example03.java @@ -0,0 +1,16 @@ +package chapter7; + +import java.io.File; + +public class Example03 { + public static void main(String[] args) { + File file = new File("src/chapter7"); + if (file.isDirectory()) { + System.out.println("是目录"); + String[] names = file.list(); + for (String name : names) { + System.out.println(name); + } + } + } +} diff --git a/src/chapter7/Example04.java b/src/chapter7/Example04.java new file mode 100644 index 0000000..bf8994f --- /dev/null +++ b/src/chapter7/Example04.java @@ -0,0 +1,31 @@ +package chapter7; + +import java.io.File; +import java.io.FilenameFilter; + +public class Example04 { + public static void main(String[] args) { + // 创建 File 对象 + File file = new File("src/chapter7"); + // 创建过滤器对象 + FilenameFilter filter = new FilenameFilter() { + // 实现 accept 方法 + @Override + public boolean accept(File dir, String name) { + File currFile = new File(dir, name); + if (currFile.isFile() && name.endsWith(".java")) { + return true; + } else { + return false; + } + } + }; + + if (file.exists() && file.isDirectory()) { + String[] names = file.list(filter); + for (String name : names) { + System.out.println(name); + } + } + } +}