Advertisement
Guest User

Untitled

a guest
Oct 17th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. /**
  2. * open files for reading and writing
  3. *
  4. * Roshan Gampala
  5. * September 27, 2018
  6. **/
  7. import java.util.Scanner;
  8. import java.io.FileNotFoundException;
  9. import java.io.PrintWriter;
  10.  
  11. public class OpenFile {
  12.  
  13. //open file to read using scanner class
  14. //parameter fileName = name of the file to open
  15. //return the scanner object to the file
  16.  
  17. public static Scanner openToRead(String fileName) {
  18.  
  19. Scanner input = null;
  20. try {
  21. input = new Scanner(new java.io.File(fileName));
  22. }
  23. catch (FileNotFoundException e){
  24. System.err.println("ERROR; cannot open " + fileName +
  25. " for reading.");
  26.  
  27. System.exit(404);
  28. }
  29. return input;
  30. }
  31.  
  32. // opens a file to write using the Print Writer class
  33. // @param: fileName - name of the file to open
  34. //return Print Writer object to the file
  35. public static PrintWriter openToWrite(String fileName) {
  36. PrintWriter output = null;
  37. try {
  38. output = new PrintWriter(new java.io.File(fileName));
  39. }
  40. catch (FileNotFoundException e) {
  41. System.err.println("ERROR: Cannot open " + fileName +
  42. " for writing.");
  43. System.exit(401);
  44. }
  45. return output;
  46. }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement