Skip to content

Instantly share code, notes, and snippets.

@jtkorb
Created February 21, 2013 12:07
Show Gist options
  • Save jtkorb/5004318 to your computer and use it in GitHub Desktop.
Save jtkorb/5004318 to your computer and use it in GitHub Desktop.
A program to demonstrate simple text-based input and output in Java.
/**
* Simple File IO
*
* A program to demonstrate simple text-based input and output in Java.
*
* This version uses PrintStream, the same class as System.out. An
* alternative implementation would be to use the PrintWriter class.
*
* @author jtk
*
* @date 2/21/2013
*/
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
public class SimpleFileIO {
public static void main(String[] args) {
Scanner scanner = null;
PrintStream ps = null;
// Specify the input File and create a Scanner object for it...
File in = new File("input.txt");
try {
scanner = new Scanner(in);
} catch (FileNotFoundException e) {
e.printStackTrace(); // just print an error stack trace
}
// Specify the output File and create a PrintStream object for it...
File out = new File("output.txt");
try {
ps = new PrintStream(out);
} catch (FileNotFoundException e) {
e.printStackTrace(); // just print an error stack trace
}
// Loop reading lines, processing (trivially), and writing...
while (scanner.hasNextLine()) {
String s = scanner.nextLine();
s = s.toUpperCase();
ps.println(s);
}
// Close the output file to flush internal buffers...
ps.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment