Java Streams   «Prev  Next»

Lesson 10

Writing Data Output Streams - Conclusion

This module discussed how to write data by using Java output streams. You learned how to
  1. copy a file to System.out with maximum efficiency,
  2. when to flush and close an output stream, and
  3. how to use file streams.

public class DataOutputStream
extends FilterOutputStream
implements DataOutput

A data output stream lets an application write primitive Java data types to an output stream in a portable way. An application can then use a data input stream to read the data back in.

Constructor Detail

DataOutputStream
public DataOutputStream(OutputStream out)

Creates a new data output stream to write data to the specified underlying output stream. The counter written is set to zero. Parameters: out - the underlying output stream, to be saved for later use.

Write a program using Java 11 to write data to a file

In this example, we'll create a Java program that writes data to a file using Java 11's Files.writeString() and Files.write() methods. The Files.writeString() method is used for writing text, while the Files.write() method is used for writing bytes.
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;

public class FileWriteJava11 {

    public static void main(String[] args) {
        Path filePath = Path.of("output.txt");

        try {
            writeFile(filePath);
            System.out.println("Data written to file successfully.");
        } catch (IOException e) {
            System.err.println("Failed to write data to the file: " + e.getMessage());
        }
    }

    public static void writeFile(Path file) throws IOException {
        // Write text to a file
        String text = "Hello, world!";
        Files.writeString(file, text, StandardCharsets.UTF_8, StandardOpenOption.CREATE);

        // Write bytes to a file
        byte[] data = "This is an example.".getBytes(StandardCharsets.UTF_8);
        Files.write(file, data, StandardOpenOption.APPEND);
    }
}


Code Explanation

In this example, the writeFile() method writes a string to the specified file using the Files.writeString() method and appends another string as bytes using the Files.write() method. The StandardOpenOption.CREATE option is used to create the file if it doesn't exist, and the StandardOpenOption.APPEND option is used to append data to the existing file.
To compile and run the program, use the following commands in the terminal:
# Compile the program
javac FileWriteJava11.java

# Run the program
java FileWriteJava11

After running the program, you should see a file named output.txt in the same directory, containing the written data.

In the next module you will learn about filter streams. Filter streams have two purposes.
  1. First, they allow you to read and write data at a higher level than working with the raw bytes. For example, you will learn to read a file that is full of double values and write another file of double values.
  2. Second, you will learn how to use a filter stream to modify data as it is read.