/** * fetches content of the given file * @param file * @return content string * @throws IOException */ public String getFileContent(File file) throws IOException { assertInDirectory(file); return FileUtils.readFileToString(file, "UTF-8"); }
/** * creates a directory * @param directoryToCreate */ public void mkdir(File directoryToCreate) { assertInDirectory(directoryToCreate); directoryToCreate.mkdirs(); }
/** * writes content to file * @param file * @param content * @throws IOException */ public void writeToFile(File file, String content) throws IOException { assertInDirectory(file); FileUtils.writeStringToFile(file, content, "UTF-8"); }
/** * creates symbolic link * @param destination * @param target * @throws IOException */ public void createSymbolicLink(File destination, File target) throws IOException { assertInDirectory(destination); assertInDirectory(target); Files.createSymbolicLink(destination.toPath(), target.toPath()); }
/** * deletes a file only if it is inside Phoenicis root * @param fileToDelete fileOrDirectoryToDelete */ public void remove(File fileToDelete) throws IOException { assertInDirectory(fileToDelete); if (fileToDelete.isDirectory()) { FileUtils.deleteDirectory(fileToDelete); } else { Files.deleteIfExists(fileToDelete.toPath()); } }
/** * lists files and directories in given directory (non-recursive) * @param directory * @return list of files and directories */ public String[] ls(File directory) { assertInDirectory(directory); File[] files = directory.listFiles(); return Arrays.stream(files) .map(file -> file.getName()) .toArray(String[]::new); }
/** * computes file size of the given file * @param file * @return file size * @throws IOException */ public long getSize(File file) throws IOException { assertInDirectory(file); Path folder = Paths.get(file.getAbsolutePath()); return Files.walk(folder).filter(p -> p.toFile().isFile()).mapToLong(p -> p.toFile().length()).sum(); }
/** * copies file * @param source * @param target * @throws IOException */ public void copy(File source, File target) throws IOException { assertInDirectory(source); assertInDirectory(target); if (source.isDirectory()) { FileUtils.copyDirectory(source, target); } else { if (target.isDirectory()) { FileUtils.copyFile(source, new File(target, source.getName())); } else { FileUtils.copyFile(source, target); } } }