Java没有在目录中获取文件

时间:2012-01-04 11:38:02

标签: java file directory

    System.out.println("READ");

    String currentWorldName = "RANDOM";
    String propertiesFileDirectory = propertiesFolder + currentWorldName + "/props.properties";
    String entitiesFolderDirectory = propertiesFolder + currentWorldName + "/Entities";

    try 
    {
        properties.load(new FileInputStream(propertiesFileDirectory));
    } 

    catch (FileNotFoundException e)
    {
        //Since it doesn't exist either it was deleted by the user or hasn't been created yet.
        createNewPropertiesFile();
    } 

    catch (IOException e) 
    {
        outputToLog("IOException when loading properties file for the world: '" + currentWorldName + "'.\n" + e.getStackTrace().toString());
    }

    //getting values from properties

    //Now to read each properties file in Entities
    File entitiesFolder = new File(entitiesFolderDirectory);

    try 
    {
        List<String> entitiesDirectoryContents = Arrays.asList(entitiesFolder.list());

        //Read each file in the entities directory and load it into memory.
        for (String entityPropertiesFileName : entitiesDirectoryContents)
        {
            if (propertiesBelongsToEntityCH(entityPropertiesFileName))
            {
                                 //Get properties one way
            }

            else //The properties file we're working does not belong to CH.
            {
                //Get properties from the same file a different way
            }
        }
               //This should never be hit since we have the file to read.
    catch (FileNotFoundException e) 
    {
        outputToLog("FileNotFoundException when loading entity properties file." + e.getMessage().toString());
    } 

    //I don't know when/if this would be hit. It hasn't happened.
    catch (IOException e) 
    {
        outputToLog("IOException when loading entity properties file." + e.getMessage().toString());
    }

    catch (NullPointerException e)
     {
          entitiesFolder.mkdirs();
     }

我发誓这已经工作了。它刚刚开始这样做。 Java一直声称“entitiesFolder”目录不存在(我查看了entitiesFolder.exists())。我有一个解决方案,当你看到的时候发生这种情况,因为当我的程序运行时它肯定会发生。好吧它仍然声称文件夹不存在,一遍又一遍。

我绝对肯定它是正确的目录,因为我将“entitiesFolderDirectory”打印到控制台。这是正确的。我也可以查看该文件夹中的文件,当mkdirs()运行时,只删除它们。

Java bug?这完全打破了我的计划。

2 个答案:

答案 0 :(得分:0)

我会在不抛出NullPointerException的情况下编写它。

File entitiesFolder = new File(entitiesFolderDirectory);
entitiesFolder.mkdirs();
for (String entityPropertiesFileName : entitiesFolder.list()) {
     //Do stuff
}

除非无法创建文件夹,否则这将始终有效。

答案 1 :(得分:0)

此代码有什么问题?我发现只有一个花括号丢失,否则它工作正常。

如果不存在,这将在c盘中创建一个dir测试,否则它将再次列出测试目录中的文件

String entitiesFolderDirectory = "C:\\test";
        File entitiesFolder = new File(entitiesFolderDirectory);

        try 
        {
            List<String> entitiesDirectoryContents = Arrays.asList(entitiesFolder.list());

            for (String entityPropertiesFileName : entitiesDirectoryContents)
            {
                System.out.println(entityPropertiesFileName);
            }

        }catch (NullPointerException e)
        {
            System.out.println("creating new folder");
            entitiesFolder.mkdirs();
        }