search
HomeJavajavaTutorialFile overview and usage introduction
File overview and usage introductionJun 21, 2017 am 09:34 AM
filerecursion

File Overview
java.io.File class: abstract representation of file and directory path names.
Used to describe files, folders, and path classes in the computer

Three commonly used words related to File:
file: file
directory: file Folder (directory)
path: path
File is a class that has nothing to do with the system

Three overloaded construction methods of the File class
Path:
Directory separation of the window system The symbol is a \
The directory separator in java is: \\ or /
Path classification :
Absolute path: A path starting with a drive letter
For example: D:\\ase\\20170514\\day10
D:\\Work_EE_266\\day10\\src
Relative path: relative to the current project, the path When writing, you can omit the path between the drive letter and the project
D:\\Work_EE_266\\day10-->src

Note: Lu Jin is not case-sensitive
File(File parent, String child)
Pass path, pass File type parent path, String type child path
Benefits: The parent path is File type, the parent path can directly call the method of File class
File(String parent, String child)
Pass the path, pass the parent path of string type, and the child path of string type
Benefits: Operate the parent path and child path separately, it is more flexible to use, you can pass the path separately as a parameter
File(String pathname)
Pass the path name: you can write to a folder or a file
c:\\abc c:\\abc\\Demo.java
It doesn’t matter whether the path exists or not Create, the path is not case-sensitive

 1 public static void main(String[] args) { 2         /* 3          * static String pathSeparator 与系统有关的路径分隔符,为了方便,它被表示为一个字符串。 
 4          * static char pathSeparatorChar  与系统有关的路径分隔符。 
 5          * static String separator  与系统有关的默认名称分隔符,为了方便,它被表示为一个字符串。 
 6          * static char separatorChar  与系统有关的默认名称分隔符。 
 7          */ 8         String pathSeparator = File.pathSeparator; 9         System.out.println(pathSeparator);//路径分隔符 windows 分号; linux 冒号:10         11         String separator = File.separator;12         System.out.println(separator);//目录名称分隔符windows 反斜杠\  linux 正斜杠/13         14         /*15          * System类中的方法16          * static String getProperty(String key) 获取指定键指示的系统属性。 
17          * file.separator 文件分隔符(在 UNIX 系统中是“/”) 
18          * path.separator 路径分隔符(在 UNIX 系统中是“:”) 
19          * line.separator 行分隔符(在 UNIX 系统中是“/n”) 
20          */21         System.out.println(System.getProperty("file.separator"));22         System.out.println(System.getProperty("line.separator"));23         System.out.println(System.getProperty("path.separator"));24     }

File class creation and deletion function
File class deletion function
boolean delete()
Delete a file or folder, given in the File construction method
Returns true if the deletion is successful, false if the deletion fails (does not exist, there is content in the folder)
The deletion method does not go to the recycle bin, delete directly from the hard disk
Deletion is risky, please run with caution


File creation folder function
boolean mkdir() can only create a single-layer folder
boolean mkdirs( ) Can create both single-layer folders and multi-layer folders
The created path is also given in the File construction method
If the folder already exists, it will not be created


File function to create a file
boolean createNewFile()
The path and file name of the created file are given in the File construction method
If the file already exists, it will return false if it is not created
Only files can be created, not folders (look at the type, don’t look at the suffix)
The path to create the folder must exist

The judgment function of the File class
boolean isDirectory()
Determine whether the path encapsulated in the File construction method is a folder
If it is a folder, return true, if it is not a folder, return false
boolean isFile()
Determine the File construction method Whether the path encapsulated in is a file
boolean exists()
Determine whether the encapsulated path in the File construction method exists
If it exists, it returns true, if it does not exist, it returns false

The acquisition function of the File class
String getParent() returns a String object
File getParentFile() returns a File object
Gets the parent path, and returns the parent path at the end of the file

long length()
Returns the path The number of bytes of the file represented in, the folder has no size

String getPath() Converts this abstract pathname to a pathname string.
Same as toString

String getName()
Returns the file or folder name represented in the path
Gets the name of the last part of the path

