FileUtils { public static PrintWriter createFileWriter(String parent, String name) { try { final File dir = new File(parent); if (dir.exists() || dir.mkdirs()) { final File file = new File(name); file.createNewFile(); final PrintWriter writer = new PrintWriter(new OutputStreamWriter( new FileOutputStream(file, true))); return writer; } else { throw new RuntimeException("fail to create parent directory " + parent); } } catch (IOException e) { throw new RuntimeException("No such file or directory " + name, e); } } private  FileUtils(); static List<String> readLines(InputStream input); static PrintWriter createFileWriter(String parent, String name);  }
@Test public void testFileCreateWhenParentDoesNotExist() { writer = FileUtils.createFileWriter(PARENT, PATH); assertThat(writer).isNotNull(); assertThat(dir).exists(); assertThat(file).exists(); }
@Test public void testFileCreateWhenParentExists() { dir.mkdirs(); assertThat(dir).exists(); writer = FileUtils.createFileWriter(PARENT, PATH); assertThat(writer).isNotNull(); assertThat(file).exists(); }