#createNewFile() conditional always being false

18 messages · Page 1 of 1 (latest)

unborn plover
#

The code should check if there is a file created named ciclos.txt and then, if there isnt, create the file. If there is one then should say that its already created. I tried multiple options but no matter what always getting the same result, with or without file created.
Any ideas whats happening? (Something silly probably)

versed socketBOT
#

This post has been reserved for your question.

Hey @unborn plover! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

jolly yacht
#

You forgot to do a thing

#

"if file does not exist, create it"

#

Also don't name variables with Pascal_Case

#

that's not what you do in Java

#

variables are either CAPS_SNAKE_CASE if static final or standardCamelCase if other

faint widget
#

Why are you using colons( :)?

jolly yacht
#

Also, I suggest using Path class as it allows better control

#

Path filePath = Paths.get("location", "location2", "file");

#

and then you can either do File pathFile = filePath.toFile() and do the Boolean checks, or just use raw calls like

if (!filePath.toFile().exists()) {
}```
#

Usually you do the exists call before making new file as you don't want to create a new file every time, no matter the case of whether or not the file exists or not

#

you want to only make the file when it doesn't exist

#

Here's an example of mine that I use in prod:

Path getConfigFilePath() throws IOException {
    var configFilePath = Paths.get(String.valueOf(LauncherUtils.WORKING_DIR_PATH),
        MessageFormat.format("{0}_{1}.properties",
            "twentyten", System.getProperty("user.name")));
    var configFile = configFilePath.toFile();
    if (!configFile.exists() && !configFile.createNewFile()) {
      throw new IOException();
    }
    return configFilePath;
  }
#

I should probably change the double condition to return null instead with a warning

unborn plover
#

Thanks, I managed to figure out with your answers :)

versed socketBOT