Skip to content

Instantly share code, notes, and snippets.

@indrayam
Created June 19, 2019 23:15
Show Gist options
  • Save indrayam/af21f0746cfe2437945ce05c9fb247a8 to your computer and use it in GitHub Desktop.
Save indrayam/af21f0746cfe2437945ce05c9fb247a8 to your computer and use it in GitHub Desktop.
Simplest FileIO in Java (Part 2)
package dev.anandsharma.corejava.ch3;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.Scanner;
public class FileIOTest2 {
public static final String ABSOLUTE_FILENAME = "/Users/anasharm/Downloads/test2.txt";
public static void main(String[] args) throws IOException {
FileWriter fw = new FileWriter(ABSOLUTE_FILENAME, StandardCharsets.UTF_8);
PrintWriter pw = new PrintWriter(fw, true);
pw.printf("%s: %s%n", "Name", "Anand Sharma");
pw.printf("%s: %d%n", "Age", 25);
pw.printf("%s: %s%n", "Company", "Cisco Systems Inc.");
pw.close();
Scanner in = new Scanner(Path.of(ABSOLUTE_FILENAME), StandardCharsets.UTF_8);
while (in.hasNextLine()) {
System.out.printf("%s%n", in.nextLine());
}
in.close();
System.out.println("Over and out!");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment