Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 这段代码保持格式化文件,而不是写入文件。我怎么修理它?_Java_Arrays_Java Io_Formatter - Fatal编程技术网

Java 这段代码保持格式化文件,而不是写入文件。我怎么修理它?

Java 这段代码保持格式化文件,而不是写入文件。我怎么修理它?,java,arrays,java-io,formatter,Java,Arrays,Java Io,Formatter,我在这里声明了一个数组和变量。productList数组、名称、价格和编号 import java.io.*; 导入java.util.*; 公共类读写{ 公共字符串名称; 公开双价; 公共整数; 公共读写[]产品列表=新读写[3]; 公开读写(){ } 公共读写(字符串名称、双倍价格、整数){ this.name=名称; 这个价格=价格; 这个数字=数字; } 公共内容{ int i=0; 试一试{ FileReader fl=新的FileReader(“Product List.txt”)

我在这里声明了一个数组和变量。productList数组、名称、价格和编号

import java.io.*;
导入java.util.*;
公共类读写{
公共字符串名称;
公开双价;
公共整数;
公共读写[]产品列表=新读写[3];
公开读写(){
}
公共读写(字符串名称、双倍价格、整数){
this.name=名称;
这个价格=价格;
这个数字=数字;
}
公共内容{
int i=0;
试一试{
FileReader fl=新的FileReader(“Product List.txt”);
扫描仪scn=新扫描仪(fl);
while(scn.hasNext()){
字符串productName=scn.next();
double productPrice=scn.nextDouble();
int productAmount=scn.nextInt();
System.out.println(productName+“是“+productPrice+”pula。还有“+productAmount+”项留在柄中。”);
productList[i]=新读写(产品名称、产品价格、产品金额);
i=i+1;
}
scn.close();
}捕获(IOException){
exc.printStackTrace();
}捕获(异常exc){
exc.printStackTrace();
}
}
公共无效写入内容(){
试一试{
//FileOutputStream格式化程序=新的FileOutputStream(“Product List.txt”,true);
格式化程序编写器=新格式化程序(新文件输出流(“Product List.txt”,true));
对于(int i=0;i}
您正在使用的构造函数会截断输入文件,正如Javadoc中明确指出的:

fileName用作此格式化程序目标的文件名。如果该文件存在,则它将被截断为零大小;否则,将创建一个新文件。输出将写入文件并进行缓冲

您可以使用其他构造函数,例如
格式化程序(OutputStream os)
来避免这种截断

Formatter writer = new Formatter(new FileOutputStream("Product List.txt",true));
关于
NullPointerException
,请参见您的评论:

 public String name; 
name
未初始化,因此默认为
null
,这会导致
NullPointerException

另外,我不知道什么是
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");

  }
for(int i=0;i
具有不正确的参数。因此它给出了NullPointerException。 阅读Java文档,了解应该传递的正确参数

这样做:

public String str=null; //declare class variable str.
Formatter writer = new Formatter(new FileOutputStream("Product List.txt", true));

  for (int i = 0; i < productList.length; ++i) {

    writer.format(str);
  }
在printContents()方法中,为字符串指定以下内容:

str = "\n"+productName +" "+ (productPrice + 100.0)+" "+ (productAmount -   1)+"\n";
最后是方法,更改传递给writer.format()方法的参数,如下所示:

public String str=null; //declare class variable str.
Formatter writer = new Formatter(new FileOutputStream("Product List.txt", true));

  for (int i = 0; i < productList.length; ++i) {

    writer.format(str);
  }
Formatter writer=new格式化程序(new FileOutputStream(“Product List.txt”,true));
对于(int i=0;i
这对我有用。
您想要的值被正确地附加到文件“ProductList.txt”中。

我添加了完整的代码。。。现在呢?NullPointerException的原因是什么?