栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java小农养成记第十八天

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java小农养成记第十八天

day18 第一章 File类 1.1 概述

java.io.File类是文件和目录路径名的抽象表示,主要用于文件和目录的创建、查找和删除等操作。

import java.io.File;


public class Demo01File {
    public static void main(String[] args) {
        
        String pathSeparator = File.pathSeparator;
        System.out.println(pathSeparator);//路径分隔符 Windows : 分号(;) Linux : 冒号(:)

        String separator = File.separator;
        System.out.println(separator);//文件名称分隔符 Windows : 反斜杠() Linux : 正斜杠(/)
    }
}
1.2 构造方法

public File(String pathname):通过将给定的路径名字符串转换为抽象路径名来创建新的File实例。public File(String parent,String child):从父路径名字符串和子路径名字符串创建新的File实例。public File(File parent,String child)∶从父抽象路径名和子路径名字符串创建新的File实例。构造举例,代码如下:

import java.io.File;


public class Demo02File {
    public static void main(String[] args) {
        show01();
        show02("E:\Java\JavaWorkSpace\enhance_code\exam08\file_directory", "a.txt");
        show03();
    }

    
    private static void show01() {
        File f1 = new File("E:\Java\JavaWorkSpace\enhance_code\exam08\file_directory\a.txt");
        System.out.println(f1);//重写了Object类的toString方法 E:JavaJavaWorkSpaceenhance_codeexam08file_directorya.txt
    }
    
    private static void show02(String parent, String child) {
        File file = new File(parent, child);
        System.out.println(file);//E:JavaJavaWorkSpaceenhance_codeexam08file_directorya.txt
    }

    
    private static void show03() {
        File parent = new File("E:\Java\JavaWorkSpace\enhance_code\exam08\file_directory\");//最后两个反斜杠加和不加都可以,结果都一样
        File file = new File(parent, "hello.java");
        System.out.println(file);//E:JavaJavaWorkSpaceenhance_codeexam08file_directoryhello.java
    }
}
1.3 常用方法 获取功能的方法

public string getAbsolutePath():返回此File的绝对路径名字符串。public string getPath():将此File转换为路径名字符串。public string getName():返回由此File表示的文件或目录的名称。public long length():返回由此File表示的文件的长度。

import java.io.File;


public class Demo03File {
    public static void main(String[] args) {
        show01();
        show02();
        show03();
        show04();
    }

    
    private static void show01() {
        File f1 = new File("E:\Java\JavaWorkSpace\enhance_code\exam08\file_directory\a.txt");
        String absolutePath1 = f1.getAbsolutePath();
        System.out.println(absolutePath1);//E:JavaJavaWorkSpaceenhance_codeexam08file_directorya.txt

        File f2 = new File("a.txt");
        String absolutePath2 = f2.getAbsolutePath();
        System.out.println(absolutePath2);//E:JavaJavaWorkSpaceenhance_codea.txt

    }
    
    private static void show02() {
        File f1 = new File("E:\Java\JavaWorkSpace\enhance_code\exam08\file_directory\a.txt");
        File f2 = new File("a.txt");
        String path1 = f1.getPath();
        System.out.println(path1);//E:JavaJavaWorkSpaceenhance_codeexam08file_directorya.txt
        String path2 = f2.getPath();
        System.out.println(path2);//a.txt

        System.out.println(f1);//E:JavaJavaWorkSpaceenhance_codeexam08file_directorya.txt
        System.out.println(f1.toString());//E:JavaJavaWorkSpaceenhance_codeexam08file_directorya.txt
    }
    
    private static void show03() {
        File f1 = new File("E:\Java\JavaWorkSpace\enhance_code\exam08\file_directory\a.txt");
        String name1 = f1.getName();
        System.out.println(name1);//a.txt

        File f2 = new File("E:\Java\JavaWorkSpace\enhance_code\exam08\file_directory");
        String name2 = f2.getName();
        System.out.println(name2);//file_directory
    }

    
    private static void show04() {
        File f1 = new File("E:\Java\JavaWorkSpace\enhance_code\exam08\file_directory\a.txt");
        long length = f1.length();
        System.out.println(length);//4字节

        File f2 = new File("E:\Java\JavaWorkSpace\enhance_code\exam08\file_directory\b.txt");
        System.out.println(f2.length());//0字节

        File f3 = new File("E:\Java\JavaWorkSpace\enhance_code\exam08\file_directory");
        System.out.println(f3.length());//文件夹没有大小概念
    }

}
判断功能方法

public boolean exists(): 此File表示的文件或目录是否实际存在public boolean isDirectory(): 此File表示的是否为目录public boolean isFile(): 此File表示的是否为文件

import java.io.File;


public class Demo04File {
    public static void main(String[] args) {
        show01();
        show02();
    }

    
    private static void show01() {
        File f1 = new File("E:\Java\JavaWorkSpace\enhance_code");
        System.out.println(f1.exists());//true

        File f2 = new File("E:\Java\JavaWorkSpace\enhance");
        System.out.println(f2.exists());//false

        File f3 = new File("exam08\file_directory\a.txt");//相对路径 E:JavaJavaWorkSpaceenhance
        System.out.println(f3.exists());//true

    }
    
    private static void show02() {
        File f1 = new File("E:\Java\JavaWorkSpace\enhance");

        //不存在,就没有必要获取
        if (f1.exists()){
            System.out.println(f1.isDirectory());
            System.out.println(f1.isFile());
        }

        File f2 = new File("E:\Java\JavaWorkSpace\enhance_code");
        if (f2.exists()){
            System.out.println(f2.isDirectory());//true
            System.out.println(f2.isFile());//false
        }

        File f3 = new File("E:\Java\JavaWorkSpace\enhance_code\exam08\file_directory\a.txt");
        if (f3.exists()){
            System.out.println(f3.isDirectory());//false
            System.out.println(f3.isFile());//true
        }
    }
}
创建删除功能的方法

public boolean createNewFile():当且仅当具有该名称的文件尚不存在时,创建一个新的空文件。public boolean delete():删除由此File表示的文件或目录。public boolean mkdir():创建由此File表示的目录。public boolean mkdirs():创建由此File表示的目录,包括任何必需但不存在的父目录。

import java.io.File;
import java.io.IOException;


public class Demo05File {
    public static void main(String[] args) throws IOException {
        show01();
        show02();
        show03();
    }

    
    private static void show01() throws IOException {
        File f1 = new File("E:\Java\JavaWorkSpace\enhance_code\exam08\file_directory\b.txt");
        boolean b1 = f1.createNewFile();
        System.out.println("b1:" + b1);

        File f2 = new File("exam08\file_directory\c.txt");
        System.out.println(f2.createNewFile());

        //File f3 = new File("exam\a.txt");
        //System.out.println(f3.createNewFile());//路径不存在会抛出IOException
    }
    
    private static void show02() {
        File f1 = new File("E:\Java\JavaWorkSpace\enhance_code\exam08\file_directory\aaa");
        boolean b1 = f1.mkdir();
        System.out.println("b1:" + b1);

        File f2 = new File("E:\Java\JavaWorkSpace\enhance_code\exam08\file_directory\bbb\ccc");
        boolean b2 = f2.mkdirs();
        System.out.println("b2:" + b2);
    }
    
    private static void show03() {
        File f1 = new File("E:\Java\JavaWorkSpace\enhance_code\exam08\file_directory\aaa");
        boolean b1 = f1.delete();
        System.out.println(b1);

        File f2 = new File("E:\Java\JavaWorkSpace\enhance_code\exam08\file_directory\c.txt");
        boolean b2 = f2.delete();
        System.out.println(b2);
    }
}
1.4 目录的遍历

public String[] list(): 返回一个String数组,表示该File目录中的所有子文件或者目录。public File[] listFiles(): 返回一个File数组,表示该File目录中的所有子文件或者目录。

import java.io.File;


public class Demo06File {
    public static void main(String[] args) {
        show01();
        show02();
    }

    
    private static void show01() {
        //File file = new File("E:\Java\JavaWorkSpace\enhance_code\exam08\file_directory\a.txt");//如果访问的不是文件夹会报空指针异常NullPointerException
        File file = new File("E:\Java\JavaWorkSpace\enhance_code\exam08\file_directory");
        String[] arr = file.list();
        for (String fileName : arr){
            System.out.println(fileName);
        }
    }
    
