Writing and Appending Data to a Text File

Writing and Appending to a Text file

This is a follow up to this post about Reading CSV Data from a file.

The Java file io functions BufferedWriter and FileWriter work very much like the BufferedReader and FileReader classes. you can save a little typing by declaring one instance inside the other, like this:

BufferedWriter *variablename* = new BufferedWriter(new FileWriter("filename.txt", boolean *append*))

The boolean append tells it whether you want to create a new file or append an existing one. Use this carefully, as you can easily delete important data. Set this to true to append, or false to create a new file. Also note, if this is set to true, if the file does not exist it will still be created. Both BufferedReader and BufferedWriter have checked exceptions, which means they must be handled in some way, such as a try/catch block.

So, say we wanted to add an entry to our Inventory text file from the last example. Remember it is a CSV, or comma-delimited file like this: (inventory.txt)

Blinker Fluid,10,10.99
Duncel Component,100,27.85
Widget1a,25,15.25
Widget2b,100,4.99

And the Inventory class:

public class Inventory
{
    private String item;
    private int qty;
    private float price;

    public Inventory(String item, int qty, float price)
    {
        this.item = item;
        this.qty = qty;
        this.price = price;
    }

    public float getTotal()
    {
        return qty * price;
    }

    public String toString()
    {
        return "Item: " + item + "\n" + "Quantity: " + qty + "\n"
                    + "Price: " + price;
    }
}

here is the code to add one line to the file:

try
{
	// create Bufferedwriter instance with a FileWriter
	// the flag set to 'true' tells it to append a file if file exists
	BufferedWriter out = new BufferedWriter(new FileWriter("inventory.txt", true));

	// write the text string to the file
	out.write("Left Handed Wrench,200,2.78f");

	// write a `newline` to the file
	out.newLine();

	// close the file
	out.close();
}

// handle exceptions
catch (IOException ioe)
{
	ioe.printStackTrace();
}

The output file will now look like:

Blinker Fluid,10,10.99
Duncel Component,100,27.85
Widget1a,25,15.25
Widget2b,100,4.99
Left Handed Wrench,200,2.78f

Note that the write() command does not add a new line at the end, so that must be done manually, or of course you could add it to the end of the string with a "\n".

Also, if you want to write multiple lines of text before the close() function is called, you want to use the flush() method to push the lines from the buffer to the file.

Now, in our last example, we were loading the data from the file into an ArrayList of Inventory objects. Let's say you wanted to take an existing Inventory object you created and write it to the file. The esiest way would e to add another method to the Inventory class first that returns a comma-delimited string like this:

public String toCSVString()
{
	return item + "," + qty + "," + price;
}

We also should wrap the file writing code into a static method, so it is easier to reuse:

public static void writeFile(String filename, String output, boolean append)
{
	try
	{
		// create Bufferedwriter instance with a FileWriter
		// the flag set to 'true' tells it to append a file if file exists
		BufferedWriter out = new BufferedWriter(new FileWriter(filename, append));
		out.write(output);
		out.newLine();
		out.close();
	}
	catch (IOException ioe)
	{
		ioe.printStackTrace();
	}
}

Here is both our first example and an example of using objects, with the static method:

	// append line to file

	FileFunctions.writeFile("inventory.txt", "Left Handed Wrench,200,2.78f", true);

	// make new Inventory object
	Inventory newItem = new Inventory("Cool Hat", 150, 19.99f);

	// write to file using object method to get 
	// comma-delimited string, with `append` flag set to false
	// OVER-WRITES FILE!!
	FileFunctions.writeFile("inventory.txt", newItem.toCSVString(), false);

The output will just be this:

Cool Hat,150,19.99

As we over-write the file in that last line.

Here is the full program FileFunctions.java:

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class FileFunctions
{

	public static void writeFile(String filename, String output, boolean append)
	{
		try
		{
			// create Bufferedwriter instance with a FileWriter
			// the flag set to 'true' tells it to append a file if file exists
			BufferedWriter out = new BufferedWriter(new FileWriter(filename, append));
			out.write(output);
			out.newLine();
			out.close();
		}
		catch (IOException ioe)
		{
			ioe.printStackTrace();
		}
	}

	public static ArrayList<Inventory> readFile(String filename)
	{
		// create ArrayList to store the invetory objects
		ArrayList<Inventory> invItem = new ArrayList<>();
		try
		{
			// create a Buffered Reader object instance with a FileReader
			BufferedReader br = new BufferedReader(new FileReader("inventory.txt"));

			// read the first line from the text file
			String fileRead = br.readLine();

			// loop until all lines are read
			while (fileRead != null)
			{

				// use string.split to load a string array with the values from each line of
				// the file, using a comma as the delimiter
				String[] tokenize = fileRead.split(",");

				// assume file is made correctly
				// and make temporary variables for the three types of data
				String tempItem = tokenize[0];
				int tempQty = Integer.parseInt(tokenize[1]);
				float tempPrice = Float.parseFloat(tokenize[2]);

				// creat temporary instance of Inventory object
				// and load with three data values
				Inventory tempObj = new Inventory(tempItem, tempQty, tempPrice);

				// add to array list
				invItem.add(tempObj);

				// read next line before looping
				// if end of file reached 
				fileRead = br.readLine();
			}

			// close file stream
			br.close();
		}
		
		// handle exceptions
		catch (FileNotFoundException fnfe)
		{
			System.out.println("file not found");
			return null;
		}

		catch (IOException ioe)
		{
			ioe.printStackTrace();
		}

		return invItem;		
	}

	public static void main( String [] args )
	{

		List<Inventory> invList = FileFunctions.readFile("inventory.txt");
		// display inventory
		for (Inventory each : invList)
		{
			System.out.println("====================");
			System.out.println(each);
			System.out.println();
			System.out.printf("Total value = %8.2f %n", each.getTotal());

		}

		// append line to file

		FileFunctions.writeFile("inventory.txt", "Left Handed Wrench,200,2.78f", true);

		// make new Inventory object
		Inventory newItem = new Inventory("Cool Hat", 150, 19.99f);

		// write to file using object method to get 
		// comma-delimited string
		// OVER-WRITES FILE!!
		FileFunctions.writeFile("inventory.txt", newItem.toCSVString(), false);

	}

}

Posted May 2, 2015, 9:12 PM UTC · edited

31
Loading comments…