创建的文件没有父级? [英] Created File Has No Parent?

查看:133
本文介绍了创建的文件没有父级?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 文件temp = new File(temp); 
temp.createNewFile();

然后出于某种原因,当我写

 文件pDir = temp.getParentFile(); 

,pDir为空。我实际上想写

 文件pDir = temp.getParentFile()。getParentFile(); 

但是会抛出一个空指针异常。

解决方案

您需要一个文件路径,尝试getAbsoluteFile。

 文件pDir = temp.getAbsoluteFile()。getParentFile(); 


In a java program, I create a file with

File temp = new File("temp");
temp.createNewFile();

Then for some reason when I write

File pDir = temp.getParentFile();

and pDir is null. I actually want to write

File pDir = temp.getParentFile().getParentFile();

but that throws a null pointer exception.

解决方案

You need a file with a path for that, try getAbsoluteFile.

File pDir = temp.getAbsoluteFile().getParentFile();

这篇关于创建的文件没有父级?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