    private static void show02() {
        File file = new File("E:\Java\JavaWorkSpace\enhance_code\exam08\file_directory");
        File[] files = file.listFiles();
        for (File f : files){
            System.out.println(f);
        }
    }

}
第二章 递归 2.1 概述

递归︰指在当前方法内调用自己的这种现象。递归的分类:

递归分为两种,直接递归和间接递归。直接递归称为方法自身调用自己。间接递归可以A方法调用B方法,B方法调用C方法,C方法调用A方法。 注意事项:

递归一定要有条件限定,保证递归能够停止下来,否则会发生栈内存溢出。在递归中虽然有限定条件,但是递归次数不能太多。否则也会发生栈内存溢出。构造方法,禁止递归

public class Demo01Recursion {
    public static void main(String[] args) {
        a();
        b(1);
    }
    

    
    
    private static void a(){
        System.out.println("a方法!");
        a();
    }
    
    private static void b(int i) {
        System.out.println(i);
        if (i == 20000) {
            return;
        }
        b(++i);
    }

}
2.2 递归求和
public class Demo02Recurison {
    public static void main(String[] args) {
        
        int s = sum (100);
        System.out.println(s);
    }
    private static int sum(int n){
        if (n == 1) return 1;
        return n + sum(n - 1);
    }

}
2.3 递归求阶乘
public class Demo03Recursion {
    public static void main(String[] args) {
        int ans = jc(5);
        System.out.println(ans);
    }

    private static int jc(int i) {
        if (i == 1){
            return 1;
        }
        return i * jc (i - 1);
    }
}
2.4 递归打印多级目录
import java.io.File;


public class Demo04Recursion {
    public static void main(String[] args) {
        File file = new File("E:\Java\JavaWorkSpace\enhance_code\exam08\file_directory");
        getAllFile(file);
    }

    private static void getAllFile(File dir){
        System.out.println(dir);
        File[] files = dir.listFiles();
        for (File f : files){
            if (f.isDirectory())
                getAllFile(f);
            else{
                System.out.println(f);
            }
        }
    }
}
第三章 综合案例 3.1 文件搜索
import java.io.File;


public class Demo05Recursion {
    public static void main(String[] args) {
        File file = new File("E:\Java\JavaWorkSpace\enhance_code\exam08\file_directory");
        getAllFile(file);
    }

    private static void getAllFile(File dir){
        File[] files = dir.listFiles();
        for (File f : files){
            if (f.isDirectory())
                getAllFile(f);
            else{
                
                //String s = f.getName();
                //String s = f.getPath();
                String s = f.toString();

                //把字符串,转换为小写
                s = s.toLowerCase();

                //2.调用String类中的方法endswith判断字符串是否是以.txt结尾
                boolean b = s.endsWith(".txt");

                //3.如果是以.txt结尾的文件,这输出
                if (b){
                    System.out.println(f);
                }
            }
        }
    }
}
3.2 Filter过滤器

import java.io.File;
import java.io.FileFilter;


public class FileFilterImpl implements FileFilter {
    @Override
    public boolean accept(File pathname) {
        
        if (pathname.isDirectory())
            return true;
        String s = pathname.toString();
        if(s.endsWith(".txt"))
            return true;
        return false;
    }
}

import java.io.File;

public class demo01Filter {
    public static void main(String[] args) {
        File file = new File("E:\Java\JavaWorkSpace\enhance_code\exam08\file_directory");
        getAllFile(file);
    }

    private static void getAllFile(File dir){
        File[] files = dir.listFiles(new FileFilterImpl());
        
        for (File f : files){
            if (f.isDirectory())
                getAllFile(f);
            else{
                System.out.println(f);
            }
        }
    }
}
3.3 FileNameFilter过滤
import java.io.File;
import java.io.FileFilter;
import java.io.FilenameFilter;

public class demo02Filter {
    public static void main(String[] args) {
        File file = new File("E:\Java\JavaWorkSpace\enhance_code\exam08\file_directory");
        getAllFile(file);
    }

    private static void getAllFile(File dir){
        

//        File[] files = dir.listFiles(new FilenameFilter() {
//            @Override
//            public boolean accept(File dir, String name) {
//                return new File(dir, name).isDirectory() || name.toLowerCase().endsWith(".txt");
//            }
//        });

        //使用Lambda表达式优化匿名内部类(接口中只有一个抽象方法)
//        File[] files = dir.listFiles((File d, String name)->{
//            return new File(d, name).isDirectory() || name.toLowerCase().endsWith(".txt");
//        });
        File[] files = dir.listFiles((d, name)->new File(d, name).isDirectory() || name.toLowerCase().endsWith(".txt"));

        
        for (File f : files){
            if (f.isDirectory())
                getAllFile(f);
            else{
                System.out.println(f);
            }
        }
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/713142.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号