Outfiles created even with exists check in java Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Outfiles created even with exists check in java without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I am currently creating a package in Java, which one of my classes containing a method that creates and writes to outfiles.

My goal is to have the method check to see if an outfile has already been created. If not, create it. If it has, do nothing. By creating the outfile, I want to simply add the sourceType variable I specify in my conditional as a prefix to the actual file name (not whole path).

My problem lies with the last sentence, because despite my checks, the file still gets created. If I run it once, I get “/path/to/FL_myFile” from “/path/to/myFile”. If I run it twice, it creates another called “/path/to/FL_FL_myFile”, which is wrong.

I’ve been programming non stop for a while and my brain is pretty tired so please forgive me if this is a stupid error…

Thank you very much!

Here is my code:

public static void RenameFiles(File subDir) {
    File[] files = subDir.listFiles();
    Path source = FileSystems.getDefault().getPath(subDir.toString());
    String sourceType = null;
    if (source.toString().endsWith("Flavonoid")) {
        sourceType = "/FL_";
    } else {
        sourceType = "/SR_";
    }
    for (File file : files) {
        Path oldFilePath = FileSystems.getDefault().getPath(file.toString());
        Path newFilePath = FileSystems.getDefault().getPath(subDir.toString() + sourceType + file.getName());
        File newFile = newFilePath.toFile();
        if(newFile.exists()) {
            System.out.println("Exists!!! Do nothing! : " + newFile.toString());
            continue;
        } else {
            System.out.println("Does not exist!! Make it!! " + newFile.toString());
            try {
                Files.move(oldFilePath, newFilePath);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

Thanks to Paul Ostrowski for the answer. As expected, it was obvious yet I wasn’t seeing it. Added a simple fix below.

public static void RenameFiles(File subDir) {
    File[] files = subDir.listFiles();
    Path source = FileSystems.getDefault().getPath(subDir.toString());
    String sourceType = null;
    if (source.toString().endsWith("Flavonoid")) {
        sourceType = "/FL_";
    } else {
        sourceType = "/SR_";
    }
    for (File file : files) {
        Path oldFilePath = FileSystems.getDefault().getPath(file.toString());
        Path newFilePath = FileSystems.getDefault().getPath(subDir.toString() + sourceType + file.getName());
        File newFile = newFilePath.toFile();
        if(file.getName().startsWith("SR_") || file.getName().startsWith("FL_")) {
            continue;
        } else {
            System.out.println("Creating outfile: " + newFile.toString());
            try {
                Files.move(oldFilePath, newFilePath);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

Answer

You dont have any check to see if the file starts with “FL_” or “SR_”. The first time you run, foo.bar gets renamed to FL_foo.bar. The second time you run, foo.bar doesn’t get renamed, but the already existing FL_foo.bar gets renamed to FL_FL_foo.bar. You need to see if the file name starts with “FL_” or “SR_” as appropriate.

We are here to answer your question about Outfiles created even with exists check in java - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji