Common class java class 08_File

1.File class basic usage

java.io.File class: representatives of files and directories. In the development, read files, generated files, delete files, often used to modify the properties of this class when file.

Common File class constructor:public File(String pathname)

Representatives pathname file path, the path may be a relative path or an absolute path. If the pathname is relative path, the default attributes currently stored in the system path in user.dir.

[Example], respectively, to create a File object relative and absolute paths

public static void main(String[] args) throws IOException {
	// 获取当前工作目录路径
	System.out.println(System.getProperty("user.dir"));
	// 1.相对路径创建File对象:默认放到user.dir目录下面
	File file1 = new File("abc.txt");
	// 创建文件
	file1.createNewFile();
	// 2.绝对路径创建File对象
	File file2 = new File("D:/abc.txt");
	// 创建文件
	file2.createNewFile();
}

In eclipse project development, user.dir is the directory of the project. Therefore, after the implementation, under this project and the D drive generates a new file (if it is under the eclipse, the directory structure must press F5 to refresh to see the new file). As shown in Figure:
Here Insert Picture Description
other construction methods of the File class, please review the following table:

Construction method description
public File(String pathname) To create a new File instance by converting the given pathname string into an abstract pathname
public File(File parent, String child) Creates a new File instance from a parent abstract pathname and a child pathname string
public File(String parent, String child) Creates a new File instance from a parent pathname string and a child pathname string

2.File class acquisition function

Method name description
public String getName() Returns the name of the file or directory thus abstract pathname represented.
public String getPath() Returns the absolute form of this abstract pathname (absolute or relative path).
public String getAbsolutePath() Returns the absolute form of this abstract pathname (absolute path).
public long lastModified() Returns the abstract pathname of the file was last modified.
public long length() Abstract pathname thereby return the length of the file.

[Example] File class basic acquisition function

// 1.相对路径创建File对象
File file = new File("abc.txt");
// 2.获取文件或目录名字
System.out.println(file.getName());
// 3.返回此抽象路径名的绝对形式(绝对路径或相对路径)。 
System.out.println(file.getPath());
// 4.返回此抽象路径名的绝对形式(绝对路径)。 
System.out.println(file.getAbsolutePath());
// 5.返回此抽象路径名表示的文件最后一次被修改的时间 
System.out.println(file.lastModified());
// 6.返回由此抽象路径名表示的文件的长度。 
Sstem.out.println(file.length());

Class determining function 3.File

Method name description
public boolean exists() File or directory to test this abstract pathname exists.
public boolean isDirectory () Tests whether this abstract pathname of the file is a directory.
public boolean isFile () Tests whether this abstract pathname of the file represented is a standard file.
public boolean isHidden() Tests whether this abstract pathname of the file is a hidden file.
public boolean canRead () Tests whether the application can read this abstract pathname of the file.
cyhoedd boolean canWrite () Tests whether the application can modify this abstract pathname of the file represented.

[Example] File class to determine the function Case

// 1.相对路径创建File对象
File file = new File("abc.txt");
// 2.测试此抽象路径名表示的文件或目录是否存在。 
System.out.println(file.exists());
// 3.测试此抽象路径名表示的文件是否是一个目录。
System.out.println(file.isDirectory());
// 4.测试此抽象路径名表示的文件是否是一个标准文件。
System.out.println(file.isFile());
// 5.测试此抽象路径名指定的文件是否是一个隐藏文件。
System.out.println(file.isHidden());
// 6.测试应用程序是否可以读取此抽象路径名表示的文件。 
System.out.println(file.canRead());
// 7.测试应用程序是否可以修改此抽象路径名表示的文件。 
System.out.println(file.canWrite());

Other features like 4.File

Method name description
public boolean mkdir() Create this abstract pathname specified directory.
public boolean mkdirs () Create this abstract pathname of the directory, including any necessary but nonexistent parent directories.
public boolean createNewFile() If and only if a file with this abstract pathname specified name does not exist, inseparably create a new empty file.
public boolean delete() Delete this abstract pathname of the file or directory.
public boolean renameTo (File) Rename this abstract pathname of the file represented.
public String[] list() Returns an array of strings, these strings specify this abstract pathname of the directory representation of files and directories.
public File[] listFiles() Returns an array of abstract pathnames, which indicates that the file path name directory for this abstract pathname represented.

