[英]Using BufferedReader to read a single line in Java
I have code successfully reading from a CSV. 我已成功从CSV中读取代码。 However when I try to use fileReader
to read a solo line, it makes my code stop working. 但是,当我尝试使用fileReader
读取独奏行时,它会使我的代码停止工作。
Here is my code: 这是我的代码:
try {
String line = "";
fileReader = new BufferedReader(new FileReader(filename));
while ((line = fileReader.readLine()) != null) {
String[] tokens = line.split(DELIMITER);
for (String token : tokens) {
totalData.add(token);
if (!artists.contains(token)) {
artists.add(token);
}
}
for (int l = 0; l <= 999; l++) {
lineData = fileReader.readLine();
lineArray[l] = lineData;
}
}
} finally {
fileReader.close();
}
When I try to read arrayList
sizes and print data I get from the arrayList
s above this code below makes it stop working: 当我尝试读取arrayList
大小和打印数据时,我从上面这个代码上面的arrayList
获得它使它停止工作:
for (int l = 0; l <= 80; l++) {
lineData = fileReader.readLine();
lineArray[l] = lineData;
}
If I comment this for loop, everything is fine. 如果我评论这个循环,一切都很好。 I really need this for loop, how can I edit my code to resolve this issue? 我真的需要这个for循环,如何编辑我的代码来解决这个问题? Also, what is happening? 还有,发生了什么?
for (int l = 0; l <= 80; l++) {
lineData = fileReader.readLine();
lineArray[l] = lineData;
}
This hard code can replace a single line of code: 这个硬代码可以代替一行代码:
lineArray[i++] = line;
I corrected your code and that's what happened: 我纠正了你的代码,这就是发生的事情:
String line = "";
int i = 0;
try (BufferedReader fileReader = new BufferedReader(new FileReader(""))) {
while ((line = fileReader.readLine()) != null) {
lineArray[i++] = line;
String[] tokens = line.split(DELIMITER);
for (String token : tokens) {
totalData.add(token);
if (!artists.contains(token)) {
artists.add(token);
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.