|
@@ -0,0 +1,226 @@
|
|
|
+package com.lovecoding.day21.file01;
|
|
|
+
|
|
|
+import org.junit.Test;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileFilter;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.Date;
|
|
|
+
|
|
|
+public class TestFile {
|
|
|
+
|
|
|
+ //* `public String[] list()` :返回一个String数组,表示该File目录中的所有子文件或目录。
|
|
|
+ //* `public File[] listFiles()` :返回一个File数组,表示该File目录中的所有的子文件或目录。
|
|
|
+ //* `public File[] listFiles(FileFilter filter)`:返回所有满足指定过滤器的文件和目录。如果给定 filter 为 null,则接受所有路径名。否则,当且仅当在路径名上调用过滤器的 FileFilter.accept(File pathname)方法返回 true 时,该路径名才满足过滤器。如果当前File对象不表示一个目录,或者发生 I/O 错误,则返回 null。
|
|
|
+ //* `public String[] list(FilenameFilter filter)`:返回返回所有满足指定过滤器的文件和目录。如果给定 filter 为 null,则接受所有路径名。否则,当且仅当在路径名上调用过滤器的 FilenameFilter .accept(File dir, String name)方法返回 true 时,该路径名才满足过滤器。如果当前File对象不表示一个目录,或者发生 I/O 错误,则返回 null。
|
|
|
+ //* `public File[] listFiles(FilenameFilter filter)`:返回返回所有满足指定过滤器的文件和目录。如果给定 filter 为 null,则接受所有路径名。否则,当且仅当在路径名上调用过滤器的 FilenameFilter .accept(File dir, String name)方法返回 true 时,该路径名才满足过滤器。如果当前File对象不表示一个目录,或者发生 I/O 错误,则返回 null。
|
|
|
+ @Test
|
|
|
+ public void test14(){
|
|
|
+ File file = new File("E:\\VIP-23\\javacode\\JavaSE\\day21");
|
|
|
+ //接口
|
|
|
+
|
|
|
+// File[] files = file.listFiles(new FilenameFilter() {
|
|
|
+// @Override
|
|
|
+// public boolean accept(File dir, String name) {
|
|
|
+// return false;
|
|
|
+// }
|
|
|
+// });
|
|
|
+ File[] files1 = file.listFiles((File dir, String name) -> {
|
|
|
+ return name.endsWith(".iml");
|
|
|
+ });
|
|
|
+
|
|
|
+ System.out.println(Arrays.toString(files1));
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void test13(){
|
|
|
+ File file = new File("E:\\VIP-23\\javacode\\JavaSE\\day21");
|
|
|
+ //接口
|
|
|
+
|
|
|
+ File[] files = file.listFiles(new FileFilter() {
|
|
|
+ //返回true
|
|
|
+ @Override
|
|
|
+ public boolean accept(File pathname) {
|
|
|
+ //业务逻辑
|
|
|
+ if (pathname.toString().endsWith(".iml")){
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ System.out.println(Arrays.toString(files));
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void test12(){
|
|
|
+ File file = new File("../day21");
|
|
|
+ File[] files = file.listFiles();
|
|
|
+
|
|
|
+ for (File file1 : files) {
|
|
|
+ System.out.println(file1);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ @Test
|
|
|
+ public void test11(){
|
|
|
+ File file = new File("E:\\VIP-23\\javacode\\JavaSE\\day21");
|
|
|
+ String[] list = file.list();
|
|
|
+
|
|
|
+ System.out.println(Arrays.toString(list));
|
|
|
+ }
|
|
|
+
|
|
|
+ //- `public boolean createNewFile()` :当且仅当具有该名称的文件爱不存在时,创建一个新的空文件。
|
|
|
+ //- `public boolean delete()` :删除由此File表示的文件或目录。 只能删除空目录。
|
|
|
+ //- `public boolean mkdir()` :创建由此File表示的目录。
|
|
|
+ //- `public boolean mkdirs()` :创建由此File表示的目录,包括任何必需但不存在的父目录。
|
|
|
+ @Test
|
|
|
+ public void test10() throws IOException {
|
|
|
+
|
|
|
+ File file = new File("aaa");
|
|
|
+ boolean mkdir = file.mkdir();
|
|
|
+ System.out.println(mkdir);
|
|
|
+
|
|
|
+ File file1 = new File("bbb/ccc/ddd");
|
|
|
+ boolean mkdirs = file1.mkdirs();
|
|
|
+ System.out.println(mkdirs);
|
|
|
+ }
|
|
|
+ @Test
|
|
|
+ public void test09() throws IOException {
|
|
|
+ File file1 = new File("aaa.txt");
|
|
|
+ boolean delete = file1.delete();
|
|
|
+ System.out.println(delete);
|
|
|
+
|
|
|
+ File file2 = new File("aaa");
|
|
|
+ System.out.println(file2.delete());
|
|
|
+
|
|
|
+ }
|
|
|
+ @Test
|
|
|
+ public void test08() throws IOException {
|
|
|
+ File file1 = new File("aaa.txt");
|
|
|
+
|
|
|
+ boolean newFile = file1.createNewFile();
|
|
|
+ System.out.println(newFile);
|
|
|
+ //绝对路径
|
|
|
+ System.out.println(file1.getAbsoluteFile());
|
|
|
+ //大小
|
|
|
+ System.out.println(file1.length());
|
|
|
+ }
|
|
|
+
|
|
|
+ //- `public boolean exists()` :此File表示的文件或目录是否实际存在。
|
|
|
+ //- `public boolean isDirectory()` :此File表示的是否为目录。
|
|
|
+ //- `public boolean isFile()` :此File表示的是否为文件。
|
|
|
+ @Test
|
|
|
+ public void test07(){
|
|
|
+ File file1 = new File("E:\\VIP-23\\javacode\\JavaSE\\aaa.txt");
|
|
|
+ File file2 = new File("bbb.txt");
|
|
|
+
|
|
|
+ System.out.println(file1.exists());
|
|
|
+ System.out.println(file2.exists());
|
|
|
+
|
|
|
+ File file3 = new File("E:\\VIP-23\\javacode\\JavaSE\\bbb.txt");
|
|
|
+ boolean directory = file3.isDirectory();
|
|
|
+ System.out.println(directory);
|
|
|
+ System.out.println(file1.isFile());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ //#### 各种路径问题
|
|
|
+ //
|
|
|
+ //* ` public String getPath() ` :将此File转换为路径名字符串。
|
|
|
+ //* `public String getAbsolutePath() ` :返回此File的绝对路径名字符串。
|
|
|
+ //* `String getCanonicalPath()`:返回此File对象所对应的规范路径名。
|
|
|
+
|
|
|
+ // 绝对路径 : 从盘符出发 到文件 到目录
|
|
|
+ // 相对路径 : 上一级 ../ a/
|
|
|
+ // 规范路径 : D:aa/bb/cc
|
|
|
+ // ../../a.txt
|
|
|
+ @Test
|
|
|
+ public void test06() throws IOException {
|
|
|
+ File file = new File("aaa.txt");
|
|
|
+ String path = file.getPath();
|
|
|
+ System.out.println(path);
|
|
|
+ //系统变量
|
|
|
+ String property = System.getProperty("user.dir");
|
|
|
+ System.out.println(property+file.getName());
|
|
|
+
|
|
|
+ String absolutePath = file.getAbsolutePath();
|
|
|
+ System.out.println(absolutePath);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void test05() throws IOException {
|
|
|
+ File file = new File("../../aaa.txt");
|
|
|
+ String path = file.getPath();
|
|
|
+ System.out.println(path);
|
|
|
+
|
|
|
+ File absoluteFile = file.getAbsoluteFile();
|
|
|
+ System.out.println(absoluteFile);
|
|
|
+
|
|
|
+ File canonicalFile = file.getCanonicalFile();
|
|
|
+ System.out.println(canonicalFile);
|
|
|
+ }
|
|
|
+ @Test
|
|
|
+ public void test04() throws IOException {
|
|
|
+ File file = new File("D:aaa\\aaa.txt");
|
|
|
+ String path = file.getPath();
|
|
|
+ System.out.println(path);
|
|
|
+
|
|
|
+ File absoluteFile = file.getAbsoluteFile();
|
|
|
+ System.out.println(absoluteFile);
|
|
|
+
|
|
|
+ File canonicalFile = file.getCanonicalFile();
|
|
|
+ System.out.println(canonicalFile);
|
|
|
+ }
|
|
|
+ @Test
|
|
|
+ public void test03() throws IOException {
|
|
|
+ File file = new File("aaa.txt");
|
|
|
+ String path = file.getPath();
|
|
|
+ System.out.println(path);
|
|
|
+
|
|
|
+ File absoluteFile = file.getAbsoluteFile();
|
|
|
+ System.out.println(absoluteFile);
|
|
|
+
|
|
|
+ File canonicalFile = file.getCanonicalFile();
|
|
|
+ System.out.println(canonicalFile);
|
|
|
+ }
|
|
|
+
|
|
|
+ //* `public String getName()` :返回由此File表示的文件或目录的名称。
|
|
|
+ //* `public long length()` :返回由此File表示的文件的长度。
|
|
|
+ //* ` public String getPath() ` :将此File转换为路径名字符串。
|
|
|
+ //* `public long lastModified()`:返回File对象对应的文件或目录的最后修改时间(毫秒值)
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void test02(){
|
|
|
+ File file01 = new File("aaa.txt");
|
|
|
+ String name = file01.getName();
|
|
|
+ //名称
|
|
|
+ System.out.println(name);
|
|
|
+ //大小字节
|
|
|
+ System.out.println(file01.length());
|
|
|
+ //路径
|
|
|
+ System.out.println(file01.getPath());
|
|
|
+ //修改时间
|
|
|
+ long l = file01.lastModified();
|
|
|
+
|
|
|
+ Date date = new Date(l);
|
|
|
+ System.out.println(date);
|
|
|
+
|
|
|
+ }
|
|
|
+ @Test
|
|
|
+ public void test01(){
|
|
|
+
|
|
|
+ //构造
|
|
|
+ File file01 = new File("aaa.txt");
|
|
|
+
|
|
|
+ File file02 = new File("bbb");
|
|
|
+
|
|
|
+ File file03 = new File(file02, "bbb.txt");
|
|
|
+
|
|
|
+ File file04 = new File("ccc/", "bbb.txt");
|
|
|
+
|
|
|
+ }
|
|
|
+}
|