为了账号安全,请及时绑定邮箱和手机立即绑定

为何createNewFile无法创建文件?

public static void main(String[] args) {
		File file=new File("resources");
		if(!file.exists()){
			file.mkdir();
		System.out.println("ok");}
		System.out.println(file.getAbsolutePath());
		File file2=new File(file,"one.dat");
		if(!file2.exists()){
			try{
			file.createNewFile();
			System.out.println("file2 created");
			}
			catch (IOException e){
				System.out.println("error");
			}
			System.out.println(file2.getAbsolutePath());}
		System.out.println(file2.exists());
	}
	
	经查硬盘,创建了resources文件夹,但并没有创建one.dat,并且输出结果是:
	E:\javaProject\demo\resources
        file2 created
        E:\javaProject\demo\resources\one.dat
        false
        
        java的getAbsolutePath可以获取one.dat的路径,但是exists却判断one.dat不存在(确实也不存在)
        
        请问问题出在哪里,我该如何解决?


正在回答

3 回答

file.createNewFile();这里你要写file2.createNewFile()才对啊

0 回复 有任何疑惑可以回复我~
#1

好鬼过瘾 提问者

非常感谢!
2015-09-28 回复 有任何疑惑可以回复我~

to青春如風鈴應該是輸入法不同 這是慕課網厲害的地方,能兼容繁、簡體^^

ejcfruqzsloooyxxhbbrgyfkexgcba

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

为何createNewFile无法创建文件?

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信