Java Utililty Methods Path File Read nio

List of utility methods to do Path File Read nio

Description

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

Method

StringreadFileToSingleString(String path)
Get the content of a text file as a single string.
List<String> lns = new ArrayList<>();
try {
    lns = Files.readAllLines(new File(path).toPath());
} catch (IOException e) {
    e.printStackTrace();
StringBuilder sb = new StringBuilder();
for (String x : lns)
...
StringreadFileToString(@Nonnull Path file)
read File To String
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();
...
byte[]readFromFile(Path absolutePath)
read From File
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[8192];
int i;
try (FileInputStream fis = new FileInputStream(absolutePath.toString())) {
    while ((i = fis.read(buffer)) != -1) {
        baos.write(buffer, 0, i);
return baos.toByteArray();
StringreadFromFile(String path)
read From File
File file = new File(path);
if (!file.exists())
    return "";
if (file.isDirectory())
    throwRuntimeException("file expected, this is a directory: " + path);
try {
    FileInputStream fin = new FileInputStream(file);
    return readFromStream(fin);
...
StringreadFromFile(String path)
read From File
Preconditions.checkArgument(Files.exists(Paths.get(path)), "File '" + path + "' does not exist");
StringBuilder sb = new StringBuilder("");
try (BufferedReader reader = new BufferedReader(new FileReader(path))) {
    String line = reader.readLine();
    while (line != null) {
        line = line.trim();
        if (!"".equals(line)) {
            sb.append(line).append("\n");
...
byte[]readFully(Path filePath)
read Fully
return readFully(new FileInputStream(filePath.toFile()));
int[]readIntsFromFile(String filePath)
Reads a file which has one integer value on each line, and returns the values in an integer array.
try {
    Path path = Paths.get(filePath);
    List<Integer> values = new ArrayList<Integer>();
    BufferedReader reader = Files.newBufferedReader(path, Charset.defaultCharset());
    String line = reader.readLine();
    while (line != null) {
        Integer value = Integer.valueOf(line);
        values.add(value);
...
TreadJson(Class dataClass, Path path)
Reads the data from a JSON file at the given path
if (path == null) {
    return null;
ObjectMapper jsonMapper = new ObjectMapper();
return jsonMapper.readValue(path.toFile(), dataClass);
TreadJsonObject(Path path, TypeReference typeReference)
read Json Object
ObjectMapper mapper = new ObjectMapper();
return mapper.readValue(path.toFile(), typeReference);
MapreadKeyFile(String path)
read Key File
Map<String, Integer> result = new HashMap<String, Integer>();
List<String> lines = readLinesOfFile(path);
for (int i = 0; i < lines.size(); i++) {
    String line = lines.get(i);
    if (line.startsWith(A_KEY)) {
        result.put(A_KEY, Integer.valueOf(line.split("=")[1]));
    } else if (line.startsWith(K_KEY)) {
        result.put(K_KEY, Integer.valueOf(line.split("=")[1]));
...