BufferedWriter 不写数组
BufferedWriter not writing the array
我使用缓冲写入器将数组中的内容写入文本文件
try {
File file = new File("Details.txt");
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
for (String line : customer) {
bw.write(line); //line 178
bw.newLine();
}
bw.flush();
bw.close();
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
}
customer[]
数组如下:
String customer[] = new String[10];
customer[1]="Anne";
customer[2]="Michelle";
但是当我尝试写入文件时出现以下错误:
Exception in thread "main" java.lang.NullPointerException
at java.io.Writer.write(Unknown Source)
at HotelFunctions.storeData(CustomerMenu.java:178)
at MainClass.main(MainClass.java:38)
我发现错误是因为customer[0]
为null。我想避免空元素,只写包含字符串内容的元素。有没有办法处理这个错误?
几件事。首先,数组索引从 0 开始,而不是 1。您应该从 customer[0]
.
开始
customer[0] = "Anne";
customer[1] = "Michelle";
其次,您可以检查是否为空。这是一种方式。
for (String line: customer) {
if (line != null) {
bw.write(line);
bw.newLine();
}
}
更好的方法是使用 ArrayList
而不是原始数组。数组是固定大小的集合。如果您想拥有不同数量的元素,ArrayList
会更适合您。您不必防范空元素。如果您添加两个客户,列表将有两个条目,而不是十个。
List<String> customers = new ArrayList<>();
customers.add("Anne");
customers.add("Michelle");
for (String customer: customers) {
bw.write(customer);
bw.newLine();
}
(顺便说一句,我鼓励你使用我上面做的命名方案。常规变量是单数,而数组和列表使用复数:customers
是一个列表,每个元素是一个 customer
.)
默认情况下,对象数组(如字符串)填充有 null
个值。所以
String customer[] = new String[10];
customer[1]="Anne";
customer[2]="Michelle";
您将以类似 [null, "Anne", "Michelle", null, null, ..., null]
的数组结尾。
现在,write
方法的代码如下所示:
public void write(String str) throws IOException {
write(str, 0, str.length());
}
所以当你传递 null
时(字符串数组默认填充 null
s)str.length()
最终为 null.length()
这是无效的,因为 null
没有任何方法或字段,并抛出 NPE。
如果你想跳过 null
元素,你可以简单地用 ==
或 !=
测试它们,比如
for (String line : customer) {
if (line != null){
bw.write(line);
bw.newLine();
}
}
我使用缓冲写入器将数组中的内容写入文本文件
try {
File file = new File("Details.txt");
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
for (String line : customer) {
bw.write(line); //line 178
bw.newLine();
}
bw.flush();
bw.close();
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
}
customer[]
数组如下:
String customer[] = new String[10];
customer[1]="Anne";
customer[2]="Michelle";
但是当我尝试写入文件时出现以下错误:
Exception in thread "main" java.lang.NullPointerException
at java.io.Writer.write(Unknown Source)
at HotelFunctions.storeData(CustomerMenu.java:178)
at MainClass.main(MainClass.java:38)
我发现错误是因为customer[0]
为null。我想避免空元素,只写包含字符串内容的元素。有没有办法处理这个错误?
几件事。首先,数组索引从 0 开始,而不是 1。您应该从 customer[0]
.
customer[0] = "Anne";
customer[1] = "Michelle";
其次,您可以检查是否为空。这是一种方式。
for (String line: customer) {
if (line != null) {
bw.write(line);
bw.newLine();
}
}
更好的方法是使用 ArrayList
而不是原始数组。数组是固定大小的集合。如果您想拥有不同数量的元素,ArrayList
会更适合您。您不必防范空元素。如果您添加两个客户,列表将有两个条目,而不是十个。
List<String> customers = new ArrayList<>();
customers.add("Anne");
customers.add("Michelle");
for (String customer: customers) {
bw.write(customer);
bw.newLine();
}
(顺便说一句,我鼓励你使用我上面做的命名方案。常规变量是单数,而数组和列表使用复数:customers
是一个列表,每个元素是一个 customer
.)
默认情况下,对象数组(如字符串)填充有 null
个值。所以
String customer[] = new String[10];
customer[1]="Anne";
customer[2]="Michelle";
您将以类似 [null, "Anne", "Michelle", null, null, ..., null]
的数组结尾。
现在,write
方法的代码如下所示:
public void write(String str) throws IOException {
write(str, 0, str.length());
}
所以当你传递 null
时(字符串数组默认填充 null
s)str.length()
最终为 null.length()
这是无效的,因为 null
没有任何方法或字段,并抛出 NPE。
如果你想跳过 null
元素,你可以简单地用 ==
或 !=
测试它们,比如
for (String line : customer) {
if (line != null){
bw.write(line);
bw.newLine();
}
}