java新建一个文件夹和文件 thanks

zgycsmb 2012-05-22 11:46:33
我想在d盘根目录下,新建一个文件夹 demosun 再在此文件夹下新建一个demosu.txt文件

这如何用代码实现呀 thanks
...全文
188 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
a395885670 2012-05-22
  • 打赏
  • 举报
回复
File file = new File("D:\\demosun");
if (!file.exists()){
file.mkdirs() ;
}
file = new File("D:\\demosun\\demosu.txt") ;
if (!file.exists()){
bool b = file.createNewFile();
if (b){
System.out.println("创建成功。") ;
}
}
ningtieming 2012-05-22
  • 打赏
  • 举报
回复
package test;

import java.io.File;
import java.io.IOException;

public class Test {
public static void main(String []args){
File directory = new File("D:\\demosun");
if(!directory.exists()){
directory.mkdirs();
}
File file = new File("D:\\demosun\\demosu.txt");
try{
if(!file.exists()){
file.createNewFile();
}
}
catch(IOException e){
e.printStackTrace();
}
if(file.exists()){
System.out.println("文件夹及文件创建成功");
}
}
}
_jerrytiger 2012-05-22
  • 打赏
  • 举报
回复

public static void main(String[] args) throws IOException {
File file = new File("d:/demosun/demosu.txt");
if(!file.getParentFile().exists())
file.getParentFile().mkdirs();
if(!file.exists())
file.createNewFile();
}
五哥 2012-05-22
  • 打赏
  • 举报
回复
File file = new File("D:\\demosun");
if (!file.exists()){
file.mkdirs() ;
}
file = new File("D:\\demosun\\demosu.txt") ;
if (!file.exists()){
bool b = file.createNewFile();
if (b){
System.out.println("创建成功。") ;
}
}
古布 2012-05-22
  • 打赏
  • 举报
回复
File f = new File("D:/demosun");
f.mkdirs();
FileWriter fw = new FileWriter("D:/demosun/demosu.txt");
fw.close();
古布 2012-05-22
  • 打赏
  • 举报
回复
File f = new File("D:/demosun");
f.mkdirs();
FileWriter fw = new FileWriter("D:/demosun/demosu.txt");
fw.close();
五哥 2012-05-22
  • 打赏
  • 举报
回复
File file = new File("D:\\demosun");
if (!file.exists()){
file.mkdirs() ;
}
sffx123 2012-05-22
  • 打赏
  • 举报
回复
我擦来晚了

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