Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/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_File_Io - Fatal编程技术网

Java 文件输出一直被覆盖?

Java 文件输出一直被覆盖?,java,file,io,Java,File,Io,我试图让一个循环获得用户输入的信息3次,并保存到一个文件。为什么文件会不断被覆盖?我最初是从saveInfo()函数中实例化File类,但是我认为在构造函数中移动和处理该类会有所帮助,但事实并非如此 注意:这个类是从一个主类实例化的,然后调用go() package informationcollection; import java.util.Scanner; import java.util.Formatter; import java.io.File; import java.io.Fi

我试图让一个循环获得用户输入的信息3次,并保存到一个文件。为什么文件会不断被覆盖?我最初是从saveInfo()函数中实例化File类,但是我认为在构造函数中移动和处理该类会有所帮助,但事实并非如此

注意:这个类是从一个主类实例化的,然后调用go()

package informationcollection;

import java.util.Scanner;
import java.util.Formatter;
import java.io.File;
import java.io.FileNotFoundException;
import java.lang.Integer;

public class Getter {

    private String name;
    private int age;
    private File fp;


    public Getter () {
        name = "";
        fp = new File("programOutput.txt");
        System.out.println("The Getter class has been instanstiated!");
    }

    public void go() {
        getInfo();
        System.out.println("The information has been saved to a file!");
    }

    public void getInfo() {
        Scanner keyboard = new Scanner(System.in);
        int i;

        for(i=0;i<3;i++) {
            System.out.println("What is your name?");
            System.out.printf(">>: ");
            name = keyboard.nextLine();

            System.out.println("How old are you?:");
            System.out.printf(">>: ");
            age = Integer.parseInt(keyboard.nextLine());
            System.out.printf("We will save that your name is %s, and you are %d years old!\n", name, age);
            saveInfo();
        }

    }

    public void saveInfo() {
        try {
            Formatter output = new Formatter(fp);
            output.format("%s is %d years old!\n", name, age);
            output.flush();
        }
        catch (FileNotFoundException ex) {
            System.out.println("File doesn't exist.");
        }



    }



}
包装信息采集;
导入java.util.Scanner;
导入java.util.Formatter;
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.lang.Integer;
公共类Getter{
私有字符串名称;
私人互联网;
私有文件fp;
公共Getter(){
name=“”;
fp=新文件(“programOutput.txt”);
System.out.println(“Getter类已实例化!”);
}
公开作废go(){
getInfo();
System.out.println(“信息已保存到文件中!”);
}
public void getInfo(){
扫描仪键盘=新扫描仪(System.in);
int i;

对于(i=0;i而不是
Formatter
默认构造函数,只需使用构造函数即可

这有助于您添加

或者选择
FileWriter

FileWriter fw = new FileWriter(file.getAbsoluteFile() ,true);
对象。如果第二个参数为true,则字节将写入文件的末尾而不是开头


不要使用
Formatter
默认构造函数,只需使用构造函数即可

这有助于您添加

或者选择
FileWriter

FileWriter fw = new FileWriter(file.getAbsoluteFile() ,true);
对象。如果第二个参数为true,则字节将写入文件的末尾而不是开头


您使用的
格式化程序
构造函数的java api说明如下:

文件-用作此格式化程序目标的文件。如果该文件存在,则它将被截断为零大小;否则,将创建一个新文件

因此,每次调用
格式化程序(文件文件)
构造函数时,都会删除文件中的所有内容

要解决此问题,请将格式化程序定义为类成员:

private String name;
private int age;
private File fp;
private Formatter output;
在构造函数中分配它:

public Getter () {
    try {
        name = "";
        fp = new File("programOutput.txt");
        output = new Formatter(fp);
        System.out.println("The Getter class has been instanstiated!");
    } catch (FileNotFoundException e) {
        System.out.println("File doesn't exist.");
    }
}

然后只需在
saveInfo()
方法中使用它!

您使用的
格式化程序
构造函数的java api说明如下:

文件-用作此格式化程序目标的文件。如果该文件存在,则它将被截断为零大小;否则,将创建一个新文件

因此,每次调用
格式化程序(文件文件)
构造函数时,都会删除文件中的所有内容

要解决此问题,请将格式化程序定义为类成员:

private String name;
private int age;
private File fp;
private Formatter output;
在构造函数中分配它:

public Getter () {
    try {
        name = "";
        fp = new File("programOutput.txt");
        output = new Formatter(fp);
        System.out.println("The Getter class has been instanstiated!");
    } catch (FileNotFoundException e) {
        System.out.println("File doesn't exist.");
    }
}
然后只需在
saveInfo()
方法中使用它!

根据状态(粗体文本我自己):

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

您可以使用类似这样的方法来避免文本被截断:

PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("programOutput.txt", true)));
根据各州(粗体文本我自己):

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

您可以使用类似这样的方法来避免文本被截断:

PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("programOutput.txt", true)));
检查的JavaDoc。它说明如下:

如果文件存在,那么它将被截断为零大小;否则,将创建一个新文件

由于每次尝试保存信息时都会实例化一个新的
格式化程序
,因此您总是将文件截断为零大小。为了防止这种情况发生,只需为包含所述
格式化程序
的类定义一个新属性,并仅创建一次即可。

检查的JavaDoc。它说明如下:

如果文件存在,那么它将被截断为零大小;否则,将创建一个新文件


由于每次尝试保存信息时都会实例化一个新的
格式化程序
,因此您总是将文件截断为零大小。为了防止这种情况发生,只需为包含所述
格式化程序
的类定义一个新属性,并仅创建一次即可。

hehe;)另一个api爱好者!@fge:你是说
nio
包吗?一切
java.nio.file
,是的;也被称为NIO2,但无论如何,你的
新缓冲写入程序(new FileWriter(…)
可以,事实上应该在2015年被
Files.newBufferedWriter(…)
.hehe;)另一个api爱好者!@fge:你是说
nio
软件包?一切
java.nio.file
,是的;也被称为NIO2,但无论如何,你的
新缓冲写入程序(新文件写入程序(…)
可以,事实上应该在2015年被
文件.newBufferedWriter(…)
取代。