Java Path File Read nio readFileToString(@Nonnull Path file)

Here you can find the source of readFileToString(@Nonnull Path file)

Description

read File To String

License

Mozilla Public License

Return

The contents of the file, with linebreaks included

Declaration

@Nonnull
public static String readFileToString(@Nonnull Path file) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Mozilla Public License 

import javax.annotation.Nonnull;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Path;

public class Main {
    /**/*www.j  a  va2  s .co m*/
     * @return The contents of the file, with linebreaks included
     */
    @Nonnull
    public static String readFileToString(@Nonnull Path file) throws IOException {
        StringBuilder sb = new StringBuilder();
        try (FileReader fr = new FileReader(file.toFile())) {
            int chr;
            while ((chr = fr.read()) != -1) {
                sb.append((char) chr);
            }
        }
        return sb.toString();
    }
}

Related

  1. readFileByLines(String filePath)
  2. readFileIntoString(Path path)
  3. readFileLines(Path file)
  4. readFileRows(Path path)
  5. readFileToSingleString(String path)
  6. readFromFile(Path absolutePath)
  7. readFromFile(String path)
  8. readFromFile(String path)
  9. readFully(Path filePath)