Matt Murch
Matt Murch

Reputation: 23

My array list to .txt file program is giving me several errors

I have an inventory application that sets a list of strings based on user input. Than it is supposed to export the strings to a .txt file. I have never used FileWriter before, and am very much stuck.

Any help is appreciated.

Here is all the relevant code:

The three lines near the end that have 'writer.' in them are giving me a unreported exception IOException; must be caught or declared to be thrown.

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

public class mVentory extends javax.swing.JFrame {

    /**
     * Creates new form mVentory
     */
    public mVentory() {
        initComponents();

    }

    public class ProductInfo {

        String name;
        String des;
        String ID;
        String num;

        public ProductInfo(String name, String des, String ID, String num) {
            this.name = name;
            this.des = des;
            this.ID = ID;
            this.num = num;
        }
    }

    public static void Inventory() {

    }

    //creat Array
    ArrayList<String> Inventory = new ArrayList<String>();

    private void AddGoActionPerformed(java.awt.event.ActionEvent evt) {
        // Add Item
        String Name, Description, Identification, Number, Item;

        Name = NameIn.getText();
        Description = DesIn.getText();
        Identification = IDIn.getText();
        Number = NumIn.getText();

        Item = "" + Name + "," + Description + "," + Identification + "," + Number + ".";

        Inventory.add(new String(Item));

        NameIn.setText("");
        DesIn.setText("");
        IDIn.setText("");
        NumIn.setText("");

    }

    private void ExportGoActionPerformed(java.awt.event.ActionEvent evt) {
        FileWriter writer;
        writer = new FileWriter("Inventory.txt");
        for (String str : Inventory) {
            writer.write(str);
        }
        writer.close();
    }
}

Upvotes: 0

Views: 89

Answers (2)

Jazzepi
Jazzepi

Reputation: 5480

FileWriter extends OutputStreamWriter which is where it gets its write method from.

http://docs.oracle.com/javase/7/docs/api/java/io/OutputStreamWriter.html#write(char[], int, int)

According to the docs write throws an IOException.

In Java you have to either wrap the code that throws an exception in a try catch block. Or throw the exception.

private void ExportGoActionPerformed(java.awt.event.ActionEvent evt) {
    FileWriter writer;
    try {
        writer = new FileWriter("Inventory.txt");
        for (String str : Inventory) {
            writer.write(str);
        }
        writer.close();
    } catch (IOException e) {
         //Do something here to handle the error. Maybe publish a notification to the user that their file didn't write.
    } finally {
        try {
            writer.close();
        } catch (IOException ignore){}
    }
}

Note that the FileWriter constructor you're using can also throw an IOException (http://docs.oracle.com/javase/7/docs/api/java/io/FileWriter.html#FileWriter(java.lang.String)) as well as the close method. So you need to include all of those statements within the try block.

The finally block is there to close your handle to the file if at all possible. The try-with-resource gets rid of the need to do that though. So I recommend using it.

http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html

Upvotes: 1

Wouter
Wouter

Reputation: 4016

You need a try/catch statement around your writer.

Such as:

private void ExportGoActionPerformed(java.awt.event.ActionEvent evt) {
    FileWriter writer = null;
    try {
        writer = new FileWriter("Inventory.txt");
        for (String str : Inventory) {
            writer.write(str);
        }
    } catch (IOException e) {
        //handle exception, rethrow or log
        e.printStackTrace();
    } finally {
        try {
            writer.close();
        } catch (IOException ignore){

        }
    }
 }

Upvotes: 1

Related Questions