List of utility methods to do Path File Read nio
String | readLine(BufferedReader br, Path path) read Line try { return br.readLine(); } catch (IOException e) { throw new IOException("Can't read from file: " + path, e); |
String | readLineByLine(String filePath) read Line By Line StringBuilder contentBuilder = new StringBuilder(); try (Stream<String> stream = Files.lines(Paths.get(filePath), StandardCharsets.UTF_8)) { stream.forEach(s -> contentBuilder.append(s).append("\n")); } catch (IOException e) { e.printStackTrace(); return contentBuilder.toString(); |
List | readLines(Path path, boolean ignoreComments) read Lines File file = path.toFile(); if (!file.exists() || !file.isFile()) { return new ArrayList<>(); List<String> lines = new ArrayList<>(); try (BufferedReader reader = new BufferedReader(new FileReader(file))) { String line; while ((line = reader.readLine()) != null) { ... |
String | readLineS(String filePath, int line) Read the context of a specified line in a text file. File f = new File(filePath); List<String> lines = Files.readAllLines(f.toPath()); return lines.get(line - 1); |
List | readLinesFromFile(Path path) read Lines From File try { return Files.readAllLines(path, StandardCharsets.UTF_8); } catch (IOException e) { throw new UnsupportedOperationException("Could not read from file '" + path + "'", e); |
Stream | readLinesFromFileLazy(String path) read Lines From File Lazy return Files.lines(Paths.get(path), Charset.defaultCharset());
|
List | readLinesOfFile(String path) read Lines Of File Path pathToFile = Paths.get(path);
return Files.readAllLines(pathToFile, StandardCharsets.US_ASCII);
|
String | readLineSP(String filePath, int line, int pos) Read the context after a specified position of a line in a text file. pos -= 1; String l = readLineS(filePath, line); if (l.length() < pos) pos = l.length(); l = l.substring(pos, l.length()); return l; |
String | readLinesToString(Path file) read Lines To String final StringBuilder sb = new StringBuilder(); try (BufferedReader reader = new BufferedReader(new FileReader(file.toFile()), BUF_SIZE)) { reader.lines().forEach(line -> { sb.append(line); sb.append('\n'); }); return sb.toString(); ... |
Map | readManifest(Path path) read Manifest try (InputStream fileInput = Files.newInputStream(path); InputStream input = new BufferedInputStream(fileInput)) { Properties properties = new Properties(); properties.load(input); Map<String, String> result = new HashMap<>(2 * properties.size()); for (Map.Entry<?, ?> entry : properties.entrySet()) { result.put(entry.getKey().toString(), entry.getValue().toString()); return result; |