Skip to content

Instantly share code, notes, and snippets.

@indrayam
Last active June 19, 2019 23:15
Show Gist options
  • Save indrayam/ca8d39b931c4e14ab242ffb6c440ffda to your computer and use it in GitHub Desktop.
Save indrayam/ca8d39b931c4e14ab242ffb6c440ffda to your computer and use it in GitHub Desktop.
Simplest FileIO in Java (Part 1)
package dev.anandsharma.corejava.ch3;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.Scanner;
public class FileIOTest1 {
public static final String RW_FILE_PATH = "/Users/anasharm/Downloads/test1.txt";
public static void main(String[] args) throws IOException {
PrintWriter pw = new PrintWriter(RW_FILE_PATH, StandardCharsets.UTF_8);
pw.printf("%s%n", "Cisco Systems Inc.");
pw.printf("%s%n", "Research Triangle Park, Cary NC");
pw.printf("%s%n", "27519");
pw.close();
Scanner in = new Scanner(Path.of(RW_FILE_PATH), StandardCharsets.UTF_8);
while(in.hasNextLine()) {
System.out.printf("%s%n", in.nextLine());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment