List of utility methods to do Path File Read nio
Properties | readPropertiesFile(Path path) read Properties File Properties properties = new Properties(); try (InputStream is = Files.newInputStream(path)) { properties.load(is); return properties; |
String | readSheBang(Path script) read She Bang try (BufferedReader reader = new BufferedReader( new InputStreamReader(Files.newInputStream(script), "UTF-8"))) { String line = reader.readLine(); while (line != null) { line = line.trim(); if (line.length() > 0) { if (line.startsWith("#!")) { return line; ... |
String | readStringFromPath(Path path) read String From Path FileReader fr = new FileReader(path.toString()); BufferedReader br = new BufferedReader(fr); String content; try { StringBuilder sb = new StringBuilder(); String line = br.readLine(); while (line != null) { sb.append(line); ... |
String | readTextFile(Path p) read Text File try (InputStream is = Files.newInputStream(p)) { try (ByteArrayOutputStream os = new ByteArrayOutputStream()) { copy(is, os); return new String(os.toByteArray(), StandardCharsets.UTF_8); |
String | readTextFileFromResources(String filepath, ClassLoader classLoader) read Text File From Resources InputStream is = null; BufferedReader reader = null; try { is = classLoader.getResourceAsStream(filepath); if (is == null) { throw new FileNotFoundException("file not found:" + filepath); reader = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8"))); ... |
String[] | readTextFromFile(String textFilePath) read Text From File String[] linesArr = null; Path filePath = Paths.get(textFilePath); Charset charset = Charset.forName("ISO-8859-1"); try { List<String> lines = Files.readAllLines(filePath, charset); linesArr = new String[lines.size()]; for (int i = 0; i < lines.size(); i++) { linesArr[i] = lines.get(i); ... |
void | setReadable(final Path path, boolean readable) set Readable if (isWindows()) { final UserPrincipal currentUser = path.getFileSystem().getUserPrincipalLookupService() .lookupPrincipalByName(System.getProperty("user.name")); final AclFileAttributeView view = Files.getFileAttributeView(path, AclFileAttributeView.class); final AclEntryType entryType = readable ? AclEntryType.ALLOW : AclEntryType.DENY; final AclEntry entry = AclEntry.newBuilder().setType(entryType).setPrincipal(currentUser) .setPermissions(AclEntryPermission.READ_DATA, AclEntryPermission.READ_ATTRIBUTES).build(); final List<AclEntry> acl = view.getAcl(); ... |
boolean | setReadOnly(Path file) set Read Only try { Files.getFileAttributeView(file, PosixFileAttributeView.class).setPermissions(READ_ONLY_PERM); } catch (Exception ignore) { try { Files.getFileAttributeView(file, DosFileAttributeView.class).setReadOnly(true); } catch (Exception ignored) { return false; return true; |