此代码保持格式化文件而不是写入文件。我如何解决它?
This code keeps formatting the file instead of writing to it. How do i fix it?
我在这里声明了一个数组和变量。 productList 数组、名称、价格和数量
import java.io.*;
import java.util.*;
public class ReadingAndWritting {
public String name;
public double price;
public int number;
public ReadingAndWritting[] productList = new ReadingAndWritting[3];
public ReadingAndWritting() {
}
public ReadingAndWritting(String name, double price, int number) {
this.name = name;
this.price = price;
this.number = number;
}
public void printContents() {
int i = 0;
try {
FileReader fl = new FileReader("Product List.txt");
Scanner scn = new Scanner(fl);
while (scn.hasNext()) {
String productName = scn.next();
double productPrice = scn.nextDouble();
int productAmount = scn.nextInt();
System.out.println(productName + " is " + productPrice + " pula. There are " + productAmount + " items left in stalk.");
productList[i] = new ReadingAndWritting(productName, productPrice, productAmount);
i = i + 1;
}
scn.close();
} catch (IOException exc) {
exc.printStackTrace();
} catch (Exception exc) {
exc.printStackTrace();
}
}
public void writeContents() {
try {
//FileOutputStream formater = new FileOutputStream("Product List.txt",true);
Formatter writer = new Formatter(new FileOutputStream("Product List.txt", true));
for (int i = 0; i < productList.length; ++i) {
writer.format(name, (price + 100.0), (number - 1), "\n");
}
writer.close();
} catch (IOException exc) {
exc.printStackTrace();
}
}
public static void main(String[] args) {
ReadingAndWritting obj = new ReadingAndWritting();
System.out.println("_____THIS IS THE BEGINNING OF THE PRODUCT LIST_____");
obj.printContents();
System.out.println("_____THIS IS THE END OF THE PRODUCT LIST_____");
System.out.println("_____THIS IS THE BEGINNING OF THE PRODUCT LIST_____");
obj.writeContents();
obj.printContents();
System.out.println("_____THIS IS THE END OF THE PRODUCT LIST_____");
}
}
每次我 运行 代码都会格式化现有文件,然后报告 NullPointerException。我完全不知道如何解决这个问题,我们的讲师也从未涉及过。请
the productList[] array has name,price and number value contained in each identifier. I want to wipe the contents of the existing file and then write with the new values to update contents from the array
您正在使用的构造函数截断了输入文件,Javadoc 中明确指出:
fileName The name of the file to use as the destination of this formatter. If the file exists then it will be truncated to zero size; otherwise, a new file will be created. The output will be written to the file and is buffered.
您可以使用其他构造函数,例如 Formatter(OutputStream os)
来避免截断。
Formatter writer = new Formatter(new FileOutputStream("Product List.txt",true));
至于NullPointerException
,看到你的评论:
public String name;
name
没有初始化,所以默认是null
,这就导致了NullPointerException
.
P.S。我不知道 productList
是什么,根据它的名称和你正在迭代它的事实,也许你应该从该列表的元素中获取 price
和 name
.
编辑:
根据您的其余代码,您应该从 productList
数组中获取值:
for (int i = 0; i < productList.length; ++i) {
writer.format(productList[i].name, (productList[i].price + 100.0), (productList[i].number - 1), "\n");
}
writer.format(name, (price + 100.0), (number - 1), "\n");
参数不正确。由于它给出了 NullPointerException。
阅读 Java 文档以了解应该传递的正确参数。
做这样的事情:
public String str=null; //declare class variable str.
在 printContents() 方法中,将以下内容分配给字符串:
str = "\n"+productName +" "+ (productPrice + 100.0)+" "+ (productAmount - 1)+"\n";
最后是方法,改变传递给 writer.format() 方法的参数,如下所示:
Formatter writer = new Formatter(new FileOutputStream("Product List.txt", true));
for (int i = 0; i < productList.length; ++i) {
writer.format(str);
}
这对我有用。
您想要的值已正确附加到文件 "ProductList.txt".
我在这里声明了一个数组和变量。 productList 数组、名称、价格和数量
import java.io.*;
import java.util.*;
public class ReadingAndWritting {
public String name;
public double price;
public int number;
public ReadingAndWritting[] productList = new ReadingAndWritting[3];
public ReadingAndWritting() {
}
public ReadingAndWritting(String name, double price, int number) {
this.name = name;
this.price = price;
this.number = number;
}
public void printContents() {
int i = 0;
try {
FileReader fl = new FileReader("Product List.txt");
Scanner scn = new Scanner(fl);
while (scn.hasNext()) {
String productName = scn.next();
double productPrice = scn.nextDouble();
int productAmount = scn.nextInt();
System.out.println(productName + " is " + productPrice + " pula. There are " + productAmount + " items left in stalk.");
productList[i] = new ReadingAndWritting(productName, productPrice, productAmount);
i = i + 1;
}
scn.close();
} catch (IOException exc) {
exc.printStackTrace();
} catch (Exception exc) {
exc.printStackTrace();
}
}
public void writeContents() {
try {
//FileOutputStream formater = new FileOutputStream("Product List.txt",true);
Formatter writer = new Formatter(new FileOutputStream("Product List.txt", true));
for (int i = 0; i < productList.length; ++i) {
writer.format(name, (price + 100.0), (number - 1), "\n");
}
writer.close();
} catch (IOException exc) {
exc.printStackTrace();
}
}
public static void main(String[] args) {
ReadingAndWritting obj = new ReadingAndWritting();
System.out.println("_____THIS IS THE BEGINNING OF THE PRODUCT LIST_____");
obj.printContents();
System.out.println("_____THIS IS THE END OF THE PRODUCT LIST_____");
System.out.println("_____THIS IS THE BEGINNING OF THE PRODUCT LIST_____");
obj.writeContents();
obj.printContents();
System.out.println("_____THIS IS THE END OF THE PRODUCT LIST_____");
}
}
每次我 运行 代码都会格式化现有文件,然后报告 NullPointerException。我完全不知道如何解决这个问题,我们的讲师也从未涉及过。请
the productList[] array has name,price and number value contained in each identifier. I want to wipe the contents of the existing file and then write with the new values to update contents from the array
您正在使用的构造函数截断了输入文件,Javadoc 中明确指出:
fileName The name of the file to use as the destination of this formatter. If the file exists then it will be truncated to zero size; otherwise, a new file will be created. The output will be written to the file and is buffered.
您可以使用其他构造函数,例如 Formatter(OutputStream os)
来避免截断。
Formatter writer = new Formatter(new FileOutputStream("Product List.txt",true));
至于NullPointerException
,看到你的评论:
public String name;
name
没有初始化,所以默认是null
,这就导致了NullPointerException
.
P.S。我不知道 productList
是什么,根据它的名称和你正在迭代它的事实,也许你应该从该列表的元素中获取 price
和 name
.
编辑:
根据您的其余代码,您应该从 productList
数组中获取值:
for (int i = 0; i < productList.length; ++i) {
writer.format(productList[i].name, (productList[i].price + 100.0), (productList[i].number - 1), "\n");
}
writer.format(name, (price + 100.0), (number - 1), "\n");
参数不正确。由于它给出了 NullPointerException。 阅读 Java 文档以了解应该传递的正确参数。
做这样的事情:
public String str=null; //declare class variable str.
在 printContents() 方法中,将以下内容分配给字符串:
str = "\n"+productName +" "+ (productPrice + 100.0)+" "+ (productAmount - 1)+"\n";
最后是方法,改变传递给 writer.format() 方法的参数,如下所示:
Formatter writer = new Formatter(new FileOutputStream("Product List.txt", true));
for (int i = 0; i < productList.length; ++i) {
writer.format(str);
}
这对我有用。 您想要的值已正确附加到文件 "ProductList.txt".