search
HomeJavaJavagetting StartedDifference between methods of creating files in java

Difference between methods of creating files in java

Nov 28, 2019 pm 01:57 PM
javacreatethe differencedocumentmethod

Difference between methods of creating files in java

区别:

mkdirmkdirs: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!

Statement
This article is reproduced at:CSDN. If there is any infringement, please contact admin@php.cn delete

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

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor