Created
February 21, 2013 12:07
-
-
Save jtkorb/5004318 to your computer and use it in GitHub Desktop.
A program to demonstrate simple text-based input and output in Java.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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