内容来自于韩顺平学Java

在学习其视频下跟着编写

package com.hsp.file;import java.io.File;import java.io.IOException;public class FileCreate {public static void main(String[] args) {new FileCreate().create01();new FileCreate().create02();new FileCreate().createFile03();}public void create01(){//创建文件的方法01String filepath = "E://123.txt";File file = new File(filepath);try {file.createNewFile();System.out.println("文件创建成功!");} catch (IOException e) {throw new RuntimeException(e);}}public void create02(){//父目录文件+子路径构成File parentFile = new File("e:\\");String path = "new123.txt";//这里下面的File对象,在java程序中,只是一个对象,只有当createNewFile时才是真正把对象写入或者创建到磁盘中去File file = new File(parentFile, path);try {//try catch快捷键,alt+ctrl+Tfile.createNewFile();} catch (IOException e) {throw new RuntimeException(e);}}public void createFile03(){String parentPath = "e:\\";String fileName = "new12303.txt";File file = new File(parentPath, fileName);try {file.createNewFile();System.out.println("new12303版创建成功");} catch (IOException e) {throw new RuntimeException(e);}}}

文件创建成功