I/O的基礎(chǔ)demo-文件對(duì)象

基本的創(chuàng)建文件

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Date;

public class TestCreateFile {
    public static void main(String[] args) {
        //先創(chuàng)建一個(gè)文件夾
        File scFileFolder = new File("e:/SCfile");
        if(scFileFolder.exists()){
            System.out.println("確定路徑存在 文件夾創(chuàng)建成功"+scFileFolder.getAbsolutePath());
        }
        //然后在文件下面創(chuàng)建一個(gè)txt文件(空的里面啥都沒(méi)有)
        try {
            File file = new File(scFileFolder,"maru.txt");
            if (file.getParentFile().exists()) {
                if (file.exists()) {
                    System.out.println("文件已經(jīng)存在");
                } else {
                    file.createNewFile();
                }
            }
        }catch (IOException e){
            e.printStackTrace();
        }
        //另一種方式 Path 和Files 是java7里面引入的
        Path filepath = Paths.get("e:/SCfile","seral.txt");
        try {
            if (Files.exists(filepath)){
                System.out.println("文件已經(jīng)存在");
            }else {
                Files.createFile(filepath);
            }
        }catch (IOException e){
            e.printStackTrace();
        }


    }
}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容