File getAbsoluteFile() returns The absolute pathname form of this abstract pathname.
String getAbsolutePath() Returns the absolute pathname string of this abstract pathname.
Get the absolute path


Methods for traversing directories list and listFiles
Notes:
1. The traversed path can only be one directory
2. The directory being traversed must exist
Otherwise, a null pointer exception will be thrown

static File[] listRoots()
Get all root directories in the system

File[] listFiles()
Get the file and folder names in the path encapsulated in the File construction method (traverse a directory)
Returns the full path of the directory or file

String[] list( )
Get the file and folder names in the path encapsulated in the File construction method (traverse a directory)
Only the name is returned

Recursive
Recursive: The method calls itself
Category:
Recursion is divided into two types, direct recursion and indirect recursion.
Direct recursion is called the method itself calling itself. Indirect recursion allows method A to call method B, method B to call method C, and method C to call method A.

Note:
1. Recursion must be conditionally qualified to ensure that the recursion can be stopped, otherwise stack memory overflow will occur.
2. Although there are restrictions in recursion, the number of recursions cannot be too many. Otherwise, stack memory overflow will also occur.
3. Constructor method, recursion is prohibited

 1 使用递归计算1-n之间的和 2       n + (n-1)+ (n-2)+(n-3)+...+1 3       5 +(5-1)+(4-1)+(3-1)+(2-1) 4       结束条件:n=1的时候结束 5       方法自己调用自己目的:获取n-1,获取到n=1的时候结束 6     public static int DGSum(int n){ 7         //添加结束条件 8         if(n==1){ 9             return 1;10         }11         return n+DGSum(n-1);12     } 
13 14 使用递归计算阶乘15      private static long DGJC(int n) {16         //递归的结束条件 n==117         if(n==1){18             return 1;19         }20         return n*DGJC(n-1);21     }22 23 使用递归计算斐波那契数列    
24     private static int fbnq(int month) {25         //结束条件如果month是1,2直接返回126         if(month==1 || month==2){27             return 1;28         }29         //3月以上:兔子数量是前两个月之和30         return fbnq(month-1)+fbnq(month-2);31     }

文件过滤器
文件的过滤器:
需求:遍历hello文件夹,只获取文件夹中的.java结尾的文件
c:\\hello
c:\\hello\\demo.txt
c:\\hello\\Hello.java

在File类中listFiles()方法是遍历文件夹的方法
有两个和 listFiles重载的方法,参数传递的就是过滤器
File[] listFiles(FileFilter filter)
File[] listFiles(FilenameFilter filter)
返回抽象路径名数组,这些路径名表示此抽象路径名表示的目录中满足指定过滤器的文件和目录。  
发现方法的参数FileFilter和FilenameFilter是接口
所有我们需要自己定义接口的实现类,重写接口中的方法accept,实现过滤功能

 1 public class FileFilterImpl implements FileFilter{ 2     /* 3      * 实现过滤的方法: 4         1.把传递过来的路径pathname,变成字符串 5             Stirng s  = pathname.toString(); "c:\\hello\\demo.txt" 6             String s  = pathname.getPaht(); "c:\\hello\\demo.txt" 7             String s = pathname.getName(); "demo.txt" 8         2.使用String类中的方法endsWith判断字符串是否以指定的字符串结尾 9             boolean b = s.endsWith(".java");10             return b;11      */12     @Override13     public boolean accept(File pathname) {14         /*String s = pathname.getName();15         boolean b = s.endsWith(".java");16         return b;*/17         return pathname.getName().toLowerCase().endsWith(".java");18     }19 }20 public class FilenameFilterImpl implements FilenameFilter{21 22     @Override23     public boolean accept(File dir, String name) {24         return name.toUpperCase().endsWith(".JAVA");25     }26 27 }

断点调试
 debug断点调试
 f6:逐行执行
 f5:进入到方法中
 f7:结束方法
 f8:跳到下一个断点
 watch:捕获

The above is the detailed content of File overview and usage introduction. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
鸿蒙原生应用随机诗词鸿蒙原生应用随机诗词Feb 19, 2024 pm 01:36 PM

想了解更多关于开源的内容,请访问:51CTO鸿蒙开发者社区https://ost.51cto.com运行环境DAYU200:4.0.10.16SDK:4.0.10.15IDE:4.0.600一、创建应用点击File->newFile->CreateProgect。选择模版:【OpenHarmony】EmptyAbility:填写项目名,shici,应用包名com.nut.shici,应用存储位置XXX(不要有中文,特殊字符,空格)。CompileSDK10,Model:Stage。Device

使用java的File.length()函数获取文件的大小使用java的File.length()函数获取文件的大小Jul 24, 2023 am 08:36 AM

使用Java的File.length()函数获取文件的大小文件大小是在处理文件操作时很常见的一个需求,Java提供了一个很方便的方法来获取文件的大小,即使用File类的length()方法。本文将介绍如何使用该方法来获取文件的大小,并给出相应的代码示例。首先,我们需要创建一个File对象来表示我们想要获取大小的文件。以下是创建File对象的方法:Filef

php blob怎么转filephp blob怎么转fileMar 16, 2023 am 10:47 AM

php blob转file的方法:1、创建一个php示例文件;2、通过“function blobToFile(blob) {return new File([blob], 'screenshot.png', { type: 'image/jpeg' })}”方法实现Blob转File即可。

使用java的File.renameTo()函数重命名文件使用java的File.renameTo()函数重命名文件Jul 25, 2023 pm 03:45 PM

使用Java的File.renameTo()函数重命名文件在Java编程中,我们经常需要对文件进行重命名的操作。Java提供了File类来处理文件操作,其中的renameTo()函数可以方便地重命名文件。本文将介绍如何使用Java的File.renameTo()函数来重命名文件,并提供相应的代码示例。File.renameTo()函数是File类的一个方法,

如何解决Python的最大递归深度错误?如何解决Python的最大递归深度错误?Jun 24, 2023 pm 02:48 PM

Python是一门易学易用的编程语言,然而在使用Python编写递归函数时,可能会遇到递归深度过大的错误,这时就需要解决这个问题。本文将为您介绍如何解决Python的最大递归深度错误。1.了解递归深度递归深度是指递归函数嵌套的层数。在Python默认情况下,递归深度的限制是1000,如果递归的层数超过这个限制,系统就会报错。这种报错通常称为“最大递归深度错误

使用java的File.getParentFile()函数获取文件的父目录使用java的File.getParentFile()函数获取文件的父目录Jul 27, 2023 am 11:45 AM

使用java的File.getParentFile()函数获取文件的父目录在Java编程中,我们经常需要操作文件和文件夹。当我们需要获取文件的父目录时,可以使用Java提供的File.getParentFile()函数来完成。本文将介绍如何使用这个函数并提供代码示例。Java中的File类是用于操作文件和文件夹的主要类。它提供了许多方法来获取和操作文件的属性

如何使用Vue表单处理实现表单的递归嵌套如何使用Vue表单处理实现表单的递归嵌套Aug 11, 2023 pm 04:57 PM

如何使用Vue表单处理实现表单的递归嵌套引言:随着前端数据处理和表单处理的复杂性不断增加,我们需要通过一种灵活的方式来处理复杂的表单。Vue作为一种流行的JavaScript框架,为我们提供了许多强大的工具和特性来处理表单的递归嵌套。本文将向大家介绍如何使用Vue来处理这种复杂的表单,并附上代码示例。一、表单的递归嵌套在某些场景下,我们可能需要处理递归嵌套的

使用java的File.getParent()函数获取文件的父路径使用java的File.getParent()函数获取文件的父路径Jul 24, 2023 pm 01:40 PM

使用java的File.getParent()函数获取文件的父路径在Java编程中,我们经常需要操作文件和文件夹。有时候,我们需要获取一个文件的父路径,也就是该文件所在文件夹的路径。Java的File类提供了getParent()方法用于获取文件或文件夹的父路径。File类是Java对文件和文件夹的抽象表示,它提供了一系列操作文件和文件夹的方法。其中,get

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools