Advertisement
Evang12

Java Fast File IO

Jun 26th, 2022
643
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. public class Main {
  5.     static BufferedReader br;
  6.     static StringTokenizer st;
  7.     static PrintWriter pw;
  8.  
  9.     public static void main(String[] args) throws Exception {
  10.         br = new BufferedReader(new FileReader("input.txt"));
  11.         pw = new PrintWriter("output.txt");
  12.  
  13.         int n = readInt();
  14.         String s = next();  // reads a string (not necessarily an entire line!)
  15.         String entireLine = readLine();
  16.         double myDouble = readDouble();
  17.  
  18.         pw.println("Use PrintWriter -- it's faster!");
  19.         pw.print("PrintWriter is shorter to write (compared to System.out.println)\n");
  20.         pw.println("But be careful when using PrintWriter in interactive problems " +
  21.             "use pw.flush() to make sure pw prints before the next input!");
  22.         pw.flush();
  23.  
  24.         pw.close();  // always write this line before ending the program!!
  25.     }
  26.  
  27.     static String readLine() throws IOException {
  28.         return br.readLine().trim();
  29.     }
  30.     static String next() throws IOException {
  31.         while(st == null || !st.hasMoreTokens())
  32.             st = new StringTokenizer(readLine());
  33.         return st.nextToken();
  34.     }
  35.     static int readInt() throws IOException {
  36.         return Integer.parseInt(next());
  37.     }
  38.     static double readDouble() throws IOException {
  39.         return Double.parseDouble(next());
  40.     }
  41. }
  42.  
Advertisement
RAW Paste Data Copied
Advertisement