import java.io.File;
import java.io.IOException;
/*
演示创建文件
*/
public class FileCreate {
public static void main(String[] args) {
create03();
}
//方式一 new File(String pathname)
public static void create01(){
String filePath = "news1.txt";
File file = new File(filePath);
try {
file.createNewFile();
System.out.println("文件创建成功");
} catch (IOException e) {
throw new RuntimeException(e);
}
}
//方式二 new File(File parent,String child) //根据父目录文件+子路径构建
public static void create02(){
File parentFile = new File("Data");
String fileName = "news2.txt";
File file = new File(parentFile,fileName);
try {
file.createNewFile();
System.out.println("文件创建成功");
} catch (IOException e) {
throw new RuntimeException(e);
}
}
//方式三 new File(String parent,String child) //根据父级目录+子目录创建文件
public static void create03(){
String parentPath = "Data/";
String fileName = "news3.txt";
File file = new File(parentPath,fileName);
try {
file.createNewFile();
System.out.println("文件创建成功");
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Java 在磁盘中创建文件的三种方式
最新推荐文章于 2023-07-25 10:08:38 发布
1013

被折叠的 条评论
为什么被折叠?



