Advertisement
Guest User

Untitled

a guest
Sep 29th, 2016
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. public static ArrayList readFile(String nameOfFile)
  2. {
  3.  
  4. ArrayList<String> list = new ArrayList();
  5. Scanner file = null;
  6. // The name of the file to open.
  7.  
  8.  
  9. // This will reference one line at a time
  10. String line = null;
  11. try {
  12. // FileReader reads text files in the default encoding.
  13. FileReader fileReader =
  14. new FileReader(nameOfFile);
  15.  
  16. // Always wrap FileReader in BufferedReader.
  17. BufferedReader bufferedReader =
  18. new BufferedReader(fileReader);
  19.  
  20. while((line = bufferedReader.readLine()) != null) {
  21. list.add(line);
  22. }
  23.  
  24. // Always close files.
  25. bufferedReader.close();
  26. }
  27. catch(FileNotFoundException ex) {
  28. System.out.println(
  29. "Unable to open file '" +
  30. nameOfFile + "'");
  31. }
  32. catch(IOException ex) {
  33. System.out.println(
  34. "Error reading file '"
  35. + nameOfFile + "'");
  36. // Or we could just do this:
  37. // ex.printStackTrace();
  38. }
  39. return list;
  40. }// readFile
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement