Writing Files with java.nio.file in Java


Use Files.writeString() for an efficient and concise way to write content to files.

Source Code

import java.nio.file.*;
import java.io.IOException;

public class WriteFileNio {
    public static void main(String[] args) {
        Path filePath = Paths.get("output.txt");
        String content = "Hello, NIO!";
        try {
            Files.writeString(filePath, content, StandardOpenOption.CREATE);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments