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 create a folder in java

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

In this issue, the editor will bring you about how to create a folder in java. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

1. Traditional way of creating folders in API

The traditional IO API of Java uses the file.mkdir () and file.mkdirs () methods in the java.io.File class to create folders

File.mkdir () returns true if the folder is created successfully and false if it fails. Returns false if the parent folder of the created folder does not exist. No exception was thrown.

File.mkdirs () creates a folder along with the folder's parent folder, and returns true if the creation is successful, or false if the creation fails. The creation failure also has no exception thrown.

[@ Test] (https://my.oschina.net/azibug)void testCreateDir1 () {/ / "D:\ data111" directory does not exist now String dirStr = "D:\\ data111\\ test"; File directory = new File (dirStr); / / mkdir boolean hasSucceeded = directory.mkdir (); System.out.println ("create folder result (excluding parent folder):" + hasSucceeded); / / mkdirs hasSucceeded = directory.mkdirs () System.out.println ("create folder result (including parent folder):" + hasSucceeded);}

The output is as follows: failed to create using mkdir, created successfully using mkdirs.

Create folder result (excluding parent folder): false create folder result (including parent folder): true

As you can see, mkdir and mkdirs can create files, but they are very unfriendly in exception handling. Unified false is returned after the creation failure. The reason for the creation failure is not specified. The creation failed because the parent folder does not exist? Or did the creation fail because the folder already exists? Or did you fail to create a folder because of disk IO?

2. Create a folder by Java NIO

In order to solve the problem of unclear handling of exception failures in traditional IO creation folders, improvements have been made in Java's NIO.

2.1. Files.createDirectory create folder

If the parent folder of the folder being created does not exist, NoSuchFileException.

If the created folder already exists, FileAlreadyExistsException. Exe is thrown.

If an exception occurs because of disk IO, IOException. Exe.

Path path = Paths.get ("D:\\ data222\\ test"); Path pathCreate = Files.createDirectory (path); 2.2.Files.createDirectories create folder and its parent folder

If the parent folder of the folder being created does not exist, create it

If the created folder already exists, the folder that already exists will not be created repeatedly and no exception will be thrown.

If an exception occurs because of disk IO, IOException. Exe.

Path path = Paths.get ("D:\\ data222\\ test"); Path pathCreate = Files.createDirectorys (path)

In addition, you should note that the return value of the folder created by NIO's API is Path, so it is convenient for us to continue to write file data to the folder after creating the folder. It is much better than traditional IO to return only one Boolean value.

This is how to create a folder in the java shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

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

Internet Technology

Wechat

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

12
Report