如何通过在工作目录中设置文件名来获取实例化文件的 File.getParent()?

How to obtain the File.getParent() for the File that was instantiated by setting just filename in the working directory?

如果我实例化了一个完整路径的文件,如

    File file = null;
    try {
        file = new File(new URI("file:/C:/java2018/infa2019_2sm/test.txt"));
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }

以后可以打电话

   File directory = file.getParentFile(); 

并且会分配到C:/java2018/infa2019_2sm目录下

但是如果我们只使用工作目录中现有文件的文件名来实例化文件,就像这样

   File file=new File("test.txt");

除了 getParentFile()、getParentFile() 之外,文件的每个 getter 都有效 returns null。

那么,通过使用相对于工作目录的完整路径和短路径来实例化 File 对象的保证目录的合法方法是什么? java.io 是否可行,或者我仍然需要使用 java.nio?

Windows 10,如果重要的话,当然我希望它能跨平台。

在@Tom 的回答的帮助下,工作方式看起来像

   File directory=new File(new File(file.getAbsolutePath()).getParent());