Fastest read / write in java

I am using simple bufferreader but it is slow enough and due to this in some program i got time limit exceed problem . So guys tell me about fastest read and write code for java program.

1 Like

@theshubhamgoel This is fastest read & write code as far as I know.

    class InputReader {
    private InputStream stream;
    private byte[] buf = new byte[1024];
    private int curChar;
    private int numChars;
    private SpaceCharFilter filter;
 
    public InputReader(InputStream stream) {
        this.stream = stream;
    }
    
    public int read() {
        if (numChars == -1)
            throw new InputMismatchException();
        if (curChar >= numChars) {
            curChar = 0;
            try {
                numChars = stream.read(buf);
            } catch (IOException e) {
                throw new InputMismatchException();
            }
            if (numChars <= 0)
                return -1;
        }
        return buf[curChar++];
    }
    
    public int readInt() {
        int c = read();
	while (isSpaceChar(c))
            c = read();
	int sgn = 1;
	if (c == '-') {
            sgn = -1;
            c = read();
	}
        int res = 0;
	do {
            if (c < '0' || c > '9')
                throw new InputMismatchException();
            res *= 10;
            res += c - '0';
            c = read();
        } while (!isSpaceChar(c));
        return res * sgn;
    }
    
    public long readLong() {
        int c = read();
        while (isSpaceChar(c))
            c = read();
        int sgn = 1;
        if (c == '-') {
            sgn = -1;
            c = read();
        }
	long res = 0;
        do {
            if (c < '0' || c > '9')
                throw new InputMismatchException();
		res *= 10;
		res += c - '0';
                c = read();
        } while (!isSpaceChar(c));
        return res * sgn;
    }
    
    public String readString() {
        int c = read();
        while (isSpaceChar(c))
            c = read();
        StringBuilder res = new StringBuilder();
	do {
            res.appendCodePoint(c);
            c = read();
        } while (!isSpaceChar(c));
	return res.toString();
    }
 
    public boolean isSpaceChar(int c) {
        if (filter != null)
            return filter.isSpaceChar(c);
        return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
    }
 
    public String next() {
        return readString();
    }
    
    public interface SpaceCharFilter {
        public boolean isSpaceChar(int ch);
    }
}
 
class OutputWriter {
    private final PrintWriter writer;
    
    public OutputWriter(OutputStream outputStream) {
        writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
    }
    
    public OutputWriter(Writer writer) {
        this.writer = new PrintWriter(writer);
    }
    
    public void print(Object...objects) {
        for (int i = 0; i < objects.length; i++) {
            if (i != 0)
                writer.print(' ');
		writer.print(objects[i]);
        }
    }
    
    public void printLine(Object...objects) {
        print(objects);
	writer.println();
    }
    
    public void close() {
        writer.close();
    }
} 

IMP: If you are getting TLE using BufferedReader in java, then usually your program complexity is not good enough.

Thanks & Regards

CrucifiX

1 Like

@crucifix thanks ,one more question ,if i use biginteger then time taken also increase so tell me temp-let for working on big integer for big data.

1 Like

I’m afraid I can not help you with that, my primary language is c++, have not done much coding in java. :confused:

You can also read string from that already given fast method and then pass it to the constructor and create a reference.

ex:

String str =  "1234567890";

BigInteger newVar = new BigInteger(str);

__

thts it…!!!

You can also read as long or int and do:

n = //read as long or int

BigInteger newVar = BigInteger.valueOf(n).

To increase performance reading long and ints is better than reading Strings, when possible of course …:slight_smile:

@junior94

if it is possible to take the input in long and ints then why anyone will use BigInteger…

1 Like