{"record":{"id":"0084debe4b2d65e0","repo":"openjdk/jdk","slug":"directory-new-folder-exists","errorCode":null,"errorMessage":"Directory 'New folder' exists","messagePattern":"Directory 'New folder' exists","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"warning","filePath":"src/demo/share/jfc/FileChooserDemo/ExampleFileSystemView.java","lineNumber":63,"sourceCode":"\n\n/**\n * This is a simple example that uses the FileSystemView class.\n * You can provide a superclass of the FileSystemView class with your own functionality.\n *\n * @author Pavel Porvatov\n */\npublic class ExampleFileSystemView extends FileSystemView {\n\n    /**\n     * Creates a new folder with the default name \"New folder\". This method is invoked\n     * when the user presses the \"New folder\" button.\n     */\n    public File createNewFolder(File containingDir) throws IOException {\n        File result = new File(containingDir, \"New folder\");\n\n        if (result.exists()) {\n            throw new IOException(\"Directory 'New folder' exists\");\n        }\n\n        if (!result.mkdir()) {\n            throw new IOException(\"Cannot create directory\");\n        }\n\n        return result;\n    }\n\n    /**\n     * Returns a list which appears in a drop-down list of the FileChooser component.\n     * In this implementation only the home directory is returned.\n     */\n    @Override\n    public File[] getRoots() {\n        return new File[] { getHomeDirectory() };\n    }\n","sourceCodeStart":45,"sourceCodeEnd":81,"githubUrl":"https://github.com/openjdk/jdk/blob/88dfb74bbeefcf2b0aa11835183bcd949998fc8f/src/demo/share/jfc/FileChooserDemo/ExampleFileSystemView.java#L45-L81","documentation":"IOException from ExampleFileSystemView.createNewFolder in the FileChooserDemo: the demo unconditionally names new folders 'New folder', so creating one fails if a file or directory with that name already exists in containingDir. It is a demo-simplification guard, not a general-purpose API.","triggerScenarios":"Clicking the 'New folder' button in the running FileChooserDemo twice, or opening a directory that already contains 'New folder'.","commonSituations":"Any real application copying this demo's FileSystemView implementation verbatim — real file managers need unique names ('New folder (2)', ...) which this demo deliberately omits.","solutions":["As a user of the demo: navigate to a directory without an existing 'New folder' or delete/rename the old one first.","As a developer copying this code: generate a unique name before calling mkdir (see example fix)."],"exampleFix":"// before\nFile result = new File(containingDir, \"New folder\");\n\n// after: pick a unique name\nFile result = new File(containingDir, \"New folder\");\nint i = 2;\nwhile (result.exists()) {\n    result = new File(containingDir, \"New folder (\" + (i++) + \")\");\n}","handlingStrategy":"validation","validationCode":"// check (and pick a unique name) before creating\nFile result = new File(containingDir, \"New folder\");\nint n = 2;\nwhile (result.exists()) result = new File(containingDir, \"New folder (\" + n++ + \")\");","typeGuard":null,"tryCatchPattern":"try {\n    return fsv.createNewFolder(dir);\n} catch (IOException e) {\n    // name clash: propose a suffixed name to the user and retry once\n    return new File(dir, \"New folder \" + System.currentTimeMillis());\n}","preventionTips":["Never hardcode a fixed folder name in a FileSystemView — always uniquify.","Do not ship demo FileSystemView implementations as production code."],"tags":["swing","filechooser","demo","filesystem"],"backgroundTag":null,"analyzedSha":"88dfb74bbeefcf2b0aa11835183bcd949998fc8f","analyzedAt":"2026-08-14T11:45:09.665Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}