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

voidappendToLog(String content, Path newFile)
append To Log
try (BufferedWriter writer = Files.newBufferedWriter(newFile, Charset.defaultCharset(),
        new OpenOption[] { StandardOpenOption.APPEND })) {
    writer.append(content);
    writer.append("\n");
    writer.flush();
} catch (IOException exception) {
    System.out.println("Error writing to file");
voidassertSameContent(String expectedFilePath, String actualFilePath)
assert Same Content
assertSameContent(new File(expectedFilePath), new File(actualFilePath));
booleancontentEquals(Path leftPath, Path rightPath)
content Equals
boolean leftExists = Files.exists(leftPath);
boolean rightExists = Files.exists(rightPath);
if (!leftExists && !rightExists) {
    return true;
if (leftExists != rightExists) {
    return false;
if (Files.isSameFile(leftPath, rightPath)) {
    return true;
try (BufferedReader leftStream = Files.newBufferedReader(leftPath);
        BufferedReader rightStream = Files.newBufferedReader(rightPath)) {
    for (;;) {
        String leftLine = leftStream.readLine();
        String rightLine = rightStream.readLine();
        if (leftLine == null && rightLine == null) {
            return true;
        if ((leftLine == null) != (rightLine == null)) {
            return false;
        assert leftLine != null && rightLine != null;
        if (!leftLine.equals(rightLine)) {
            return false;
StringfileContent(final Path p)
Read a file content, or wait the file to be created.
if (p == null) {
    throw new IllegalArgumentException();
if (Files.exists(p)) {
    return readAndDelete(p);
final Path parent = p.getParent();
if (Files.notExists(parent)) {
...
StringfileContent(final String filePath)
file Content
return Resources.toString(Resources.getResource(filePath), Charset.defaultCharset());
StringgetContent(Path path)
get Content
String textReturned = null;
textReturned = java.nio.file.Files.lines(path).collect(Collectors.joining());
return textReturned;
StringgetFileContents(final String absFilePath)
get File Contents
String data = null;
if (absFilePath != null) {
    File file = new File(absFilePath);
    if (file.isFile()) {
        byte[] buffer = new byte[4096];
        BufferedInputStream bis = null;
        try {
            int readSize = 0;
...
StringgetFileContents(Path file)
get File Contents
try (BufferedReader in = newBufferedReader(file, Charset.defaultCharset())) {
    StringBuffer sb = new StringBuffer();
    String line;
    while ((line = in.readLine()) != null) {
        sb.append(line);
    return sb.toString();
StringgetFileContents(String path)
get File Contents
String returnString = null;
File file = new File(path);
if (file.exists() && file.isFile()) {
    BufferedReader fileBufferedReader = null;
    try {
        fileBufferedReader = Files.newBufferedReader((FileSystems.getDefault().getPath(path)),
                Charset.forName("utf-8"));
        StringBuffer sb = new StringBuffer();
...
ListlistUris(Path content)
Lists URIs relative to the given Path .
List<Path> paths = listFiles(content);
List<String> result = new ArrayList<>();
for (Path path : paths) {
    String uri = toUri(path, content);
    result.add(uri);
return result;