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

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

问题描述

在一个java程序中,我用

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();

并且 pDir 为空.其实我很想写

and pDir is null. I actually want to write

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

但这会引发空指针异常.

but that throws a null pointer exception.

推荐答案

你需要一个带有路径的文件,试试 getAbsoluteFile.

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

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

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

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