Reading a text file using java.util.Scanner in Java.
StringBuilder text = new StringBuilder(); try(Scanner scanner = new Scanner(new BufferedReader(new FileReader("<FILENAME>")))) { while(scanner.hasNext()) { text.append(scanner.nextLine()).append("\n"); } } catch (FileNotFoundException e) { e.printStackTrace(); } System.out.println(text.toString());
Where:
FILENAME | The text file to read. |
Leave a Reply