Last active
July 30, 2016 17:32
-
-
Save zeako/2321aa4b5a59943c3f250c68bcd4a0a8 to your computer and use it in GitHub Desktop.
Method for reading input files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Read text file lines. | |
* | |
* @param fileName input file name should be at the same directory as the runner | |
* @return list of string arrays | |
*/ | |
public static List<String[]> readFile(String fileName) { | |
LinkedList<String[]> lines = new LinkedList<>(); | |
try (BufferedReader br = new BufferedReader(new FileReader(fileName))) { | |
String line; | |
while ((line = br.readLine()) != null) | |
lines.add(line.split(" ")); | |
} catch (FileNotFoundException e) { | |
System.err.println("File '" + e.getMessage() + "' does not exist, please provide correct file name"); | |
System.exit(0); | |
} catch (IOException e) { | |
System.err.println("An error occurred while reading the file"); | |
System.exit(1); | |
} | |
return lines; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment