从文件中读取文本行
Reading Lines of Text from a File
我有一个包含 5 个客户(每行 1 个)的文本文件,客户 1、客户 2、客户 3、客户 4 和客户 5。使用以下代码,它可以完美读取 5 行文本;
import java.io.*;
public class CustomerIO {
public void method () {
try (BufferedReader br = new BufferedReader(new FileReader(new File ("Customers.txt")))) {
int numberOfLines = readLines();
String [] text = new String [numberOfLines];
for (int i = 0; i < numberOfLines; i++) {
text[i] = br.readLine();
}
for (int i = 0; i < numberOfLines; i++) {
System.out.println(text[i]);
}
} catch (Exception e) {
e.printStackTrace();
}
private int readLines() {
try (BufferedReader br = new BufferedReader(new FileReader(new File ("Customers.txt")))) {
while ((line = br.readLine()) != null) {
numberOfLines++;
}
} catch (Exception e) {
e.printStackTrace();
}
return numberOfLines;
}
}
然而,当我更改为以下内容时,输出为:Customer 2,Customer 4,null
import java.io.*;
public class CustomerIO {
String line;
int numberOfLines = 0;
public void method () {
try (BufferedReader br = new BufferedReader(new FileReader(new File ("Customers.txt")))) {
while (br.readLine() != null) {
System.out.println(br.readLine());
}
} catch (Exception e) {
e.printStackTrace();
}
}
private int readLines() {
try (BufferedReader br = new BufferedReader(new FileReader(new File ("Customers.txt")))) {
while ((line = br.readLine()) != null) {
numberOfLines++;
}
} catch (Exception e) {
e.printStackTrace();
}
return numberOfLines;
}
}
主要方法包含在以下runner.class文件中
public class Runner {
public static void main(String[] args) {
CustomerIO cus = new CustomerIO ();
cus.method();
}
}
你能帮我理解为什么会这样吗?我想在正确读取时将客户读入数组列表,而不是使用 String [].
谢谢
问题是您为每个循环调用了两次 readline。
这是您的错误代码:
while (br.readLine() != null) {
System.out.println(br.readLine());
可行的解决方案可能是:
String line = br.readline();
while (line != null) {
System.out.println(line);
line = br.readline()
}
以下代码在循环的每次迭代中调用 readLine()
两次 ,这就是为什么您只能看到每隔一行的原因:
while (br.readLine() != null) {
System.out.println(br.readLine());
}
您需要将返回值赋给一个变量,以便检查是否为空。
有很多方法可以做到这一点,所以我将按照推荐的降序顺序展示一些。
// Using for loop (recommended)
// Pro: Keeps the loop logic with the loop
// Limits the scope of the variable
// Smallest amount of code (lines of loop code: 2)
// Con: Unusual construct
// Inline assignment
for (String line; (line = br.readLine()) != null; ) {
System.out.println(line);
}
// Using while loop with inline assignment
// Pro: Common construct
// Keeps the loop logic with the loop
// Small amount of code (lines of loop code: 3)
// Con: Variable scope not limited to the loop
// Inline assignment
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
// Using while loop without inline assignment
// Pro: Common construct
// Con: Loop logic both before and at end of loop
// Variable scope not limited to the loop
// More code (lines of loop code: 4)
// Calling the readLine() method in two places
String line = br.readLine();
while (line != null) {
System.out.println(line);
line = br.readLine();
}
// Using break to exit forever loop
// Pro: Limits the scope of the variable
// Con: Loop logic both before and at end of loop
// Using infinite loop and the break statement
// More code (lines of loop code: 6)
for (;;) { // or: while (true)
String line = br.readLine();
if (line == null) {
break;
}
System.out.println(line);
}
我有一个包含 5 个客户(每行 1 个)的文本文件,客户 1、客户 2、客户 3、客户 4 和客户 5。使用以下代码,它可以完美读取 5 行文本;
import java.io.*;
public class CustomerIO {
public void method () {
try (BufferedReader br = new BufferedReader(new FileReader(new File ("Customers.txt")))) {
int numberOfLines = readLines();
String [] text = new String [numberOfLines];
for (int i = 0; i < numberOfLines; i++) {
text[i] = br.readLine();
}
for (int i = 0; i < numberOfLines; i++) {
System.out.println(text[i]);
}
} catch (Exception e) {
e.printStackTrace();
}
private int readLines() {
try (BufferedReader br = new BufferedReader(new FileReader(new File ("Customers.txt")))) {
while ((line = br.readLine()) != null) {
numberOfLines++;
}
} catch (Exception e) {
e.printStackTrace();
}
return numberOfLines;
}
}
然而,当我更改为以下内容时,输出为:Customer 2,Customer 4,null
import java.io.*;
public class CustomerIO {
String line;
int numberOfLines = 0;
public void method () {
try (BufferedReader br = new BufferedReader(new FileReader(new File ("Customers.txt")))) {
while (br.readLine() != null) {
System.out.println(br.readLine());
}
} catch (Exception e) {
e.printStackTrace();
}
}
private int readLines() {
try (BufferedReader br = new BufferedReader(new FileReader(new File ("Customers.txt")))) {
while ((line = br.readLine()) != null) {
numberOfLines++;
}
} catch (Exception e) {
e.printStackTrace();
}
return numberOfLines;
}
}
主要方法包含在以下runner.class文件中
public class Runner {
public static void main(String[] args) {
CustomerIO cus = new CustomerIO ();
cus.method();
}
}
你能帮我理解为什么会这样吗?我想在正确读取时将客户读入数组列表,而不是使用 String [].
谢谢
问题是您为每个循环调用了两次 readline。
这是您的错误代码:
while (br.readLine() != null) {
System.out.println(br.readLine());
可行的解决方案可能是:
String line = br.readline();
while (line != null) {
System.out.println(line);
line = br.readline()
}
以下代码在循环的每次迭代中调用 readLine()
两次 ,这就是为什么您只能看到每隔一行的原因:
while (br.readLine() != null) {
System.out.println(br.readLine());
}
您需要将返回值赋给一个变量,以便检查是否为空。
有很多方法可以做到这一点,所以我将按照推荐的降序顺序展示一些。
// Using for loop (recommended)
// Pro: Keeps the loop logic with the loop
// Limits the scope of the variable
// Smallest amount of code (lines of loop code: 2)
// Con: Unusual construct
// Inline assignment
for (String line; (line = br.readLine()) != null; ) {
System.out.println(line);
}
// Using while loop with inline assignment
// Pro: Common construct
// Keeps the loop logic with the loop
// Small amount of code (lines of loop code: 3)
// Con: Variable scope not limited to the loop
// Inline assignment
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
// Using while loop without inline assignment
// Pro: Common construct
// Con: Loop logic both before and at end of loop
// Variable scope not limited to the loop
// More code (lines of loop code: 4)
// Calling the readLine() method in two places
String line = br.readLine();
while (line != null) {
System.out.println(line);
line = br.readLine();
}
// Using break to exit forever loop
// Pro: Limits the scope of the variable
// Con: Loop logic both before and at end of loop
// Using infinite loop and the break statement
// More code (lines of loop code: 6)
for (;;) { // or: while (true)
String line = br.readLine();
if (line == null) {
break;
}
System.out.println(line);
}