vikram_shaw's blog

By vikram_shaw, history, 21 month(s) ago, In English

I tried to make a template that would work both on your local machine and in any online compiler. Please click here to redirect to my GitHub to find the code.

On your local machine, it will take inputs from your local files, and when you run it in any online compiler, it will use system IO.

I added an environment variable named LOCAL_JAVA in my system and then tried to instantiate it accordingly.

import java.io.*;

abstract class ReaderWriter {
    public BufferedReader in;
    public PrintStream out;
    protected ReaderWriter(BufferedReader br, PrintStream ps) throws IOException {
        in = br;
        out = ps;
    }
}

class OfflineReaderWriter extends ReaderWriter {
    OfflineReaderWriter(String inputFileName, String outputFileName) throws IOException {
        // Read from inputFileName and write to outputFileName
        super(new BufferedReader(new FileReader(inputFileName)), new PrintStream(new File(outputFileName)));
    }
}

class OnlineReaderWriter extends ReaderWriter {
    OnlineReaderWriter() throws IOException {
        // Read from / write to console / terminal 
        super(new BufferedReader(new InputStreamReader(System.in)), new PrintStream(System.out));
    }
}

interface IReaderWriterCreator {
    ReaderWriter Create() throws IOException;
}

class OfflineReaderWriterCreator implements IReaderWriterCreator {
    String inputFileName, outputFileName;
    OfflineReaderWriterCreator(String inputFileName, String outputFileName) {
        this.inputFileName = inputFileName;
        this.outputFileName = outputFileName;
    }
    public ReaderWriter Create() throws IOException {
        return new OfflineReaderWriter(inputFileName, outputFileName);
    }
}

class OnlineReaderWriterCreator implements IReaderWriterCreator {
    public ReaderWriter Create() throws IOException {
        return new OnlineReaderWriter();
    }
}

class ReaderWriterFactory {
    public static ReaderWriter get() throws IOException {
        ReaderWriter readerWriter;
        if(System.getenv("LOCAL_JAVA") == null) {
            readerWriter = new OnlineReaderWriterCreator().Create();
        } else {
            String inputFileName = "input.txt", outputFileName = "output.txt";
            readerWriter = new OfflineReaderWriterCreator(inputFileName, outputFileName).Create();
        }
        return readerWriter;
    }
}

class CleanCode {
    public static void main(String str[]) throws IOException{
        ReaderWriter reader = ReaderWriterFactory.get();
        String name = reader.in.readLine();
        reader.out.println("You name is " + name);
    }
}
  • Vote: I like it
  • +3
  • Vote: I do not like it

| Write comment?
»
21 month(s) ago, # |
  Vote: I like it 0 Vote: I do not like it

Putting aside the environment variable, I believe that your template can benefit from decoupling between the Reader and the Writer. Also, I personally use this IO library. You can have a look at that as well. It's quite a bit faster than the standard BufferedReader and has plenty of useful functions for output as well.