Home Java Javagetting Started Introduction to the construction method and common functions of the File class

Introduction to the construction method and common functions of the File class

Aug 28, 2020 pm 03:36 PM
file Common Functions Construction method

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());
Copy after login
File dir1 = new File("aaa");			//创建文件夹
        System.out.println(dir1.mkdirs());
Copy after login
File dir2 = new File("bbb.txt");    		//文件夹也可以加后缀
        System.out.println(dir2.mkdirs()); 
 
File dir3 = new File("ccc\\ddd");   
        System.out.println(dir3.mkdirs());  //创建多级目录(文件夹)
Copy after login

(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));
Copy after login
	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
Copy after login

(相关教程推荐: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
        }
    }
Copy after login

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!

Statement of this Website
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

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

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Use java's File.length() function to get the size of the file Use java's File.length() function to get the size of the file Jul 24, 2023 am 08:36 AM

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

Hongmeng native application random poetry Hongmeng native application random poetry Feb 19, 2024 pm 01:36 PM

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 How to convert php blob to file Mar 16, 2023 am 10:47 AM

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.

Rename files using java's File.renameTo() function Rename files using java's File.renameTo() function Jul 25, 2023 pm 03:45 PM

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 the file Use java's File.getParent() function to get the parent path of the file Jul 24, 2023 pm 01:40 PM

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 the file Use java's File.getParentFile() function to get the parent directory of the file Jul 27, 2023 am 11:45 AM

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? How to delete a file or directory using File.delete() method in Java? Nov 18, 2023 am 08:02 AM

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 Create multi-level directories using java's File.mkdirs() function Jul 24, 2023 am 11:04 AM

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

See all articles