List of utility methods to do Path File Read nio
BufferedReader | openBufferedReader(String pathOrUrl) open Buffered Reader InputStream inputStream; if (isRemote(pathOrUrl)) { URL url = new URL(pathOrUrl); inputStream = openConnection(url).getInputStream(); } else { inputStream = new FileInputStream(pathOrUrl); return new BufferedReader(new InputStreamReader(inputStream, Charset.defaultCharset())); ... |
ByteChannel | openForReadingAndWriting(String filePath) Opens a file for writing. Set<OpenOption> opts = new HashSet<>(); opts.add(StandardOpenOption.CREATE); opts.add(StandardOpenOption.WRITE); opts.add(StandardOpenOption.READ); Path path = Paths.get(filePath); return Files.newByteChannel(path, opts); |
Reader | openResourceReader(Class> resourceOrigin, String resourcePath) Creates a reader for reading the specified class loader resource. InputStream in = resourceOrigin.getResourceAsStream(resourcePath); if (in == null) { throw new FileNotFoundException("File not found: " + resourcePath); try { Reader reader = new InputStreamReader(in, CHARSET); in = null; return reader; ... |
String | read(String aPathString) read StringBuilder sb = new StringBuilder(); Path path = Paths.get(aPathString); try (BufferedReader reader = Files.newBufferedReader(path, Charset.defaultCharset())) { String line = reader.readLine(); while (line != null) { sb.append(line); sb.append(System.lineSeparator()); line = reader.readLine(); ... |
Object | read(String filePath) read Path path = Paths.get(filePath); try (ObjectInputStream input = new ObjectInputStream(Files.newInputStream(path))) { return input.readObject(); |
String | read(String filePath) read return read(new File(filePath)); |
String | read(String path) read String content = ""; List<String> lines = Files.readAllLines(Paths.get(path), StandardCharsets.UTF_8); for (String line : lines) { content += line + "\n"; return content; |
List | readAllLines(Path path) read All Lines return Files.readAllLines(path, StandardCharsets.UTF_8);
|
List | readAllLines(String path) Read file at path. List<String> lines = new ArrayList<String>(); CharsetEncoder encoder = Charset.forName(StandardCharsets.ISO_8859_1.toString()).newEncoder(); boolean ignoreBOM = true; BufferedReader br = null; String currentLine; int lineNumber = 1; try { br = new BufferedReader(new InputStreamReader(new FileInputStream(path), StandardCharsets.UTF_8)); ... |
List | readAllLinesOrExit(Path path) read All Lines Or Exit try { return Files.readAllLines(path, Charset.forName("UTF-8")); } catch (IOException e) { System.err.println("Failed to read [" + path + "]: " + e.getMessage()); System.exit(0); return null; |