In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail what is the use of the File class in java. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
File class
This article mainly introduces some common methods of the File class. First, let's look at some important concepts:
An object of the File class that represents a file or directory on disk.
File provides a platform-independent way to manipulate files or directories on disk.
The File class deals directly with files and file systems.
The File class does not specify how information is read from or stored in a file.
Package io
Import java.io.File
Import java.io.IOException
Public class FileTest
{
Public static void main (String [] args) throws IOException
{
File file = new File ("f:/migu")
File.mkdir ()
/ / determine whether the directory represented by the abstract name exists
If (file.exists () & & file.isDirectory ())
{
System.out.println ("migu directory exists")
File file1 = new File ("f:/migu/UES.txt")
File file2 = new File ("f:\\ migu\\ CMU.txt")
/ / create a file
File1.createNewFile ()
File2.createNewFile ()
File file3 = new File ("f:/migu/ plug-in / payment Center")
/ / create the directory represented by this abstract name, as well as all required but non-existent parent directories
File3.mkdirs ()
File [] files = file.listFiles ()
/ / this method returns all files and directories at this layer under the path
For (File f: files)
{
System.out.println ("File name in the migu directory:" + f.getName ())
System.out.println ("absolute path to the migu directory file:" + f.getAbsolutePath ())
}
}
Else
{
System.out.println ("migu directory does not exist")
}
}
}
Execution result:
Migu directory exists
File name under the migu directory: CMU.txt
Absolute path to the migu directory file: F:\ migu\ CMU.txt
File name under the migu directory: UES.txt
Absolute path to the migu directory file: F:\ migu\ UES.txt
File name in the migu directory: plug-in
Absolute path to the migu directory file: F:\ migu\ plug-in
The methods shown in this DEMO related to the File class are easy to understand, and there are two areas that need to be emphasized:
1. Mkdirs creates a file or directory represented by an abstract name, and also creates that directory or all non-existent parent directories of the file.
2. In lines 16 and 17, I use two different path separators to represent the abstract path, which is feasible from the running results. So what's the difference between the two?
File separator under different operating systems: "\" in windows and "/" in linux. If you want to use "\" to read files on disk in windows system with JAVA code, it must be written as "\", because an "\" represents an escape character in java. So use "\" for "\". My advice here is to write "/" as much as possible, because "/" is never a problem in all operating systems.
This is the end of this article on "what is the use of File classes in java". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.