Java File IO

There are lots of ways of writing to files in Java but the IB seem to recommend BufferedReader for input and PrintWriter for output. Here is an example of how to use them.

The program provides a general purpose class to read from an input file called in.txt and write to an output file called out.txt. The main routine uses this class to read lines from a comma-delimited file and write them out with the delimiter changed to a # character. I chose this example because it shows you not only basic file input/output, but it also shows you how to you can use a delimiter to structure your file.

The output file will be created if it doesn’t already exist but you must create an input file called in.txt and save it at the root c: before you run this code. Sample input file contents plus Java code is provided below.

Presidency,President,Took office,Left office,Party,Home State
1,George Washington,30/04/1789,4/03/1797,Independent,Virginia
2,John Adams,4/03/1797,4/03/1801,Federalist,Massachusetts
3,Thomas Jefferson,4/03/1801,4/03/1809,Democratic-Republican,Virginia
4,James Madison,4/03/1809,4/03/1817,Democratic-Republican,Virginia
5,James Monroe,4/03/1817,4/03/1825,Democratic-Republican,Virginia
6,John Quincy Adams,4/03/1825,4/03/1829,Democratic-Republican/National Republican,Massachusetts
7,Andrew Jackson,4/03/1829,4/03/1837,Democratic,Tennessee
8,Martin Van Buren,4/03/1837,4/03/1841,Democratic,New York
9,William Henry Harrison,4/03/1841,4/04/1841,Whig,Ohio
10,John Tyler,4/04/1841,4/03/1845,Whig,Virginia
11,James K. Polk,4/03/1845,4/03/1849,Democratic,Tennessee
12,Zachary Taylor,4/03/1849,9/07/1850,Whig,Louisiana
13,Millard Fillmore,9/07/1850,4/03/1853,Whig,New York
14,Franklin Pierce,4/03/1853,4/03/1857,Democratic,New Hampshire
15,James Buchanan,4/03/1857,4/03/1861,Democratic,Pennsylvania
16,Abraham Lincoln,4/03/1861,15/04/1865,Republican/National Union,Illinois
17,Andrew Johnson,15/04/1865,4/03/1869,Democratic/National Union,Tennessee
18,Ulysses S. Grant,4/03/1869,4/03/1877,Republican,Ohio
19,Rutherford B. Hayes,4/03/1877,4/03/1881,Republican,Ohio
20,James A. Garfield,4/03/1881,19/09/1881,Republican,Ohio
21,Chester A. Arthur,19/09/1881,4/03/1885,Republican,New York
22,Grover Cleveland,4/03/1885,4/03/1889,Democratic,New York
23,Benjamin Harrison,4/03/1889,4/03/1893,Republican,Indiana
24,Grover Cleveland (2nd term),4/03/1893,4/03/1897,Democratic,New York
25,William McKinley,4/03/1897,14/9/1901,Republican,Ohio
26,Theodore Roosevelt,14/9/1901,04/03/09,Republican,New York
27,William Howard Taft,04/03/09,04/03/13,Republican,Ohio
28,Woodrow Wilson,04/03/13,04/03/21,Democratic,New Jersey
29,Warren G. Harding,04/03/21,02/08/23,Republican,Ohio
30,Calvin Coolidge,02/08/23,04/03/29,Republican,Massachusetts
31,Herbert Hoover,04/03/29,04/03/33,Republican,Iowa
32,Franklin D. Roosevelt,04/03/33,12/04/45,Democratic,New York
33,Harry S. Truman,12/04/45,20/01/1953,Democratic,Missouri
34,Dwight D. Eisenhower,20/01/1953,20/01/1961,Republican,Texas
35,John F. Kennedy,20/01/1961,22/11/1963,Democratic,Massachusetts
36,Lyndon B. Johnson,22/11/1963,20/1/1969,Democratic,Texas
37,Richard Nixon,20/1/1969,09/08/74,Republican,California
38,Gerald Ford,09/08/74,20/01/1977,Republican,Michigan
39,Jimmy Carter,20/01/1977,20/01/1981,Democratic,Georgia
40,Ronald Reagan,20/01/1981,20/01/1989,Republican,California
41,George H. W. Bush,20/01/1989,20/01/1993,Republican,Texas
42,Bill Clinton,20/01/1993,20/01/2001,Democratic,Arkansas
43,George W. Bush,20/01/2001,20/01/2009,Republican,Texas
44,Barack Obama,20/01/2009,Incumbent,Democratic,Illinois

package fileiodemo;

import java.util.*;

 * @author robertsonj
public class FileIODemo {

    public static void main(String[] args) {       
        final String OLD_DELIMITER = ",";
        final String NEW_DELIMITER = "#";
        FileIO io = new FileIO();
        String sInput;
        String[] sSplit;
        // get the first line
        sInput = io.input(); 
        while (sInput != null){
            // split the line up into an array of strings on the basis 
            // of the old delimiter
            sSplit = sInput.split(OLD_DELIMITER);
            // loop through the array, writing each string to the output
            // file and using the new delimiter
            for(int i = 0; i<sSplit.length; i++){
                if (i>0) io.output(NEW_DELIMITER, false);
                io.output(sSplit[i], false);
            // add a new line character
            io.output("", true);
            // get the next line
            sInput = io.input();

class FileIO {

    final String OUTPUT_FILENAME = "c:\\out.txt";
    final String INPUT_FILENAME = "c:\\in.txt";
    BufferedReader reader;
    PrintWriter writer;

    FileIO() {
        try {
            reader = new BufferedReader(new FileReader(INPUT_FILENAME));
            writer = new PrintWriter(new FileWriter(OUTPUT_FILENAME));
        } catch (Exception e) {

    void output(String s, boolean newLine) {
        if (newLine) {
        else {

    String input() {
        String s = "";
        try {
            s = reader.readLine();
        } catch (Exception e) {
        return s;

    void closeAll() {
        try {
        } catch (Exception e) {

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s