将数据文件读入多维数组Java

时间:2011-01-29 18:47:06

标签: java arrays datainputstream

我有一个.dat文件,我想加载到自定义数组中。如何让它实际将数据加载到数组中。数据由(String,int,int,double,String)组成。

class CDinventoryItem{


   private CDinventoryItem [] inven = new CDinventoryItem[1000];


    public CDinventoryItem (String title, int itemNumber, int numberofUnits, 
    double unitPrice, String genre){

              DataInputStream input;

            try{

                input = new DataInputStream(new FileInputStream("inventory.dat"));

                inven = input.read(CDinventoryItem[]);  //line I am receiving error on


            }
            catch ( IOException error ){
                JOptionPane.showMessageDialog( null, "File not found",
                "" ,JOptionPane.ERROR_MESSAGE);
            }


        }

}

所以现在readFile就属于自己的类......

class readFile {
public CDinventoryItem[] inven;

    public readFile(){

BufferedReader in = null;

try {
    in = new BufferedReader(new FileReader("inventory.dat"));
    String line = null;
    int i = 0;
    while ((line = in.readLine()) != null) {

        // process each line
        String[] parts = line.split(",");
        String title = parts[0];
        int itemNumber = Integer.parseInt(parts[1]);
        int numberofUnits = Integer.parseInt(parts[2]);
        double unitPrice = Double.parseDouble(parts[3]);
        String genre = parts[4];

        CDinventoryItem item = new CDinventoryItem(title, itemNumber, numberofUnits, 
unitPrice, genre);

        //add item to array
        inven[i] = item;
        i++;
    }
} catch (IOException e) {
    e.printStackTrace();
} finally {
    if (in != null) {
        try {
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
}}

我从我的CDinventory类中调用它

        readFile invenItem = new readFile();
        list = new JList(invenItem.inven);

但它给了我一个:线程“main”中的异常java.lang.ArrayIndexOutOfBoundsException:1 在线:     readFile invenItem = new readFile();

似乎不喜欢我这样传递数组。

1 个答案:

答案 0 :(得分:0)

您需要逐行读取文件。拆分,上的每一行并创建一个CDInventoryItem。将项添加到您的数组。

另请注意,此方法不应位于CDInventoryItem的构造函数中。您的CDInventoryItem类甚至不应该有CDInventoryItem个数组。所有这些都应该在一个单独的课程中完成。

以下是一些可以帮助您入门的代码:

public void readFile() {

    BufferedReader in = null;

    try {
        in = new BufferedReader(new FileReader("inventory.dat"));
        String line = null;
        int i = 0;
        while ((line = in.readLine()) != null) {

            // process each line
            String[] parts = line.split(",");
            String title = parts[0];
            int itemNumber = Integer.parseInt(parts[1]);
            int numberOfUnits = Integer.parseInt(parts[2]);
            double unitPrice = Double.parseDouble(parts[3]);
            String genre = parts[4];

            CDinventoryItem item = new CDinventoryItem(title, itemNumber, unitPrice, genre);

            //add item to array
            inven[i] = item;
            i++;
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}