-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileIO.java
More file actions
40 lines (36 loc) · 1.1 KB
/
Copy pathFileIO.java
File metadata and controls
40 lines (36 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*
* Utility function for reading a file into a list of strings
*
* e.g. calling FileIO.readFile("f.txt");
* where file f.txt contains "abc\nde\n\ngh\n"
* will return an array list <"abc", "de", "", "gh">
*
* @author Rachel Cardell-Oliver
* based on Barnes and Koelling ResponseReader class
* @version May 2017
*/
import java.io.*;
import java.util.*;
public class FileIO
{
/**
* Read a file from the current directory into a list
* @param String filename name of the file to be read
* @throws FileNotFoundException, IOException
* @return ArrayList<String> of lines from the file
*/
public static ArrayList<String> readFile(String filename)
throws FileNotFoundException, IOException
{
ArrayList<String> filelines = new ArrayList<String>();
BufferedReader reader =
new BufferedReader(new FileReader(filename));
String line = reader.readLine();
while(line != null) {
filelines.add(line);
line = reader.readLine();
}
reader.close();
return filelines;
}
}