BuffererReader in Java

For reading input data fast, USACO Guide suggests using a BufferedReader, reading one line at a time, and using a string parser to break the line into individual numbers.

How about

Scanner input = new Scanner(new BufferedReader(new FileReader("file.txt")));
int n = sc.nextInt();
for (int i = 0; i < n; i++)
    read via sc.nextInt()

In a test I did on a CSES problem, this was only a tiny bit slower than the first approach.

Isn’t the fact it’s a buffered reader supposed to mean it does essentially the same thing under the hood as the first approach? Is there any reason not to use my second approach?

What problem did you test it on? When I tried submitting the solution here with Scanner wrapped around BufferedReader it received TLE on test cases 6 and onwards (before the change, it passed test case 6 in 0.7s).

import java.io.*;
import java.util.*;

public class TrafficLights {
	public static void main(String[] args) throws IOException {
		Kattio io = new Kattio(); 
	
		int streetLength = io.r.nextInt();
		int lightNum = io.r.nextInt();

		// Using an array to read values since we can't get values from sets in Java
		int[] opArray = new int[lightNum];
		NavigableSet<Integer> streetPositions = new TreeSet<>();
		// Initialize the set with beginning and ending values
		streetPositions.add(0);
		streetPositions.add(streetLength);

		for (int i = 0; i < lightNum; i++) {
			int nextTrafficLight = io.r.nextInt();
			opArray[i] = nextTrafficLight;
			streetPositions.add(nextTrafficLight);
		}
		int[] gapsArray = new int[lightNum];
		int prev = 0;
		int maxGap = 0;
		// Find the longest passage once all the streetlights are added
		for (int i : streetPositions) {
			maxGap = Math.max(i - prev, maxGap);
			prev = i;
		}
		gapsArray[lightNum - 1] = maxGap;
		/*
		 * Remove the streetlights in reverse order to how they were added, then find
		 * the gap created by removing each. Find the biggest current gap, and 
		 * add it to the next lowest index in answer.
		 */
		for (int i = lightNum - 1; i > 0; i--) {
			streetPositions.remove(opArray[i]);
			int low = streetPositions.lower(opArray[i]);
			int high = streetPositions.higher(opArray[i]);
			maxGap = Math.max(maxGap, high - low);
			gapsArray[i - 1] = maxGap;
		}
		//  Use StringBuilder to print out the array quicker
		StringBuilder sb = new StringBuilder();
		for (int i : gapsArray) {
			sb.append(i).append(" ");
		}
		io.println(sb);
		io.close();
	}
	static class Kattio extends PrintWriter {
		public Scanner r;
		private StringTokenizer st;
	
		// standard input
		public Kattio() { this(System.in, System.out); }
		public Kattio(InputStream i, OutputStream o) {
			super(o);
			r = new Scanner(new BufferedReader(new InputStreamReader(i)));
		}
		// USACO-style file input
		public Kattio(String problemName) throws IOException {
			super(new FileWriter(problemName + ".out"));
			r = new Scanner(new BufferedReader(new FileReader(problemName + ".in")));
		}
	
		// returns null if no more input
		// public String next() {
		// 	try {
		// 		while (st == null || !st.hasMoreTokens())
		// 			st = new StringTokenizer(r.readLine());
		// 		return st.nextToken();
		// 	} catch (Exception e) { }
		// 	return null;
		// }
	
		// public int nextInt() { return Integer.parseInt(next()); }
		// public double nextDouble() { return Double.parseDouble(next()); }
		// public long nextLong() { return Long.parseLong(next()); }
	}
}

Honestly I don’t remember which problem. Perhaps Movie Festival II. I will take your evidence as evidence it does matter. Thank you.