Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static ArrayList readFile(String nameOfFile)
- {
- ArrayList<String> list = new ArrayList();
- Scanner file = null;
- // The name of the file to open.
- // This will reference one line at a time
- String line = null;
- try {
- // FileReader reads text files in the default encoding.
- FileReader fileReader =
- new FileReader(nameOfFile);
- // Always wrap FileReader in BufferedReader.
- BufferedReader bufferedReader =
- new BufferedReader(fileReader);
- while((line = bufferedReader.readLine()) != null) {
- list.add(line);
- }
- // Always close files.
- bufferedReader.close();
- }
- catch(FileNotFoundException ex) {
- System.out.println(
- "Unable to open file '" +
- nameOfFile + "'");
- }
- catch(IOException ex) {
- System.out.println(
- "Error reading file '"
- + nameOfFile + "'");
- // Or we could just do this:
- // ex.printStackTrace();
- }
- return list;
- }// readFile
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement