ArrayList不在方法之间持久化

时间:2014-06-21 17:28:18

标签: java jsf ejb javabeans

我正在尝试将csv文件加载到arrayList中,以便稍后将其分解并存储。在我的方法之间,arrayList被重置为null。我对原因感到困惑,并对任何建议表示感谢

package TestInput;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;

public class Bean {

    private String fileContent;
    private String fileContent2;
    private ArrayList<String> fileContentArray;
    private int counter = 0;

    public int getCounter() {
        return counter;
    }

    public void setCounter(int counter) {
        this.counter = counter;
    }

    public ArrayList<String> getFileContentArray() {
        return fileContentArray;
    }

    public void setFileContentArray(ArrayList<String> fileContentArray) {
        this.fileContentArray = fileContentArray;
    }

    public String getFileContent() {
        return fileContent;
    }

    public void setFileContent(String fileContent) {
        this.fileContent = fileContent;
    }

    public String getFileContent2() {
        return fileContent2;
    }

    public void setFileContent2(String fileContent2) {
        this.fileContent2 = fileContent2;
    }

    public void upload() {

        File file = new File("/Users/t_sedgman/Desktop/FinalProject/test_output_data.rtf");
        FileInputStream fis = null;
        BufferedInputStream bis = null;
        DataInputStream dis = null;
        ArrayList<String> tempArray = new ArrayList<>();
        try {
            fis = new FileInputStream(file);
            // Here BufferedInputStream is added for fast reading.
            bis = new BufferedInputStream(fis);
            dis = new DataInputStream(bis);

            // dis.available() returns 0 if the file does not have more lines.
            while (dis.available() != 0) {

                // this statement reads the line from the file and print it to
                // the console.
                tempArray.add(dis.readLine());
            }
            setFileContentArray(tempArray);
            // dispose all the resources after using them.

            fis.close();
            bis.close();
            dis.close();
            fileContent = fileContentArray.get((fileContentArray.size() - 2));

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void next() {
        ArrayList<String> tempArray = getFileContentArray();
        int size = fileContentArray.size();
        if (counter <= size) {
            counter++;
            fileContent2 = tempArray.get(counter);
        } else {
            counter = 0;
        }
    }
}

非常感谢

汤姆

1 个答案:

答案 0 :(得分:0)

您可以尝试使用@ViewScoped/@SessionScoped

标记您的bean