List of utility methods to do Scanner Read All
String | readTextFile(File file) read Text File return readString(new FileInputStream(file)); |
String | readTextFile(File file) read Text File try { return readText(new FileInputStream(file)); } catch (Exception e) { throw new RuntimeException("Reading text file \"" + file + "\" failed", e); |
String | readTextFile(String filename) read Text File return readTextFile(new File(filename)); |
String | readTextFile(String path) read Text File StringBuilder result = new StringBuilder(""); File file = new File(path); try (Scanner scanner = new Scanner(file)) { while (scanner.hasNextLine()) { String line = scanner.nextLine(); result.append(line).append(System.lineSeparator()); scanner.close(); ... |
List | readTextFile(String path) Read the text file by one whole line and add each line to a String list. List<String> wordList = new ArrayList<String>(); Scanner input = null; try { input = new Scanner(new FileInputStream(new File(path)), "UTF-8"); while (input.hasNext()) { String text = input.nextLine().trim(); if (text.startsWith("#")) continue; ... |
String | readTextFileAsString(File textFile) Read the entire given file and return the contents as one long String. StringBuilder outputText = new StringBuilder(); Scanner scanner = new Scanner(textFile); while (scanner.hasNextLine()) { outputText.append(scanner.nextLine()).append(String.format("%n")); scanner.close(); String actualText = outputText.toString(); return actualText; ... |