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!