gpt4 book ai didi

java - BufferedReader 似乎只读取文件的最后一行

转载 作者:行者123 更新时间:2023-12-04 22:53:54 26 4
gpt4 key购买 nike

我正在尝试编写一个方法来获取一个多行制表符分隔的文件,并将该文件的内容作为字符串数组的数组列表返回(每一行都是一个字符串[],每个这样的字符串[]都是一个元素一个数组列表)。我的问题是,我无法判断输出是否正确。我打印了每个 arraylist 元素和 String[] 元素,因为它们被保存到 arraylist,这些打印看起来是正确的。但是在返回 arraylist 并在其中打印 String[] 之后,它们似乎只有文件最后一行的内容。我怀疑它可能与我不知道的 FileReader 或 BufferedReader 有关。 Anyhoo,这是代码:

public class DataParsingTest {

static File AAPLDailyFile = new File("./textFilesForMethodTests/dataParsingPractice2.tsv");

public static void main(String[] args) throws FileNotFoundException, IOException {
ArrayList<String[]> stringArrayList = fileToStringArray(AAPLDailyFile);
System.out.println("stringArray.size() = " + stringArrayList.size());
System.out.println(stringArrayList.get(0)[0]);

for (int i = 0; i < stringArrayList.size(); i++) {
for (int j = 0; j < stringArrayList.get(i).length; j++) {
System.out.println("index of arraylist is " + i + " and element at index " + j + " of that array is " + stringArrayList.get(i)[j]);
}
}
}

public static ArrayList<String[]> fileToStringArray(File file) throws FileNotFoundException, IOException {
ArrayList<String[]> arrayListOfStringArrays = new ArrayList<String[]>();
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
int nextChar = 0;
int noOfTokens = 1; // because the first token doesn't have a tab or newline before it
int startIndex = 0, endIndex = 0, tokenIndex = 0;
String toRead = "";
toRead = bufferedReader.readLine();
for (int i = 0; i < toRead.length(); i++) {
if (toRead.charAt(i) == '\t') {
noOfTokens++;
}
}
System.out.println("noOfTokens = " + noOfTokens);
bufferedReader.close();
fileReader.close();
String[] productString = new String[noOfTokens];
startIndex = 0;
endIndex = 0;
tokenIndex = 0;
FileReader fileReader2 = new FileReader(file);
BufferedReader bufferedReader2 = new BufferedReader(fileReader2);

tokenIndex = 0;
int count = 1;
while ((toRead = bufferedReader2.readLine()) != null) {
System.out.println("toRead = " + toRead);
startIndex = -1; // [L - so that the first time an array element is assigned, it's upped to 0]
endIndex = 0;
tokenIndex = 0;
while (true) {
endIndex = toRead.indexOf("\t", startIndex + 1);
if (endIndex == -1) {
productString[tokenIndex] = toRead.substring(startIndex + 1);
System.out.println("tokenIndex = " + tokenIndex);
System.out.println("productString[" + tokenIndex + "] = " + productString[tokenIndex]);
tokenIndex++;
count++;
arrayListOfStringArrays.add(productString);
System.out.println("just added an array to the list. the first element is " + productString[0]);
break;
}
productString[tokenIndex] = toRead.substring(startIndex + 1, endIndex);
System.out.println("tokenIndex = " + tokenIndex);
System.out.println("productString[" + tokenIndex + "] = " + productString[tokenIndex]);
startIndex = endIndex;
tokenIndex++;
count++;
}
}
fileReader2.close();
bufferedReader2.close();
return arrayListOfStringArrays;
}
}

输入文件是:

1   2
3 4
5 6

输出是:

noOfTokens = 2
toRead = 1 2
tokenIndex = 0
productString[0] = 1
tokenIndex = 1
productString[1] = 2
just added an array to the list. the first element is 1
toRead = 3 4
tokenIndex = 0
productString[0] = 3
tokenIndex = 1
productString[1] = 4
just added an array to the list. the first element is 3
toRead = 5 6
tokenIndex = 0
productString[0] = 5
tokenIndex = 1
productString[1] = 6
just added an array to the list. the first element is 5
stringArray.size() = 3
5 // from here on up, it looks like the method works correctly
index of arraylist is 0 and element at index 0 of that array is 5
index of arraylist is 0 and element at index 1 of that array is 6
index of arraylist is 1 and element at index 0 of that array is 5
index of arraylist is 1 and element at index 1 of that array is 6
index of arraylist is 2 and element at index 0 of that array is 5
index of arraylist is 2 and element at index 1 of that array is 6 //these 6 lines only reflect the last line of the input file.

非常感谢!

最佳答案

您只是创建了一个单个 字符串数组,并将其重复用于所有行。所以您的 ArrayList 只包含对同一对象的多个引用。您需要了解,当您调用 arrayListOfStringArrays.add(productString); 时,并不是将数组的副本添加到 ArrayList - 它只是添加一个 reference。 (productString 的值只是一个引用,而不是数组本身。)

只需移动这个:

String[] productString = new String[noOfTokens];

进入 while 循环,一切都应该顺利。 (无论如何,在这方面。您还应该在 finally block 中关闭文件句柄。)

关于java - BufferedReader 似乎只读取文件的最后一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3480270/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com