行业知识
Java创建文件的三种方式
2026-12-05 01:09:00
来源:互联网转载
内容来自于韩顺平学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(){ //创建文件的方法01 String 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+T file.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); } }} 文件创建成功
上一篇:西瓜交作业怎么服务器异常?
下一篇:视界服务器崩溃怎么解决?