


Introduction to the construction method and common functions of the File class
首先我们来介绍一个路径的问题,路径分为绝对路径和相对路径。绝对路径是一个固定的路径,从盘符开始;相对路径相对于某个位置,在eclipse下是指当前项目下。
(推荐教程:java课程)
(1)File中的构造方法:
File(String pathname):根据一个路径得到File对象
File(String parent, String child):根据一个目录和一个子文件/目录得到File对象
File(File parent, String child):根据一个父File对象和一个子文件/目录得到File对象
(2)File中的创建功能:
public boolean createNewFile():创建文件 如果存在这样的文件,就不创建了
public boolean mkdir():创建文件夹 如果存在这样的文件夹,就不创建了
public boolean mkdirs():创建文件夹,如果父文件夹不存在,会帮你创建出来
File file = new File("yyy.txt"); //可以加后缀,也可以不加后缀 System.out.println(file.createNewFile());
File dir1 = new File("aaa"); //创建文件夹 System.out.println(dir1.mkdirs());
File dir2 = new File("bbb.txt"); //文件夹也可以加后缀 System.out.println(dir2.mkdirs()); File dir3 = new File("ccc\\ddd"); System.out.println(dir3.mkdirs()); //创建多级目录(文件夹)
(3)重命名和删除功能:
public boolean renameTo(File dest):把文件重命名为指定的文件路径
public boolean delete():删除文件或者文件夹
注意:如果路径名相同,就是改名。如果路径名不同,就是改名并剪切。
File file1 = new File("xxx.txt"); File file2 = new File("ooo.txt"); System.out.println(file1.renameTo(file2)); File file3 = new File("D:\\XXX.txt"); System.out.println(file2.renameTo(file3));
File file1 = new File("yyy.txt"); System.out.println(file1.delete()); //true File file2 = new File("aaa"); System.out.println(file2.delete()); //true File file3 = new File("ccc"); //如果删除一个文件夹,那么此文件夹必须为空 System.out.println(file3.delete()); //false
(相关教程推荐:java入门教程)
(4)判断功能:
public boolean isDirectory():判断是否是目录
public boolean isFile():判断是否是文件
public boolean exists():判断是否存在
public boolean canRead():判断是否可读
public boolean canWrite():判断是否可写
public boolean isHidden():判断是否隐藏
(5)获取功能:
public String getAbsolutePath():获取绝对路径public String getPath():获取路径public String getName():获取名称public long length():获取长度。字节数public long lastModified():获取最后一次的修改时间,毫秒值public String[] list():获取指定目录下的所有文件或者文件夹的名称数组public File[] listFiles():获取指定目录下的所有文件或者文件夹的File数组
(6)文件名称过滤器的概述public String[] list(FilenameFilter filter)public File[] listFiles(FileFilter filter)
public static void main(String[] args) { File dir = new File("D:\\"); String[] arr = dir.list(new FilenameFilter(){ @Override public boolean accept(File dir, String name) { //System.out.println(dir); dir是盘符D盘 //System.out.println(name); name是文件名 File file = new File(dir,name); return file.isFile() && file.getName().endsWith(".txt"); } }); //数组里存储的全是符合条件的 for (String string : arr) { System.out.println(string); //BugReport.txt } }
The above is the detailed content of Introduction to the construction method and common functions of the File class. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Use Java's File.length() function to get the size of a file. File size is a very common requirement when dealing with file operations. Java provides a very convenient way to get the size of a file, that is, using the length() method of the File class. . This article will introduce how to use this method to get the size of a file and give corresponding code examples. First, we need to create a File object to represent the file we want to get the size of. Here is how to create a File object: Filef

To learn more about open source, please visit: 51CTO Hongmeng Developer Community https://ost.51cto.com Running environment DAYU200:4.0.10.16SDK: 4.0.10.15IDE: 4.0.600 1. To create an application, click File- >newFile->CreateProgect. Select template: [OpenHarmony] EmptyAbility: Fill in the project name, shici, application package name com.nut.shici, and application storage location XXX (no Chinese, special characters, or spaces). CompileSDK10, Model: Stage. Device

How to convert php blob to file: 1. Create a php sample file; 2. Through "function blobToFile(blob) {return new File([blob], 'screenshot.png', { type: 'image/jpeg' })} ” method can be used to convert Blob to File.

Use Java's File.renameTo() function to rename files. In Java programming, we often need to rename files. Java provides the File class to handle file operations, and its renameTo() function can easily rename files. This article will introduce how to use Java's File.renameTo() function to rename files and provide corresponding code examples. The File.renameTo() function is a method of the File class.

Use java's File.getParent() function to get the parent path of a file. In Java programming, we often need to operate files and folders. Sometimes, we need to get the parent path of a file, which is the path of the folder where the file is located. Java's File class provides the getParent() method to obtain the parent path of a file or folder. The File class is Java's abstract representation of files and folders. It provides a series of methods for operating files and folders. Among them, get

Use java's File.getParentFile() function to get the parent directory of a file. In Java programming, we often need to operate files and folders. When we need to get the parent directory of a file, we can use the File.getParentFile() function provided by Java. This article explains how to use this function and provides code examples. File class in Java is the main class used to operate files and folders. It provides many methods to obtain and manipulate file properties

How to delete a file or directory using File.delete() method in Java? Overview: In Java, we can delete a file or directory using the delete() method of the File class. This method is used to delete the specified file or directory. However, it should be noted that this method can only delete empty directories or files that are not opened by other programs. If file or directory deletion fails, you can find the specific reason by catching IOException. Step 1: Import related packages First, we need

Create multi-level directories using Java's File.mkdirs() function In Java, we often need to create folders to store and organize files. Sometimes, we need to create multi-level directories, which are folders containing subfolders. Java provides the mkdirs() function of the File class to implement this function. The File class is a class in Java that handles files and directories. It provides a series of methods for operating files and directories. Among them, the mkdirs() function is a function that creates multi-level directories. Down
