Java Utililty Methods Path File Content

List of utility methods to do Path File Content

Description

The list of methods to do Path File Content are organized into topic(s).

Method

voidwriteFile(Path path, String contents)
write File
try (PrintWriter pw = new PrintWriter(new FileWriter(path.toString(), Boolean.FALSE))) {
    pw.write(contents);
} catch (IOException ex) {
    ex.printStackTrace(System.err);
voidwriteFile(String path, String content)
write File
FileOutputStream stream = new FileOutputStream(new File(path));
try {
    FileChannel fc = stream.getChannel();
    stream.write(content.getBytes());
} finally {
    stream.close();
voidwriteFile(String path, String content)
write File
try {
    Files.write(Paths.get(path), content.getBytes());
} catch (IOException e) {
    throwRuntime(e);
voidwriteFile(String path, String contents)
write File
Path p = FileSystems.getDefault().getPath(path);
writeFile(p, contents);
voidwriteFileContents(Path to, byte[] contents)
write File Contents
Files.write(to, contents);
voidwriteJsonToFile(Path jsonPath, String jsonContent)
write Json To File
BufferedWriter jsonWriter = null;
try {
    jsonWriter = Files.newBufferedWriter(jsonPath, Charset.forName("utf-8"));
    jsonWriter.write(jsonContent, 0, jsonContent.length());
    jsonWriter.flush();
} catch (IOException e) {
    e.printStackTrace(); 
} finally {
...
PathwriteLines(Path base, String file, String[] content)
write Lines
Path out = base.resolve(file);
try (PrintWriter writer = new PrintWriter(Files.newBufferedWriter(out, CHARSET))) {
    for (String line : content) {
        writer.println(line);
} catch (IOException e) {
    e.printStackTrace();
return out;
PathwriteString(String content, Path path)
write String
try {
    Files.write(path, content.getBytes());
    return path;
} catch (IOException e) {
    throw new RuntimeException(e);
PathwriteStringToFile(String content, Path targetPath)
write String To File
return Files.write(targetPath, content.getBytes(), StandardOpenOption.CREATE_NEW);
voidwriteToBinaryFile(String filePath, byte[] binaryContent)
write To Binary File
Path path = Paths.get(filePath);
Files.write(path, binaryContent);