Advertisement
daniel_079

Scanner

Dec 11th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.36 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.FileNotFoundException;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.io.PrintWriter;
  7. import java.nio.charset.StandardCharsets;
  8.  
  9. public class FastScanner implements AutoCloseable {
  10.     InputStreamReader reader;
  11.     boolean skipn;
  12.     int cur;
  13.  
  14.     public FastScanner(FileInputStream fileInputStream) throws IOException {
  15.         skipn = (System.lineSeparator().length() == 2);
  16.         reader = new InputStreamReader(fileInputStream, StandardCharsets.UTF_8);
  17.         cur = reader.read();
  18.     }
  19.  
  20.     public String readLine() throws IOException {
  21.         StringBuilder sb = new StringBuilder();
  22.         boolean eol = false;
  23.         do {
  24.             if ((cur == '\r') || (cur == '\n')) {
  25.                 eol = true;
  26.                 break;
  27.             }
  28.             sb.append((char) cur);
  29.         } while ((cur = reader.read()) != -1);
  30.         if (eol) {
  31.             if (skipn) {
  32.                 cur = reader.read();
  33.             }
  34.         }
  35.         return sb.toString();
  36.     }
  37.  
  38.     public boolean hasNextLine() throws IOException {
  39.         return cur != -1;
  40.     }
  41.  
  42.     public void close() throws IOException {
  43.         if (reader != null) {
  44.             reader.close();
  45.         }
  46.     }
  47.  
  48.     public static void main(String[] args) throws IOException {
  49.         FastScanner in = new FastScanner(new FileInputStream("Test.txt"));
  50.         PrintWriter out = new PrintWriter(new File("output.txt"), "utf8");
  51.         out.println(in.readLine());
  52.         out.close();
  53.         in.close();
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement