文件处理 - 重新从头开始阅读
File Handling - Reading from the beginning again
我需要创建一个对象数组来保存文件中的记录。我不知道数组的大小。为此,我必须先找到文件中的行数。使用行数,可以确定数组的大小。现在我需要从头开始再次读取文件,以将文件中的记录存储在数组对象中。这就是我挣扎的地方。我不知道如何实现以便从头开始再次读取文件。请指教
/**
* 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 class 已被使用。我可以将此引用用于 FileInputStream 吗?
您当前的方法非常有限,因为您使用的数组根据定义必须定义为具有固定大小。更好的方法是使用 ArrayList
个 GameRecord
个对象。使用这种方法,您可以简单地单次遍历文件并根据需要向 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讨论这个。
如果你使用了org.apache.commons.io.IOUtils,你可以使用readLines方法。只要您的文件不包含 "Too Many" 条记录。此方法returns 一个易于转换为字符串数组的列表
我需要创建一个对象数组来保存文件中的记录。我不知道数组的大小。为此,我必须先找到文件中的行数。使用行数,可以确定数组的大小。现在我需要从头开始再次读取文件,以将文件中的记录存储在数组对象中。这就是我挣扎的地方。我不知道如何实现以便从头开始再次读取文件。请指教
/**
* 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 class 已被使用。我可以将此引用用于 FileInputStream 吗?
您当前的方法非常有限,因为您使用的数组根据定义必须定义为具有固定大小。更好的方法是使用 ArrayList
个 GameRecord
个对象。使用这种方法,您可以简单地单次遍历文件并根据需要向 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讨论这个。
如果你使用了org.apache.commons.io.IOUtils,你可以使用readLines方法。只要您的文件不包含 "Too Many" 条记录。此方法returns 一个易于转换为字符串数组的列表