[Example] File other common functions like case

public static void main(String[] args) throws IOException {
	// 1.mkdir方法使用(G盘下没有demo文件夹)
	File file1 = new File("G:/demo");
	if(!file1.exists()) { // 判断文件夹是够存在
		file1.mkdir(); // 不存在则创建一个文件夹
	}
	// 2.mkdirs方法使用(G盘下没有java和demo文件夹)
	File file2 = new File("G:/java/demo");
	if(!file2.exists()) { // 判断文件夹是够存在
		file2.mkdirs(); // 不存在则创建多个文件夹
	}
	// 3.createNewFile方法使用(G盘下没有abc.txt文件)
	File file3 = new File("G:/abc.txt");
	if(!file3.exists()) { // 判断文件是够存在
		file3.createNewFile(); // 不存在则创建该文件
	}
	// 4.delete方法,删除文件或文件夹
	file1.delete(); // 删除文件文件夹
	file3.delete(); // 删除文件
	// 5.renameTo方法,文件重命名
	file3.renameTo(new File("G:/haha.txt"));
	// 6.list方法,遍历文件和文件夹
	File file4 = new File("G:");
	for(String path : file4.list()) {
		System.out.println(path);
	}
	// 7.listFiles方法,遍历目录中的文件
	for(File file : file4.listFiles()) {
		System.out.println(file);
	}
}

5.File class filter

By listFiles () method, we can get to all the files and folders in a directory, but it can not be filtered? For example, we only want the file extension under a specified directory, or folder that contains the file certain keywords it?

We can put all the files and folders in a directory to get, and get to all content traversing current, traversed

During the screening process, but the action a little trouble, Java provides us with the appropriate functionality to solve this problem.

Now the API of the File class, the File class discovery method listFiles overloaded checking and accepting the specified filter.
Here Insert Picture Description
[Example] get all the files with the extension .java

//自定类继承 FilenameFilter过滤器接口
class MyFilenameFilter implements FilenameFilter {
	@Override
	public boolean accept(File dir, String name) {
		return name.endsWith(".java");
	}
}
// 测试类
public class FileDemo {
	// 获取扩展名为.java所有文件
	public static void main(String[] args) {
		// 创建 File 对象
		File file = new File("C:\\Users\\Administrator\\Desktop\\Java");
		// 获取指定扩展名的文件
		File[] files = file.listFiles(new MyFilenameFilter());
		// 遍历获取到的所有符合条件的文件
		for (File f : files) {
			System.out.println(f.getName());
		}
	}
}

Checking the API, we found that, in listFiles (FileFilter filter) can also accept a FileFilter filter it and we are talking about FilenameFilter so what difference does it make?
Here Insert Picture Description
Accept method FilenameFilter filter accepts two parameters, the current path to a file or folder is located, a name of the current file or folder object.

accept method FileFilter filter accepts a parameter, this parameter is the current file or folder object.

When we need to filter the file name you can use this filter FilenameFilter, when we think of the current file or files

Folder filter, you can use FileFilter, such as the current needs of all files in the directory folder, you can use FileFilter filter.

[Example] to obtain files in the specified directory

//自定类继承 FileFilter过滤器接口
class MyFileFilter implements FileFilter {
	@Override
	public boolean accept(File pathname) {
		return pathname.isFile();
	}
}
// 测试类
public class FileDemo {
	// 获取指定目录下的文件(排除文件夹)
	public static void main(String[] args) {
		// 创建 File 对象
		File file = new File("C:\\Users\\Administrator\\Desktop\\Java");
		// 获取指定目录下的文件
		File[] files = file.listFiles(new MyFileFilter());
		// 遍历获取到的所有符合条件的文件
		for (File f : files) {
			System.out.println(f.getName());
		}
	}
}

ps: For the latest free documentation and instructional videos, please add QQ group (627,407,545) receive.

Published 92 original articles · won praise 0 · Views 2591

Guess you like

Origin blog.csdn.net/zhoujunfeng121/article/details/104784342