MCA
FILE I/O // ENTERPRISE CODE // ABSTRACT FACTORY // FILE I/O // ENTERPRISE CODE // ABSTRACT FACTORY //
BACK TO SYLLABUS
MEDIUM

FILE I/O

Reading and writing files, streams, and NIO

CONCEPTS

01Byte Streams (InputStream, OutputStream)
02Character Streams (Reader, Writer)
03Serialization
04BufferedReader and BufferedWriter
05Java NIO (Files, Paths)
06Working with directories

SYNTAX_DEMO

Data persistence
import java.io.*;
import java.nio.file.*;

public class Main {
    public static void main(String[] args) {
        Path path = Paths.get("data.txt");
        
        // Writing
        try {
            Files.writeString(path, "Hello, Java NIO!");
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        // Reading
        try {
            String content = Files.readString(path);
            System.out.println("Read: " + content);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}