|
@@ -0,0 +1,310 @@
|
|
|
+package com.sf.day19;
|
|
|
+
|
|
|
+import org.junit.Test;
|
|
|
+
|
|
|
+import java.io.*;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.Date;
|
|
|
+
|
|
|
+public class Test01 {
|
|
|
+ @Test
|
|
|
+ public void t1(){
|
|
|
+ /**
|
|
|
+ * file类
|
|
|
+ * win \\ linux /
|
|
|
+ */
|
|
|
+ String pathname = "D:\\a.txt";
|
|
|
+ File file = new File(pathname);
|
|
|
+ System.out.println(file);
|
|
|
+
|
|
|
+ //构造器的第二种方式 通过父路径和子路径字符串
|
|
|
+ String parent = "D:\\aaa";
|
|
|
+ String child = "bbb.txt";
|
|
|
+ File file1 = new File(parent, child);
|
|
|
+ System.out.println(file1);
|
|
|
+
|
|
|
+ /**
|
|
|
+ * file构造器的第三种方式 通过父级File对象和子路径字符串
|
|
|
+ */
|
|
|
+ File file2 = new File("D:\\aaa");
|
|
|
+ String child2 = "bbb.txt";
|
|
|
+ File file3 = new File(file2, child2);
|
|
|
+ System.out.println(file3);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * file类下的方法演示
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ public void t2(){
|
|
|
+ File file = new File("D:\\ruanjian\\aDrive\\locales\\af.pak");
|
|
|
+ //获取当前文件的名称
|
|
|
+ System.out.println(file.getName());
|
|
|
+ //获取文件的相对路径
|
|
|
+ System.out.println(file.getPath());
|
|
|
+ //获取文件的绝对路径
|
|
|
+ System.out.println(file.getAbsolutePath());
|
|
|
+ //获取文件的父路径
|
|
|
+ System.out.println(file.getParent());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void t3(){
|
|
|
+ File file = new File("D:\\Program Files\\IntelliJ IDEA 2019.3.3\\VIP33\\a.txt");
|
|
|
+ System.out.println(System.getProperty("user.dir"));
|
|
|
+ System.out.println(file.getName());
|
|
|
+ System.out.println(file.getParent());
|
|
|
+ System.out.println(file.getPath());
|
|
|
+ System.out.println(file.getAbsolutePath());
|
|
|
+ System.out.println(file.getAbsoluteFile().getParent());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * file类的常用方法
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ public void t4(){
|
|
|
+ File file = new File("D:\\a.txt");
|
|
|
+ System.out.println(file.getPath());
|
|
|
+ System.out.println(file.getAbsolutePath());
|
|
|
+ System.out.println(file.getParent());
|
|
|
+ System.out.println(file.getName());
|
|
|
+ System.out.println(file.length());
|
|
|
+ long time = file.lastModified();
|
|
|
+ System.out.println(file.lastModified());
|
|
|
+ Date date = new Date(time);
|
|
|
+ System.out.println(date);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void t5(){
|
|
|
+ File file = new File("D:\\");
|
|
|
+ //列出当前目录下的所有文件和目录
|
|
|
+ String[] strs = file.list();
|
|
|
+ System.out.println(Arrays.toString(strs));
|
|
|
+ for (String str : strs) {
|
|
|
+ System.out.println(str);
|
|
|
+ }
|
|
|
+ //返回当前路径下的文件和目录 因为file对象 + 目录+文件
|
|
|
+ File[] files = file.listFiles();
|
|
|
+ for (File file1 : files) {
|
|
|
+ System.out.println(file1);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * file类的重新命名 renameTo
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ public void t6(){
|
|
|
+ File file = new File("D:\\a.txt");
|
|
|
+ File file1 = new File("D:\\b.txt");
|
|
|
+ boolean b = file.renameTo(file1);
|
|
|
+ System.out.println(b);
|
|
|
+ boolean b1 = file1.renameTo(file);
|
|
|
+ System.out.println(b1);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void t7(){
|
|
|
+ File file = new File("F:\\a.txt");
|
|
|
+ boolean exists = file.exists(); //判断当前文件是否存在
|
|
|
+ System.out.println(exists);
|
|
|
+ boolean directory = file.isDirectory(); //判断当前是否为文件目录
|
|
|
+ System.out.println(directory);
|
|
|
+ System.out.println(file.isFile()); //判断当前是否为文件
|
|
|
+ System.out.println(file.isHidden()); //判断当前是否隐藏
|
|
|
+ System.out.println(file.canRead());//可读
|
|
|
+ System.out.println(file.canWrite());//可写
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建文件
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ public void t8() throws IOException {
|
|
|
+ File file = new File("aaa.txt");
|
|
|
+ System.out.println(file.exists());
|
|
|
+ System.out.println(file.createNewFile());
|
|
|
+ System.out.println(file.exists());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建目录
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ public void t9() throws IOException {
|
|
|
+// File f2= new File("newDir");
|
|
|
+// System.out.println("newDir是否存在:"+f2.exists());
|
|
|
+// System.out.println("newDir是否创建:"+f2.mkdir());
|
|
|
+// System.out.println("newDir是否存在:"+f2.exists());
|
|
|
+ // 创建多级目录
|
|
|
+ File f5= new File("aaa.txt");
|
|
|
+// System.out.println("newDira\\newDirb创建:" + f5.mkdirs());
|
|
|
+ boolean delete = f5.delete();
|
|
|
+ System.out.println(delete);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void t10() throws IOException {
|
|
|
+ File file = new File("D:\\file");
|
|
|
+ boolean mkdirs = file.mkdirs();
|
|
|
+ if(mkdirs){
|
|
|
+ File file1 = new File("D:\\file\\h.java");
|
|
|
+ boolean newFile = file1.createNewFile();
|
|
|
+ if(newFile){
|
|
|
+ System.out.println("创建成功");
|
|
|
+ boolean delete = file1.delete();
|
|
|
+ if(delete){
|
|
|
+ System.out.println("删除成功");
|
|
|
+ file.delete();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void t11(){
|
|
|
+// //方式一
|
|
|
+// File file = new File("D:\\demo");
|
|
|
+// for (String s : file.list()) {
|
|
|
+// if(s.endsWith(".md")){
|
|
|
+// System.out.println(s);
|
|
|
+// }
|
|
|
+// }
|
|
|
+
|
|
|
+// //方式二
|
|
|
+// File file = new File("D:\\demo");
|
|
|
+// for (File listFile : file.listFiles()) {
|
|
|
+// if(listFile.getName().endsWith(".md")){
|
|
|
+// System.out.println(listFile.getName());
|
|
|
+// }
|
|
|
+// }
|
|
|
+
|
|
|
+ //方式三
|
|
|
+ /*
|
|
|
+ * File类提供了两个文件过滤器方法
|
|
|
+ * public String[] list(FilenameFilter filter)
|
|
|
+ * public File[] listFiles(FileFilter filter)
|
|
|
+ */
|
|
|
+// File file = new File("D:\\demo");
|
|
|
+// File[] files = file.listFiles(new FilenameFilter() {
|
|
|
+// @Override
|
|
|
+// public boolean accept(File dir, String name) {
|
|
|
+// return name.endsWith(".md");
|
|
|
+// }
|
|
|
+// });
|
|
|
+// for (File file1 : files) {
|
|
|
+// System.out.println(file1.getName());
|
|
|
+// }
|
|
|
+
|
|
|
+ File file = new File("D:\\demo");
|
|
|
+ File[] files = file.listFiles(new FileFilter() {
|
|
|
+ @Override
|
|
|
+ public boolean accept(File pathname) {
|
|
|
+ return pathname.getName().endsWith(".md");
|
|
|
+ }
|
|
|
+ });
|
|
|
+ for (File file1 : files) {
|
|
|
+ System.out.println(file1.getName());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 练习3:遍历指定目录所有文件名称,包括子文件目录中的文件。
|
|
|
+ *
|
|
|
+ * 拓展1:并计算指定目录占用空间的大小
|
|
|
+ *
|
|
|
+ * 拓展2:删除指定文件目录及其下的所有文件
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ public void t12(){
|
|
|
+ File file = new File("D:\\aaa");
|
|
|
+ deleteDirectory(file);
|
|
|
+// printSubFile(file);
|
|
|
+// long directorySize = getDirectorySize(file);
|
|
|
+// System.out.println(directorySize);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 递归方法 遍历子文件夹
|
|
|
+ * @param file
|
|
|
+ */
|
|
|
+ public static void printSubFile(File file){
|
|
|
+ File[] files = file.listFiles();
|
|
|
+ for (File file1 : files) {
|
|
|
+ if(file1.isDirectory()){
|
|
|
+ System.out.println(file1.getName());//目录
|
|
|
+ printSubFile(file1);
|
|
|
+
|
|
|
+ }else { //文件
|
|
|
+ System.out.println(file1.getName());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static long getDirectorySize(File file){
|
|
|
+ long lengthCount = 0;
|
|
|
+ if(file.isFile()){
|
|
|
+ lengthCount = file.length();
|
|
|
+ }else {
|
|
|
+ File[] files = file.listFiles();
|
|
|
+ for (File file1 : files) {
|
|
|
+ lengthCount += getDirectorySize(file1);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return lengthCount;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * // 拓展2:删除指定的目录
|
|
|
+ */
|
|
|
+ public static void deleteDirectory(File file){
|
|
|
+ if(file.isDirectory()){
|
|
|
+ File[] files = file.listFiles();
|
|
|
+ for (File file1 : files) {
|
|
|
+ deleteDirectory(file1);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ file.delete();//删掉 //从里向外删除
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 读取hello.txt文件中的字符数据,并显示在控制台上
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ public void t13() throws IOException {
|
|
|
+ File file = new File("D:\\a.txt");
|
|
|
+ FileReader fr = new FileReader(file);
|
|
|
+ int data;
|
|
|
+ while ((data = fr.read()) !=-1){
|
|
|
+ System.out.println(data);
|
|
|
+ System.out.println((char)data);
|
|
|
+ }
|
|
|
+ fr.close(); //关闭资源
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /*
|
|
|
+ //实现方式3:调用read(char[] cbuf),每次从文件中读取多个字符
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ public void t14() throws IOException {
|
|
|
+ File file = new File("D:\\a.txt");
|
|
|
+ FileReader fileReader = new FileReader(file);
|
|
|
+ char[] chars = new char[5];
|
|
|
+// fileReader.read(chars);
|
|
|
+ int data ;
|
|
|
+ while ((data = fileReader.read(chars)) != -1){
|
|
|
+ System.out.println(Arrays.toString(chars));
|
|
|
+ }
|
|
|
+ fileReader.close();
|
|
|
+ }
|
|
|
+}
|