search
HomeJavaJavagetting StartedDetailed introduction to the File class in Java

Detailed introduction to the File class in Java

构造方法

(推荐教程:java入门教程

File f = new File("文件路径")
File f = new File("parent","child")

创建一个文件:

 //在工作空间目录下创建a.txt的文件
     File f = new File("a.txt"); 
     f.createNewFile(); 

     在G:\路径下创建一个a.txt的文件.如果已经有的话这不会重新创建
     File f = new File("G:\\a.txt"); 
     f.createNewFile();

    如果路径写成\\a.txt,会在盘符下创建新的文件
     File f = new File("\\a.txt"); 
     f.createNewFile();

创建一个文件夹:

 //在工作空间目录下创建a.txt的文件夹
     File f = new File("a"); 
     f.mkdir(); 

     在G:\路径下创建一个a.txt的文件夹.如果已经有的话这不会重新创建
     File f = new File("G:\\a"); 
     f.mkdir();

     如果路径写成\\a.txt,会在盘符下创建新的文件夹
     File f = new File("\\a"); 
     f.mkdir();

     在g盘下创建文件夹a,a 下创建一个b文件夹
     File f = new File("G:\\a\\b"); 
     f.mkdirs();   //注意mkdirs(),创建多个文件夹

new File 的区别:

File f = new File("a");//此时f是文件夹
File f = new File("parent","child"); //此时f是文件,parent文件夹下的文件

注意:此时会在盘符根目录下创建文件夹 或文件 d
File f = new File("", "d");
f.createNewFile(); // f.mkdir()

(视频教程推荐:java视频教程

list()方法与listFiles()方法区别:

f.list();
    返回String[]数组.里面包含了f一级目录下的文件和文件夹名.
    注意: 如果f:\\a\\b.那么b不会包含在数组中

    f.listFiles()
    返回File[]数组.里面包含了f一级目录下的文件和文件夹.
    注意: 如果f:\\a\\b.那么b不会包含在数组中

文件名过滤器 FilenameFilter

在f1的文件夹中过滤出后缀名为 "txt"的文件

代码实现:

String[] s = f1.list(new FilenameFilter() {
            /**
             * dir 需要被过滤的文件夹 name 需要别被过滤的文  件名 .此名是相对路径
             * 如果返回true 则证明是符合条件的文件.会将改文件返回到数组中
             */
            @Override
            public boolean accept(File dir, String name) {
                File f = new File(dir, name);
                if (f.isDirectory()) {
                    return false;
                }

                if (f.getName().endsWith("txt")) {
                    return true;
                }

                return false;
            }
        });

文件过滤器 FileFilter FilenameFilter

在f1文件夹中过滤出文件长度大于20M的文件.

代码实现:

  File[] fs = f1.listFiles(new FileFilter() {
            /**
             * pathname 表示要被过滤的文件,注意:不是文件名
             * 返ture 证明是符合条件的文件
             */
            @Override
            public boolean accept(File pathname) {

                if (pathname.length() > 1024 * 1024 * 20) {
                    return true;
                }

                return false;
            }
        });

绝对路径与相对路径

绝对路径  G:\\a.txt  
相对路径  a.txt.   //相对于工作空间的路径( G:\andirodWorkspace\a.txt)

The above is the detailed content of Detailed introduction to the File class in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:csdn. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft