Skip to content

Instantly share code, notes, and snippets.

@zeako
Last active July 30, 2016 17:32
Show Gist options
  • Save zeako/2321aa4b5a59943c3f250c68bcd4a0a8 to your computer and use it in GitHub Desktop.
Save zeako/2321aa4b5a59943c3f250c68bcd4a0a8 to your computer and use it in GitHub Desktop.
Method for reading input files
/**
* 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