FileUtil { @SuppressWarnings("resource") public static FileChannel openChannel(final String filename, final boolean create) { FileChannel fileChannel = null; try { final File file = new File(filename); if (!file.exists()) { if (create) { file.getParentFile().mkdirs(); file.createNewFile(); } else { return null; } } final RandomAccessFile raf = new RandomAccessFile(file, "rw"); fileChannel = raf.getChannel(); } catch (final Exception e) { LangUtil.rethrowUnchecked(e); } return fileChannel; } private  FileUtil(); static void deleteFolder(final String path); static void ensureDirectoryExists(final Path directory); static void deleteFolder(final Path directory); @SuppressWarnings("resource") static FileChannel openChannel(final String filename, final boolean create); static void replace(final Path src, final Path dest); static void deleteFile(final File file); static void copySnapshot(final Path runtimeDirectory, final Path snapshotDirectory); static final Logger LOG; }
@Test public void shouldCreateParentDirectory() { final String folderToCreate = "folder"; final String fileToCreate = "file"; final String filePathToCreate = tempFolder.getRoot().getPath() + File.separator + folderToCreate + File.separator + fileToCreate; FileUtil.openChannel(filePathToCreate, true); final File[] matchingFolders = tempFolder.getRoot().listFiles((dir, name) -> folderToCreate.equals(name)); assertThat(matchingFolders).hasSize(1); final File folder = matchingFolders[0]; final File[] filesInFolder = folder.listFiles(); assertThat(filesInFolder).hasSize(1); assertThat(filesInFolder[0]).hasName(fileToCreate); }