温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Java IO创建目录和文件实例代码

发布时间:2020-10-21 12:22:17 来源:脚本之家 阅读:207 作者:彬菌 栏目:编程语言

我们先来看下具体代码:

 import java.io.File; import java.io.IOException; public class CreatFile{	public static void main(String[] args) {	File newDir=new File("D:/test"); //声明磁盘目录	File newFile = new File(newDir,"test.txt"); //声明目录文件	boolean newCreatDir=newDir.exists();	boolean newCreatFile=newFile.exists();	//创建目录及文件	if(newCreatDir==false){	try{ //异常监听	newDir.mkdirs(); //创建目录	System.out.println(newDir.getAbsolutePath()+"目录已创建");	newFile.createNewFile(); //创建文件	System.out.println(newFile.getAbsolutePath()+"文件已创建");	}	catch(IOException e){ //捕捉异常	e.printStackTrace(); //在命令行打印异常信息在程序中出错的位置及原因	}	}	else{	System.out.println(newDir.getAbsolutePath()+"目录已存在");	}	if(newCreatFile==true){	System.out.println(newFile.getAbsolutePath()+"文件已存在");	}	} }

说明:

创建目录的方法,mkdirs();或者mkdir(); 区别在于mkdirs()可以多级创建。

创建文件方法,createNewFile();

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI