How do I use the DosFileAttributes class?

This example show you how to use the DosFileAttributes class to get file attribute that support DOS file system. This class extends the BasicFileAttributes class. Using the DosFileAttributes class we can read file attributes using isArchive(), isHidden(), isReadOnly() and isSystem() methods.

Let’s see the code snippet below:

package org.kodejava.io;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.DosFileAttributes;

public class DosFileAttributeExample {
    public static void main(String[] args) throws Exception {
        String path = "D:/resources/data.txt";

        Path file = Paths.get(path);
        DosFileAttributes attr = Files.readAttributes(file, DosFileAttributes.class);

        System.out.println("isArchive()  = " + attr.isArchive());
        System.out.println("isHidden()   = " + attr.isHidden());
        System.out.println("isReadOnly() = " + attr.isReadOnly());
        System.out.println("isSystem()   = " + attr.isSystem());
    }
}

The output of the code snippet:

isArchive()  = true
isHidden()   = false
isReadOnly() = false
isSystem()   = false

How do I write a text file in JDK 7?

In JDK 7 we can write all lines from a List of String into a file using the Files.write() method. We need to provide the Path of the file we want to write to, the List of strings and the charsets. Each line is a char sequence and is written to the file in sequence with each line terminated by the platform’s line separator.

Let’s see the code snippet below:

package org.kodejava.io;

import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

public class WriteTextFile {
    public static void main(String[] args) {
        Path file = Paths.get("D:/resources/data.txt");

        List<String> lines = new ArrayList<>();
        lines.add("Lorem Ipsum is simply dummy text of the printing ");
        lines.add("and typesetting industry. Lorem Ipsum has been the ");
        lines.add("industry's standard dummy text ever since the 1500s, ");
        lines.add("when an unknown printer took a galley of type and ");
        lines.add("scrambled it to make a type specimen book.");

        try {
            // Write lines of text to a file.
            Files.write(file, lines, StandardCharsets.UTF_8);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

This code snippet will create a file called data.txt under the resources folder. Please make sure that this folder is existed before you tried to run the code.

How do I read all lines from a file?

The java.nio.file.Files.readAllLines() method read all lines from a file. This method ensures that the file is closed when all bytes have been read or an I/O error, or other runtime exception, is thrown. Bytes from the file are decoded into characters using the specified charset.

Note that this method is intended for simple cases where it is convenient to read all lines in a single operation. It is not intended for reading in large files. This method is available since Java 7.

package org.kodejava.io;

import java.net.URI;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.Objects;

public class ReadFileAsListDemo {
    public static void main(String[] args) {
        ReadFileAsListDemo demo = new ReadFileAsListDemo();
        demo.readFileAsList();
    }

    private void readFileAsList() {
        String fileName = "/data.txt";

        try {
            URI uri = Objects.requireNonNull(this.getClass().getResource(fileName)).toURI();
            List<String> lines = Files.readAllLines(Paths.get(uri),
                    Charset.defaultCharset());

            for (String line : lines) {
                System.out.println(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}