@Test public void shouldSaveFileUsingAbsolutePath(){ String absolutePath = Paths.get("") + "target/filename.adoc"; File file = FileUtil.saveFile(absolutePath,"data"); assertThat(file).exists(); }
public static File saveFile(String name, String data) { if (name == null) { name = ""; } String fullyQualifiedName = name; if (!new File(fullyQualifiedName).isAbsolute()) { fullyQualifiedName = Paths.get("").toAbsolutePath().toString() + "/" + name; } try { if (fullyQualifiedName.contains("/")) { File f = new File(fullyQualifiedName.substring(0, fullyQualifiedName.lastIndexOf("/"))); f.mkdirs(); } File file = new File(fullyQualifiedName); file.createNewFile(); FileUtils.write(file, data, "UTF-8"); log.info("Wrote: " + file.getAbsolutePath()); return file; } catch (IOException e) { log.log(Level.SEVERE, "Could not create file " + name, e); return null; } }
FileUtil { public static File saveFile(String name, String data) { if (name == null) { name = ""; } String fullyQualifiedName = name; if (!new File(fullyQualifiedName).isAbsolute()) { fullyQualifiedName = Paths.get("").toAbsolutePath().toString() + "/" + name; } try { if (fullyQualifiedName.contains("/")) { File f = new File(fullyQualifiedName.substring(0, fullyQualifiedName.lastIndexOf("/"))); f.mkdirs(); } File file = new File(fullyQualifiedName); file.createNewFile(); FileUtils.write(file, data, "UTF-8"); log.info("Wrote: " + file.getAbsolutePath()); return file; } catch (IOException e) { log.log(Level.SEVERE, "Could not create file " + name, e); return null; } } }
FileUtil { public static File saveFile(String name, String data) { if (name == null) { name = ""; } String fullyQualifiedName = name; if (!new File(fullyQualifiedName).isAbsolute()) { fullyQualifiedName = Paths.get("").toAbsolutePath().toString() + "/" + name; } try { if (fullyQualifiedName.contains("/")) { File f = new File(fullyQualifiedName.substring(0, fullyQualifiedName.lastIndexOf("/"))); f.mkdirs(); } File file = new File(fullyQualifiedName); file.createNewFile(); FileUtils.write(file, data, "UTF-8"); log.info("Wrote: " + file.getAbsolutePath()); return file; } catch (IOException e) { log.log(Level.SEVERE, "Could not create file " + name, e); return null; } }  }
FileUtil { public static File saveFile(String name, String data) { if (name == null) { name = ""; } String fullyQualifiedName = name; if (!new File(fullyQualifiedName).isAbsolute()) { fullyQualifiedName = Paths.get("").toAbsolutePath().toString() + "/" + name; } try { if (fullyQualifiedName.contains("/")) { File f = new File(fullyQualifiedName.substring(0, fullyQualifiedName.lastIndexOf("/"))); f.mkdirs(); } File file = new File(fullyQualifiedName); file.createNewFile(); FileUtils.write(file, data, "UTF-8"); log.info("Wrote: " + file.getAbsolutePath()); return file; } catch (IOException e) { log.log(Level.SEVERE, "Could not create file " + name, e); return null; } }  static String findJsonFile(String path); static List<String> findJsonFiles(String startDir); static List<String> findFiles(String startDir, final String suffix); static List<String> findFiles(String startDir, final String suffix, final boolean singleResult); static List<String> findFiles(String startDir, final String suffix, final boolean singleResult, final String relativePath); static File saveFile(String name, String data); static File loadFile(String path); static boolean removeFile(String path); static File copyFileFromClassPath(String source, String dest); static File copyFile(String source, String dest); }
FileUtil { public static File saveFile(String name, String data) { if (name == null) { name = ""; } String fullyQualifiedName = name; if (!new File(fullyQualifiedName).isAbsolute()) { fullyQualifiedName = Paths.get("").toAbsolutePath().toString() + "/" + name; } try { if (fullyQualifiedName.contains("/")) { File f = new File(fullyQualifiedName.substring(0, fullyQualifiedName.lastIndexOf("/"))); f.mkdirs(); } File file = new File(fullyQualifiedName); file.createNewFile(); FileUtils.write(file, data, "UTF-8"); log.info("Wrote: " + file.getAbsolutePath()); return file; } catch (IOException e) { log.log(Level.SEVERE, "Could not create file " + name, e); return null; } }  static String findJsonFile(String path); static List<String> findJsonFiles(String startDir); static List<String> findFiles(String startDir, final String suffix); static List<String> findFiles(String startDir, final String suffix, final boolean singleResult); static List<String> findFiles(String startDir, final String suffix, final boolean singleResult, final String relativePath); static File saveFile(String name, String data); static File loadFile(String path); static boolean removeFile(String path); static File copyFileFromClassPath(String source, String dest); static File copyFile(String source, String dest); static final Pattern ADOC_FILE_EXTENSION; }