简体   繁体   English

Java没有在目录中获取文件

[英]Java isn't getting files in a 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();
     }

This HAS been working, I swear. 我发誓,这一直在努力。 It just started doing this. 它只是开始这样做。 Java keeps claiming that the "entitiesFolder" directory doesn't exist (I check with entitiesFolder.exists()). Java一直声称“ entitiesFolder”目录不存在(我检查entityFolder.exists())。 I have a solution for when that happens as you can see, because while my program is running it definitely can happen. 如您所见,我为何时发生提供了解决方案,因为在我的程序运行时,肯定可以实现。 Well it still claims that the folder doesn't exist, over and over. 好吧,它仍然声称该文件夹不存在,一遍又一遍。

I'm absolutely positive that it's the right directory because I print the "entitiesFolderDirectory" out to the console. 我绝对肯定这是正确的目录,因为我将“ entitiesFolderDirectory”打印到控制台。 It's correct. 这是正确的。 I can also be looking at the files inside of that folder and when mkdirs() runs it just deletes them all. 我也可以查看该文件夹中的文件,当mkdirs()运行时,它只会删除所有文件。

Java bug? Java错误? This has completely broken my program. 这完全破坏了我的程序。

I would write it without throwing a NullPointerException. 我会写它而不抛出NullPointerException。

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

This will always work unless the folder could not be created. 除非无法创建文件夹,否则它将始终有效。

What is the problem with this code? 此代码有什么问题? only one curly braces i found is missing else it is working fine. 我发现只有一个大括号不见了,否则工作正常。

This will create a dir test in c drive if does not exist else it will list the files in test dir over n over again 如果不存在,这将在c驱动器中创建目录测试,否则它将在n中再次列出测试目录中的文件

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM