// FileNIO.java - Demonstrate simple file I/O using the nio api

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;

public class FileNIO {
	
	static final String FILE = "data.txt";
	static final String LS = System.getProperty("line.separator");
	
	FileNIO() {
        write();
        read();
	} // end FileNIO()
    
    public static void main(String args[]) {
        System.out.print("\nFileNIO...  ");
        new FileNIO();
        System.out.println("==========");
    } // end main()
    
    void write() {
    	System.out.print("WRITE...  ");
    	Charset charset = Charset.forName("US-ASCII");
    	String s = "one\ntwo\nthree\n";
    	s = "One" + LS + "Two" + LS + "Three" + LS;
    	Path file = FileSystems.getDefault().getPath(FILE);
    	try (BufferedWriter writer = Files.newBufferedWriter(file, charset)) {
    	    writer.write(s, 0, s.length());
    	    writer.close();
    	} catch (IOException ex) {
    	    System.err.format("IOException: %s%n", ex);
    	}
    } // end write()
    
    void read() {
    	System.out.println("READ...  ");
    	Charset charset = Charset.forName("US-ASCII");
    	Path file = FileSystems.getDefault().getPath(FILE);
    	try (BufferedReader reader = Files.newBufferedReader(file, charset)) {
    	    String line = null;
    	    int n = 0;
    	    while ((line = reader.readLine()) != null) {
    	        System.out.printf("%d <%s>\n", ++n, line);
    	    }
    	    reader.close();
    	} catch (IOException ex) {
    	    System.err.format("IOException: %s%n", ex);
    	}
    } // end read()
    
}

/* =====

Reading a File by Using Buffered Stream I/O

The newBufferedReader(Path, Charset) method opens a file for reading, returning a BufferedReader 
that can be used to read text from a file in an efficient manner.

The following code snippet shows how to use the newBufferedReader method to read from a file. The file is encoded in "US-ASCII."

Charset charset = Charset.forName("US-ASCII");
try (BufferedReader reader = Files.newBufferedReader(file, charset)) {
    String line = null;
    while ((line = reader.readLine()) != null) {
        System.out.println(line);
    }
} catch (IOException x) {
    System.err.format("IOException: %s%n", x);
}

Writing a File by Using Buffered Stream I/O

You can use the newBufferedWriter(Path, Charset, OpenOption...) method to write to a file using a BufferedWriter.
The following code snippet shows how to create a file encoded in "US-ASCII" using this method:

Charset charset = Charset.forName("US-ASCII");
String s = ...;
try (BufferedWriter writer = Files.newBufferedWriter(file, charset)) {
    writer.write(s, 0, s.length());
} catch (IOException x) {
    System.err.format("IOException: %s%n", x);
}
===== */
