CIS 35A: Introduction to Java Programming

Home | Green Sheet | Lectures | Assignments | FAQ | Grades

Files

Text and Binary Files
Text files
Class with a text file

A class that works with a text file


import java.io.*;
import java.util.*;

public class ProductTextFile implements ProductDAO
{
    private File productsFile = null;
    private final String FIELD_SEP = "\t";

    public ProductTextFile()
    {
        productsFile = new File("products.txt");
    }

    private void checkFile() throws IOException
    {
        // if the file doesn't exist, create it
        if (!productsFile.exists())
            productsFile.createNewFile();
    }

    private boolean saveProducts(ArrayList products)
    {
        PrintWriter out = null;
        try
        {
            this.checkFile();

            // open output stream for overwriting
            out = new PrintWriter(
                  new BufferedWriter(
                  new FileWriter(productsFile)));

            // write all products in the array list
            // to the file
            for (Product p : products)
            {
                out.print(p.getCode() + FIELD_SEP);
                out.print(p.getDescription() + FIELD_SEP);
                out.println(p.getPrice());
            }
        }

        catch(IOException ioe)
        {
            ioe.printStackTrace();
            return false;
        }
        finally
        {
            this.close(out);
        }
        return true;
    }

    private void close(Closeable stream)
    {
        try
        {
            if (stream != null)
                stream.close();
        }
        catch(IOException ioe)
        {
            ioe.printStackTrace();
        }
    }

    public ArrayList getProducts()
    {
        BufferedReader in = null;
        try
        {
            this.checkFile();

            in = new BufferedReader(
                 new FileReader(productsFile));

            ArrayList products = new ArrayList();

            // read all products stored in the file
            // into the array list
            String line = in.readLine();
            while(line != null)
            {
                String[] columns = line.split(FIELD_SEP);
                String code = columns[0];
                String description = columns[1];
                String price = columns[2];

                Product p = new Product(
                    code, description, Double.parseDouble(price));
                products.add(p);

                line = in.readLine();
            }
            return products;
        }
        catch(IOException ioe)
        {
            ioe.printStackTrace();
            return null;
        }
        finally
        {
            this.close(in);
        }
    }

    public Product getProduct(String code)
    {
        ArrayList products = this.getProducts();
        for (Product p : products)
        {
            if (p.getCode().equals(code))
                return p;
        }
        return null;
    }

    public boolean addProduct(Product p)
    {
        ArrayList products = this.getProducts();
        products.add(p);
        return this.saveProducts(products);
    }

    public boolean deleteProduct(Product p)
    {
        ArrayList products = this.getProducts();
        products.remove(p);
        return this.saveProducts(products);
    }


    public boolean updateProduct(Product newProduct)
    {
        ArrayList products = this.getProducts();

        // get the old product and remove it
        Product oldProduct = this.getProduct(newProduct.getCode());
        int i = products.indexOf(oldProduct);
        products.remove(i);

        // add the updated product
        products.add(i, newProduct);

        return this.saveProducts(products);
    }
}
Previous | Connect a character output stream to a file | Write to a text file | Connect a character input stream to a file | Read from a text file | Interface with file I/O | Class with a text file | Next