In your CS classes, you've probably implemented input and output using standard input and standard output, or using [`Scanner`](https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html) to read input and `System.out.print` to print output. These methods work, but `Scanner` and `System.out.print` are slow when we have to handle inputting and outputting tens of thousands of lines. Thus, we use `BufferedReader` and `PrintWriter` instead, which are faster because they buffer the input and output and handle it all at once as opposed to parsing each line individually.
| `r.next()` | Reads the next token (up to a whitespace) and returns a `String` |
| `r.nextInt()` | Reads the next token (up to a whitespace) and returns as an `int` |
| `r.nextLong()` | Reads the next token (up to a whitespace) and returns as a `long` |
| `r.nextDouble()` | Reads the next token (up to a whitespace) and returns as a `double` |
| `pw.println()` | Prints the argument to designated output stream and adds newline |
| `pw.print()` | Prints the argument to designated output stream |
## File I/O
In USACO, input is read from a file called `problemname.in`, and output is printed to a file called `problemname.out`. Note that you'll have to rename the `.in` and `.out` files. For example, for [this problem](http://www.usaco.org/index.php?page=viewproblem2&cpid=1035), you would replace `problemname` with `socdist1` to get `socdist1.in` and `socdist1.out`.
In order to test a program, create a file called `problemname.in`, and then run the program. The output will be printed to `problemname.out`.
## C++
You will need the [<cstdio\>](http://www.cplusplus.com/reference/cstdio/) or the [<fstream\>](http://www.cplusplus.com/reference/fstream/) library. Essentially, replace every instance of the word `problemname` in the word below with the input/output file name, which should be given in the problem.
Below, we have included C++ templates for input and output. We use `using namespace std;` so that we don't have to preface standard library functions with `std::` each time we use them.
If `<cstdio>` is used:
```cpp
#include <cstdio>
using namespace std;
int main() {
freopen("problemname.in", "r", stdin);
freopen("problemname.out", "w", stdout);
// rest of your code ...
}
```
If `<fstream>` is used: (Note that you cannot use C-style I/O (`scanf`, `printf`) with this method):
```cpp
#include <fstream>
using namespace std;
int main() {
ifstream fin("problemname.in");
ofstream fout("problemname.out");
// rest of your code ...
}
```
## Java
We can modify the template above to support file I/O.