Java SE Chapter 8 Streams and Files-Files

Java SE Chapter 8 Streams and Files-Files

1. The File class can obtain file information and manage files. The File object can represent both a file and a directory, and it can be used to perform basic operations on files, directories and their attributes.

File常用方法列表如下:
File(String pathname)
boolean canRead()
boolean createNewFile()
boolean delete()
boolean exists()
String getAbsolutePath()
String getName()
String getPath()
boolean isDirectory()
boolean isFile()
long length()
boolean mkdir()
boolean renameTo(File dest)
long lastModified()

Example: Create a File object to check whether the file exists, if it does not exist, create it, and then demonstrate part of the operations of the File class, such as the name and size of the file.

package ch08;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Scanner;
public class FileDemo {
    
    
	public static void main(String[] args) {
    
    
		System.out.println("请输入文件名:");
		Scanner scanner=new Scanner(System.in);
		String pathName=scanner.next();
		File file=new File(pathName);
		if(!file.exists()){
    
    
			try{
    
    
				file.createNewFile();
			}catch(IOException e){
    
    
				e.printStackTrace();
			}
		}
		System.out.println("文件是否存在:"+file.exists());
		System.out.println("是文件吗:"+file.isFile());
		System.out.println("是目录吗:"+file.isDirectory());
		System.out.println("名称:"+file.getName());
		System.out.println("路径:"+file.getPath());
		System.out.println("绝对路径:"+file.getAbsolutePath());
		System.out.println("最后修改时间:"+new Date(file.lastModified()).toString());
		System.out.println("文件大小:"+file.length());
	}
}

The lastModified() method returns the last modification time of the file.
The time is a long integer, which is the number of milliseconds between the point in time (1970.01.01, 00:00:00 GMT).
Therefore, it is encapsulated by the Date class, that is, a new Date object, whose parameter is the number of milliseconds obtained, and then you can use the toString() method to display the time. The format is:
Dow (day of the week) mon (month) dd (day of the week) Digit decimal) hh (some time with two decimals) mm (minutes with two decimals) ss (seconds with two decimals) zzz (standard time zone abbreviation) yyyy (year)
screenshot:
Insert picture description here

2. List
method list of the File class of the file lister :
String[] list()
File[]listFiles()
Example: define a class to demonstrate the use of the list() method to list the names of files or directories in the jdk root directory in turn .

package ch08;
import java.io.File;
public class ListDemo {
    
    
	public static void main(String[] args) {
    
    
		File file=new File("D:\\jdk");
		if(file.isDirectory()){
    
    
			String[]fileNames=file.list();
			for(String fileName :fileNames){
    
    
				System.out.println(fileName);
			}
		}
	}

The screenshot

List() method lists all the files or directories in the jdk root directory, but does not indicate which file or directory is.

Example: Use the listFiles() method to list the names of the directories or files in the jdk root directory, and mark the files or directories.

package ch08;
import java.io.File;
public class ListDemo {
    
    
	public static void main(String[] args) {
    
    
		File file=new File("D:\\jdk");
		if(file.isDirectory()){
    
    
			File[]files=file.listFiles();
			for(File f :files){
    
    
				if(f.isFile()){
    
    
					System.out.println("文件:"+f);
				}else{
    
    
				System.out.println("目录:"+f);
			}
		}
	}
}
}
}

Screenshot
Insert picture description here
3. List method with filter conditions:
String[] list(FileNameFilter filter)
File[] listFiles(FileNameFilter filter)

FileNameFilter is an interface, it has only one accept() method, so you only need to define a class to implement this interface, or you can define an anonymous class.

Example: Define a class to demonstrate the use of the list() method to list all webpage files with the suffix html or htm in the jdk root directory.

package ch08;
import java.io.File;
import java.io.FilenameFilter;
public class HtmlList {
    
    
	public static void main(String[] args) {
    
    
		File file=new File("D:\\jdk");
		if(file.exists()&&file.isDirectory()){
    
    
			String[]fileNames=file.list(new FilenameFilter(){
    
    
				public boolean accept(File dir,String name){
    
    
					return (name.endsWith(".html")||name.endsWith("htm"));
				}
			});
			for(String fileName:fileNames){
    
    
				System.out.println(fileName);
			}
		}
	}
}

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_45618376/article/details/111394126