package file; import java.io.File; import java.io.IOException; import org.junit.jupiter.api.Test; /* * File类的使用 * 1.File类的一个对象,代表代表一个文件或文件目录 * 2.路径 * 相对路径:相对于当前工作环境的路径 * 绝对路径:存在盘符内的路径,注意要用转义字符\ * 3.四种构造方式 * * 如下两个方式适用于文件目录 * public String[] list() : 得到目录下所有文件或文件目录的名称数组 * public File[] listFiles() : 得到目录下所有文件或文件目录的File数组 * */ public class FileTest { public void test1() { File file1 = new File("hello.txt"); File file2 = new File("D:\a\1.txt"); File file3 = new File("D:\a","b"); File file4 = new File(file3,"c.txt"); } @Test public void test2() { File file1 = new File("hello.txt"); File file2 = new File("d:\io\hi.txt"); System.out.println(file1.getAbsolutePath()); System.out.println(file1.getPath()); System.out.println(file1.getName()); System.out.println(file1.getParent()); System.out.println(file1.length()); System.out.println(file1.lastModified()); } @Test public void test3() { File file = new File("C:\Users\ASUS\Desktop\JAVAEE\practice\IO_FIle"); String [] list = file.list(); for(String s: list) System.out.println(s); File[] listFiles = file.listFiles(); for(File f: listFiles) System.out.println(f); } /* * file1.renameTo(file2) :把文件重命名为指定的文件路径,要求file1存在,file2不存在 * */ @Test public void test4() { File file1 = new File("1.txt"); System.out.println(file1.length()); File file2 = new File("C:\Users\ASUS\Desktop\JAVAEE\practice\IO_FIle\2.txt"); boolean renameTo = file1.renameTo(file2); System.out.println(renameTo); } @Test public void test5() { File file = new File("2.txt"); System.out.println(file.isDirectory()); System.out.println(file.isFile()); System.out.println(file.exists()); System.out.println(file.canRead()); System.out.println(file.canWrite()); System.out.println(file.isHidden()); } /* * 文件的创建与删除 * 注意:删除不走回收站 * */ @Test public void test6() throws IOException { File file = new File("3.txt"); if(!file.exists()) { file.createNewFile(); System.out.println("创建成功"); } else { file.delete(); System.out.println("删除成功"); } } @Test public void test7() { File file1 = new File("io\io1\io2"); boolean mkdir1 = file1.mkdir(); if(mkdir1) System.out.println("创建成功1"); File file2 = new File("io\io1\io2"); boolean mkdir2 = file2.mkdirs(); if(mkdir2) System.out.println("创建成功2"); } @Test public void test8() throws IOException { File file1 = new File("io\io1\hello.txt"); File file2 = new File("io\io1\haha.txt"); if(!file1.exists()) { file1.createNewFile(); System.out.println("创建成功"); } if(!file2.exists()) { file2.createNewFile(); System.out.println("创建成功"); } } @Test public void test9() throws IOException { File file = new File("io\io1\a.jpg"); if(!file.exists()) { file.createNewFile(); System.out.println("创建成功"); } File file1 = new File("io\io1"); String [] s = file1.list(); for(String ss : s) { if(ss.endsWith(".jpg")) System.out.println(ss); } } static void walk(File file) { File filelist[] = file.listFiles(); for(File f:filelist) { if(f.isDirectory()) { walk(f); } else { System.out.println(f); } } } @Test public void test10() { File file = new File("io"); walk(file); } }
1