Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * open files for reading and writing
- *
- * Roshan Gampala
- * September 27, 2018
- **/
- import java.util.Scanner;
- import java.io.FileNotFoundException;
- import java.io.PrintWriter;
- public class OpenFile {
- //open file to read using scanner class
- //parameter fileName = name of the file to open
- //return the scanner object to the file
- public static Scanner openToRead(String fileName) {
- Scanner input = null;
- try {
- input = new Scanner(new java.io.File(fileName));
- }
- catch (FileNotFoundException e){
- System.err.println("ERROR; cannot open " + fileName +
- " for reading.");
- System.exit(404);
- }
- return input;
- }
- // opens a file to write using the Print Writer class
- // @param: fileName - name of the file to open
- //return Print Writer object to the file
- public static PrintWriter openToWrite(String fileName) {
- PrintWriter output = null;
- try {
- output = new PrintWriter(new java.io.File(fileName));
- }
- catch (FileNotFoundException e) {
- System.err.println("ERROR: Cannot open " + fileName +
- " for writing.");
- System.exit(401);
- }
- return output;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement