Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the File class in Java

2025-02-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "how to use File class in Java". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to use File class in Java" can help you solve the problem.

Overview

To learn the File class in Java, we first need to know that the definition of the File class in Java's API, that is, the abstract representation of file and directory paths, is not the file itself. So in the File class, the operations involved are only about the file name and directory path, not the file content. If you want to manipulate the file content, you also need to learn the Imax O stream (input / output stream).

Basic introduction of construction method

Only when the File class is constructed can we manipulate it later. For the construction of the File class, there are three methods:

Pathnames of files and directories represented abstractly:

Public File (String pathname): creates a new File instance by converting the given pathname string to an abstract pathname.

Public File (String parent, String child): creates a new File instance from the parent path name string and the child path name string.

Public File (File parent, String child): creates a new File instance from the parent abstract pathname and child pathname strings.

The first one is used more, and the last two are used less.

Code example import java.io.File;public class FileDemo1 {public static void main (String [] args) {/ / public File (String pathname) String pathname1 = "F:\\ test1.txt"; File file1 = new File (pathname1); / / public File (String pathname) String pathname2 = "F:\ test2\\ test1.txt"; File file2 = new File (pathname2) / / public File (String parent, String child) String parent = "F:\\ test3"; String child = "test1.txt"; File file3 = new File (parent,child); / / public File (File parent, String child) File parentDir = new File ("F:\\ test"); String child1 = "test2.txt"; File file4 = new File (parentDir,child1) }} Common methods

The operation of the File class is still very important, so there are many common methods for it. To make it easier to remember, I divide it into:

Get basic information about files and directories

Judgment function

Create and delete featur

Directory traversal function

Below, these methods are introduced and illustrated with examples.

Get basic information about files and directories

Public String getName (): returns the name of the file or directory represented by this File.

Public String getPath (): converts this File to a pathname string.

Public String getParent (): get the parent path

Public String getAbsolutePath (): returns the absolute pathname string for this File

Code example

Import java.io.File;public class FileDemo2 {public static void main (String [] args) {File file = new File ("F:\\ test1\\ test2\\ text.txt"); / / convert this File to a pathname string. System.out.println ("File path:" + file.getPath ()); / / returns the name of the file or directory represented by this File System.out.println ("File name:" + file.getName ()); / / gets the parent path System.out.println ("get parent path" + file.getParent ()) / / returns the absolute pathname string System.out.println ("absolute pathname" + file.getAbsolutePath ()) of this File;}}

Judgment function

Public boolean exists (): whether the file or directory represented by this File actually exists.

Public boolean isDirectory (): whether this File represents a directory.

Public boolean isFile (): whether this File represents a file.

Public isAbsolute (): determines whether the file or directory corresponding to the File object is an absolute path

Public boolean canRead (): determines whether the file or directory corresponding to the File object is readable

Public boolean canWrite (): determines whether the file or directory corresponding to the File object is writable

Public boolean isHidden (): determines whether the file or directory corresponding to the File object is hidden

If the file or directory does not exist, then exists (), isFile (), and isDirectory () all return true

Code example

Import java.io.File;public class FileDemo3 {public static void main (String [] args) {File file1 = new File ("F:\ test\\ test1\\ test2.txt"); File file2 = new File ("F:\\ test"); / / whether the file or directory represented by this File actually exists boolean exists1 = file1.exists (); boolean exists2 = file2.exists (); System.out.println (exists1) System.out.println (exists2); / / whether this File represents a directory boolean directory1 = file1.isDirectory (); boolean directory2 = file2.isDirectory (); System.out.println (directory1); System.out.println (directory2); / / whether this File represents a file boolean file1File = file1.isFile (); boolean file2File = file2.isFile () System.out.println (file1File); System.out.println (file2File); / / determine whether the file or directory corresponding to the File object is the absolute path boolean absolute = file1.isAbsolute (); System.out.println (absolute); / / determine whether the file or directory corresponding to the File object is readable boolean canRead = file1.canRead (); System.out.println (canRead) / / determine whether the file or directory corresponding to the File object is writable boolean canWrite = file1.canWrite (); System.out.println (canWrite); / / determine whether the file or directory corresponding to the File object is hidden boolean hidden = file2.isHidden (); System.out.println (hidden);} create and delete function

Public boolean createNewFile (): creates a new empty file if and only if a file with that name does not already exist.

Public boolean delete (): deletes the file or directory represented by this File. Only empty directories can be deleted.

Public boolean mkdir (): create the directory represented by this File.

Public boolean mkdirs (): create the directory represented by this File, including any required but non-existent parent directories

Note in API: delete method, if this File represents a directory, the directory must be empty before it can be deleted.

Code example

Import java.io.File;import java.io.IOException;public class FileDemo4 {public static void main (String [] args) throws IOException {/ / create file File file1 = new File ("test.txt"); System.out.println ("exist:" + file1.exists ()); / / false System.out.println ("create" + file1.createNewFile ()) / / need to throw an exception System.out.println ("exist:" + file1.exists ()); / / true / / create a directory (single) File file2 = new File ("F:\\ test"); System.out.println ("exist:" + file2.exists ()); / / false System.out.println ("create" + file2.mkdir ()) System.out.println ("exist:" + file2.exists ()); / / true / / create a multi-level directory File file3 = new File ("F:\\ test1\\ test2"); System.out.println ("exist:" + file3.exists ()); / / false System.out.println ("create" + file3.mkdirs ()) System.out.println ("exist:" + file3.exists ()); / / true / / deletion System.out.println of files (file1.delete ()); / / deletion of directories System.out.println (file2.delete ()); System.out.println (file3.delete ()); / / only first-level}} directory traversal can be deleted

Public String [] list (): returns an array of String representing all the subfiles or directories in the File directory

Public File [] listFiles (): returns an array of File representing all the subfiles or directories in the File directory

The File object that calls the listFiles method must represent an actual directory, otherwise it returns null and cannot be traversed.

Code example

Import org.junit.Test;import java.io.File;import java.io.IOException;public class FileDemo5 {public static void main (String [] args) {File dir = new File ("F:\\ test"); dir.mkdirs (); System.out.println (dir.exists ()); / / File dir1 = new File ("F:\\ test") / / get the names of files and folders in the current directory String [] names = dir.list (); / / traverse for (String name: names) {System.out.println (name) } / / get the files and folder objects in the current directory. As long as you get the file objects, you can get more information File [] files = dir.listFiles (); for (File file: files) {System.out.println (file);}

But for traversing multi-level directories, we need to use recursive methods

/ * Printing of multi-level directories. There is no way to know how many directories there are before traversing, so we can use the recursive implementation * / import java.io.File;public class FileDemo6 {public static void main (String [] args) {File dir = new File ("F:\\ test1"); listSubFiles (dir) } private static void listSubFiles (File dir) {if (dir! = null & & dir.isDirectory ()) {File [] listFiles = dir.listFiles (); if (dir! = null) {for (File sub: listFiles) {listSubFiles (sub) / / Recursive call} System.out.println (dir);}} add: move the file public class Test5 {public static void main (String [] args) {File F1 = new File ("d:\\"); moveFile (F1);} public static void moveFile (File dir) {File [] files=dir.listFiles () For (File file:files) {if (file.isDirectory ()) moveFile (file); else {if (file.getName (). EndsWith (".java")) file.renameTo (new File ("c:\\ jad\" + file.getName (). Substring (0meme file.getName (). LastIndexOf ('.)) + ".jad") This is the end of the introduction on "how to use the File class in Java". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report