Created
November 29, 2016 04:55
-
-
Save anonymous/469050313cd1263e64befb8b35862aa1 to your computer and use it in GitHub Desktop.
Java - Simplified static File Functions, specifically for Advent of Code challenges
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
import java.io.BufferedReader; | |
import java.io.FileReader; | |
import java.io.IOException; | |
import java.nio.file.Files; | |
import java.nio.file.Paths; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.function.Function; | |
import java.util.regex.PatternSyntaxException; | |
/** | |
* @author /u/Philboyd_Studge on 12/5/2015. | |
*/ | |
public class FileIO { | |
/** | |
* Load file into one String | |
* Assuming file has no line separators! | |
* i.e. 2015 Day 1, 2015 Day 3 | |
* @param filename file in current working directory or full pathname | |
* @return String | |
*/ | |
public static String getFileAsString(String filename) { | |
String test = ""; | |
try { | |
test = new String(Files.readAllBytes(Paths.get(filename))); | |
} catch (IOException ioe) { | |
ioe.printStackTrace(); | |
} | |
return test; | |
} | |
/** | |
* Performs given Function on file, one line at a time, and summing the results | |
* @param filename file in current working directory or full pathname | |
* @param func Function that takes a String as parameter and returns an int | |
* @return int summed result | |
*/ | |
public static int performIntActionOnLine(String filename, Function<String, Integer> func) { | |
int result = 0; | |
try (BufferedReader br = new BufferedReader(new FileReader(filename))) { | |
String input = br.readLine(); | |
while (input != null) { | |
result += func.apply(input); | |
input = br.readLine(); | |
} | |
} catch (IOException ioe) { | |
ioe.printStackTrace(); | |
} | |
return result; | |
} | |
/** | |
* Loads entire file, one line at a time, into List | |
* @param filename file in current working directory or full pathname | |
* @return ArrayList of strings | |
*/ | |
public static List<String> getFileAsList(String filename) { | |
List<String> list = new ArrayList<>(); | |
try (BufferedReader br = new BufferedReader(new FileReader(filename))) { | |
String input; | |
while ((input = br.readLine()) != null) { | |
list.add(input); | |
} | |
} catch (IOException ioe) { | |
ioe.printStackTrace(); | |
} | |
return list; | |
} | |
/** | |
* Return an ArrayList of String Arrays, split using the given delimiter | |
* @param filename file in current working directory or full pathname | |
* @param delimiter REGEX string delimite. Catches PatternSyntaxException. | |
* @return List of String Arrays | |
*/ | |
public static List<String[]> getFileLinesSplit(String filename, String delimiter) { | |
List<String[]> list = new ArrayList<>(); | |
try (BufferedReader br = new BufferedReader(new FileReader(filename))) { | |
String input; | |
while ((input = br.readLine()) != null) { | |
try { | |
String[] s = input.split(delimiter); | |
list.add(s); | |
} catch (PatternSyntaxException pse) { | |
System.out.println("Bad regex syntax. Delimiter \"" + delimiter + " \""); | |
return null; | |
} | |
} | |
} catch (IOException ioe) { | |
ioe.printStackTrace(); | |
} | |
return list; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment