Difference between methods of creating files in java
区别:
mkdir
和mkdirs
:mkdir只能用来创建文件夹,且只能创建一级目录;
mkdirs同样只能用来创建文件夹,可创建多级目录 ,如果上级不存在,就会自动创建。
createNewFile
:只能用来创建文件,且只能在已存在的目录下创建文件。
一般情况下配合使用,附上一段代码,会在自定义的目录下创建名为111的docx文件,将inputString字符串内容写入其中。
想学习java么,这里有免费视频教程:java教学视频
示例演示如下:
import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; public class Modify { public static void main(String[] args) throws IOException { String path = "F:\\Users\\yyy\\Desktop\\111.docx"; Modify modify = new Modify(); modify.create("hhh",path); } /** * * @param inputString 需写入的字符串内容 * @param path 文件创建的路径 * @throws IOException */ private void create(String inputString,String path) throws IOException { String newPath = path.substring(0,path.lastIndexOf("\\")); File file = new File(newPath); if (!file.exists()){ file.mkdirs(); } File newFile = new File(path); if (!newFile.exists()){ newFile.createNewFile(); } ByteArrayInputStream input = new ByteArrayInputStream(inputString.getBytes()); int index; byte[] bytes = new byte[1024]; FileOutputStream fs = new FileOutputStream(path); while ((index = input.read(bytes)) != -1) { fs.write(bytes, 0, index); fs.flush(); } fs.close(); input.close(); } }
大家都在查看的教程:java编程入门
The above is the detailed content of Difference between methods of creating files in java. 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











Created by Ripple, Ripple is used for cross-border payments, which are fast and low-cost and suitable for small transaction payments. After registering a wallet and exchange, purchase and storage can be made.

AI can help optimize the use of Composer. Specific methods include: 1. Dependency management optimization: AI analyzes dependencies, recommends the best version combination, and reduces conflicts. 2. Automated code generation: AI generates composer.json files that conform to best practices. 3. Improve code quality: AI detects potential problems, provides optimization suggestions, and improves code quality. These methods are implemented through machine learning and natural language processing technologies to help developers improve efficiency and code quality.

The main differences between Laravel and Yii are design concepts, functional characteristics and usage scenarios. 1.Laravel focuses on the simplicity and pleasure of development, and provides rich functions such as EloquentORM and Artisan tools, suitable for rapid development and beginners. 2.Yii emphasizes performance and efficiency, is suitable for high-load applications, and provides efficient ActiveRecord and cache systems, but has a steep learning curve.

The difference between decentralized exchanges and hybrid exchanges is mainly reflected in: 1. Trading mechanism: Decentralized exchanges use smart contracts to match transactions, while hybrid exchanges combine centralized and decentralized mechanisms. 2. Asset control: Decentralized exchange users control assets, and mixed exchange ownership centralization and decentralization. 3. Privacy protection: Decentralized exchanges provide high anonymity, and hybrid exchanges require KYC in centralized mode. 4. Trading speed and liquidity: Decentralized exchanges are slower, liquidity depends on user pool, and hybrid exchanges are more fast and liquid in centralized mode. 5. Platform governance: Decentralized exchanges are governed by community governance, and hybrid exchanges are jointly governed by communities and centralized teams.

Java's platform independence means that the code written can run on any platform with JVM installed without modification. 1) Java source code is compiled into bytecode, 2) Bytecode is interpreted and executed by the JVM, 3) The JVM provides memory management and garbage collection functions to ensure that the program runs on different operating systems.

HTML5 brings five key improvements: 1. Semantic tags improve code clarity and SEO effects; 2. Multimedia support simplifies video and audio embedding; 3. Form enhancement simplifies verification; 4. Offline and local storage improves user experience; 5. Canvas and graphics functions enhance the visualization of web pages.

The difference between ISTRUE and =True query conditions in MySQL In MySQL database, when processing Boolean values (Booleans), ISTRUE and =TRUE...

Neither Huoxin nor OKX Pay directly supports fiat currency payment. Huoxin is mainly used for digital asset management and transactions, and users need to exchange fiat currency through the Huobi Exchange; OKX Pay focuses on digital asset payment and transfer, and users need to exchange fiat currency through the OKX platform.
