Java Utililty Methods Path File Write nio

List of utility methods to do Path File Write nio

Description

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

Method

booleanrobustCheckWriteable(Path logFile)
robust Check Writeable
try {
    return Files.isWritable(logFile.getParent()) && (!Files.exists(logFile) || Files.isWritable(logFile));
} catch (Throwable ex) {
    logGetLogPathError(ex.getMessage());
    return false;
voidsave(Object o, String path)
save
ObjectOutputStream stream = createObjectOutputStreamTo(path);
try {
    stream.writeObject(o);
    stream.close();
} catch (IOException e) {
    throw new RuntimeException(e);
voidsaveFile(String path, String text)
Writes a text file (as UTF-8).
byte[] encoded = text.getBytes(StandardCharsets.UTF_8);
Files.write(Paths.get(path), encoded);
voidsaveHeaders(Path path, ArrayList headers)
save Headers
try (BufferedWriter writer = Files.newBufferedWriter(path, StandardCharsets.UTF_8)) {
    for (String line : headers) {
        writer.write(line);
        writer.newLine();
} catch (IOException e) {
    e.printStackTrace();
voidsaveProperties(Path filePath, Properties prop)
save Properties
if (Files.exists(filePath))
    Files.delete(filePath);
try (Writer w = Files.newBufferedWriter(filePath)) {
    prop.store(w, "");
voidsaveTo(String path, InputStream in)
Saves content read from input stream into a file.
if (in == null)
    throw new IllegalArgumentException("input stream cannot be null");
if (path == null)
    throw new IllegalArgumentException("path cannot be null");
try (OutputStream out = new BufferedOutputStream(
        Files.newOutputStream(Paths.get(path), StandardOpenOption.CREATE, StandardOpenOption.APPEND))) {
    byte[] bytes = new byte[1024];
    for (int x = in.read(bytes); x != -1; x = in.read(bytes))
...
voidsaveToFile(String path, byte[] text)
save To File
try {
    Files.write(Paths.get(path), text);
} catch (IOException e) {
    e.printStackTrace();
voidsetPosixPermissions(String path, boolean ownerRead, boolean ownerWrite, boolean ownerExecute, boolean groupRead, boolean groupWrite, boolean groupExecute, boolean othersRead, boolean othersWrite, boolean othersExecute)
set Posix Permissions
Class<?> fileSystemsClass = Class.forName("java.nio.file.FileSystems");
Class<?> fileSystemClass = Class.forName("java.nio.file.FileSystem");
Class<?> pathClass = Class.forName("java.nio.file.Path");
Class<?> posixFileAttributeViewClass = Class.forName("java.nio.file.attribute.PosixFileAttributeView");
Class<?> filesClass = Class.forName("java.nio.file.Files");
Class<?> linkOptionClass = Class.forName("java.nio.file.LinkOption");
Class<?> posixFilePermissionClass = Class.forName("java.nio.file.attribute.PosixFilePermission");
Method fileSystemsGetDefaultMethod = fileSystemsClass.getMethod("getDefault");
...
PathsrcImageToSavePath(String src, Path saveFolder)
src Image To Save Path
int indexName = src.lastIndexOf("/");
if (indexName == src.length()) {
    src = src.substring(1, indexName + 1);
indexName = src.lastIndexOf("/") + 1;
String name = src.substring(indexName, src.length());
return saveFolder.resolve(name);
voidwrite(List list, String outputPath)
Write the list specified to the output path specified
try {
    Files.write(Paths.get(outputPath), list, StandardCharsets.UTF_8);
} catch (IOException e) {
    throw new UncheckedIOException(e);