文件处理 - 再次从头开始阅读

时间:2015-05-28 07:54:06

标签: java arrays io

我需要创建一个对象数组来保存文件中的记录。我不知道阵列的大小。为此,我必须先找到文件中的行数。使用行数,可以确定阵列的大小。现在我需要从头开始再次读取文件,以存储数组对象中文件的记录。这是我在努力的地方。我不知道如何实现以便从头开始再次读取文件。请指教。

/**
     * Loads the game records from a text file.
     * A GameRecord array is constructed to store all the game records.
     * The size of the GameRecord array should be the same as the number of non-empty records in the text file.
     * The GameRecord array contains no null/empty entries.
     * 
     * @param reader    The java.io.Reader object that points to the text file to be read.
     * @return  A GameRecord array containing all the game records read from the text file.
     */


    public GameRecord[] loadGameRecord(java.io.Reader reader) {
        // write your code after this line
        String[] parts;
        GameRecord[] gameRecord = null;
        FileReader fileReader = (FileReader) reader;
        java.io.BufferedReader bufferedReader = new java.io.BufferedReader(fileReader);
        try {
            int linenumber = 0;
            String sCurrentLine;
            while ((sCurrentLine = bufferedReader.readLine()) != null){
                System.out.println(sCurrentLine);
                linenumber++;
            }
            gameRecord = new GameRecord[linenumber]; //creating a space for total no Of lines

            //How to read the file from the beginning again, and why I am getting bufferedReader.readLine()) as null
            bufferedReader = new java.io.BufferedReader(fileReader); 
            while ((sCurrentLine = bufferedReader.readLine()) != null){
                System.out.println(sCurrentLine);
                parts = sCurrentLine.split("\t");
                    gameRecord[i] = new GameRecord(parts[0], Integer.parseInt(parts[1]),Integer.parseInt(parts[2]));
            }

        }catch (IOException exe){
            System.err.println("IOException: " + exe.getMessage());
            exe.printStackTrace();
        }finally{
            try {
                if (bufferedReader!=null)
                    bufferedReader.close();
                if (fileReader!=null)
                    fileReader.close();
            }catch(IOException exe) {
                System.err.println("IOException: " + exe.getMessage());
            }           
        }
        return gameRecord; 
    }

注意:我将获取对文件的引用作为参数。已经使用了Reader类。我可以将此引用用于FileInputStream吗?

2 个答案:

答案 0 :(得分:3)

您当前的方法非常有限,因为您使用的数组必须定义为具有固定大小。更好的方法是使用ArrayListGameRecord个对象。使用这种方法,您只需对文件进行一次传递,并根据需要将元素添加到ArrayList

示例代码:

List<GameRecord> grList = new ArrayList<GameRecord>();
bufferedReader = new java.io.BufferedReader(fileReader); 
while ((sCurrentLine = bufferedReader.readLine()) != null){
    parts = sCurrentLine.split("\t");
    GameRecord gameRecord = new GameRecord(parts[0],
                                           Integer.parseInt(parts[1]),
                                           Integer.parseInt(parts[2]));
    grList.add(gameRecord);  // add the GameRecord object to the ArrayList
                             // and let the JVM worry about sizing problems
}

// finally, convert the ArrayList to an array
GameRecord[] grArray = new String[grList.size()];
grArray = grList.toArray(grArray);

如果您必须重置BufferedReader,请查看this SO article,并讨论此问题。

答案 1 :(得分:0)

如果您使用org.apache.commons.io.IOUtils,则可以使用readLines方法。只要您的文件永远不会包含&#34;太多&#34;记录。此方法返回一个易于转换为字符串数组的List