-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEx3Utils.java
More file actions
68 lines (57 loc) · 1.71 KB
/
Ex3Utils.java
File metadata and controls
68 lines (57 loc) · 1.71 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* A utilities class for ex3 in oop.
*
*/
public class Ex3Utils {
/**
* Reads a text file (such that each line contains a single word),
* and returns a string array of its lines.
* @param fileName Text file to read.
* @return Array with the file's content (returns null if the IOException occurred).
*/
public static String[] file2array(String fileName) {
// A list to hold the file's content
List<String> fileContent = new ArrayList<String>();
// Reader object for reading the file
BufferedReader reader = null;
try {
// Open a reader
reader = new BufferedReader(new FileReader(fileName));
// Read the first line
String line = reader.readLine();
// Go over the rest of the file
while (line != null) {
// Add the line to the list
fileContent.add(line);
// Read the next line
line = reader.readLine();
}
} catch (FileNotFoundException e) {
System.err.println("ERROR: The file: " + fileName + " is not found.");
return null;
} catch (IOException e) {
System.err.println("ERROR: An IO error occurred.");
return null;
} finally {
// Try to close the file
try {
if(reader != null)
reader.close();
else
return null;
} catch (IOException e) {
System.err.println("ERROR: Could not close the file " + fileName + ".");
}
}
// Convert the list to an array and return the array
String[] result = new String[fileContent.size()];
fileContent.toArray(result);
return result;
}
}