Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 文件处理-重新从头开始读取_Java_Arrays_Io - Fatal编程技术网

Java 文件处理-重新从头开始读取

Java 文件处理-重新从头开始读取,java,arrays,io,Java,Arrays,Io,我需要创建一个对象数组来保存文件中的记录。我不知道数组的大小。为此,我必须先找到文件中的行数。使用行数可以确定数组的大小。现在,我需要从头开始再次读取该文件,以将该文件中的记录存储在数组对象中。这就是我努力奋斗的地方。我不知道如何实现,以便从头开始再次读取该文件。请告知 /** * Loads the game records from a text file. * A GameRecord array is constructed to store all the game

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

/**
     * 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吗?

您当前的方法非常有限,因为您使用的数组根据定义必须定义为具有固定大小。更好的方法是使用
GameRecord
对象的
ArrayList
。使用这种方法,只需对文件进行一次遍历,并根据需要将元素添加到
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);
List grList=new ArrayList();
bufferedReader=newjava.io.bufferedReader(fileReader);
而((sCurrentLine=bufferedReader.readLine())!=null){
parts=sCurrentLine.split(“\t”);
GameRecord GameRecord=新的GameRecord(部件[0],
整数.parseInt(第[1]部分),
整数.parseInt(部分[2]);
grList.add(gameRecord);//将gameRecord对象添加到ArrayList
//让JVM担心大小问题
}
//最后,将ArrayList转换为数组
GameRecord[]grArray=新字符串[grList.size()];
grArray=grList.toArray(grArray);

如果必须重置BufferedReader,请查看其中的讨论内容。

如果使用org.apache.commons.io.IOUtils,则可以使用readLines方法。只要您的文件不包含“太多”记录。此方法返回一个易于转换为字符串数组的列表

不,我需要根据问题返回一个对象数组。我们可以使用toArray()方法将Arraylist转换为数组。从列表中获取数组后,只需返回该数组。更新我的答案以返回数组。使用数组读取文件不是最好的方法。是的,我只是这样实现的。只是想知道如何使用BufferedReader再次读取文件。如果使用我的解决方案,则无需再次读取。您始终可以关闭基础文件,然后重新打开它。FileInputStream派生自Reader类吗?对文件的引用将在参数中发送。我不能使用第三方API,请编写自己的java代码,将完整的文件读取到ArrayList,然后将ArrayList转换为字符串数组。您只需读取文件一次,就可以得到数组作为结果