为什么我不能将数据存储在我的.txt文件中?

时间:2013-11-05 21:20:55

标签: java file class oop instance

这是我正在研究的锻炼应用程序。我们的想法是创建一个帐户(文本文件)并存储您在其中输入的数据。这是我到目前为止所拥有的。

public static void main(String[] args) {

    boolean start = true;

    while (start == true) {

        CreateNewMember();
        start = DecideToAddOrQuit();
    }
}


public static void CreateNewMember() {

    Scanner keyboard = new Scanner(System.in);
    out.println("Enter a username: ");
    String input = keyboard.nextLine();
    createMember member = new createMember(input);
    member.setMembership();
    member.setInfo();
}

public static boolean DecideToAddOrQuit() {
    Scanner keyboard = new Scanner(System.in);
    out.println("\nPress 1 if you want to continue adding data.");
    out.println("Press any other key if you want to leave.");

    String decision = keyboard.nextLine();
    if (decision.equals("1")) {
        out.println("");
        return true;
    } else {
        out.println("Goodbye!");
        return false;
    }
}

这是负责向文件添加数据的类:

public class createMember {

public String name;
private String fullName;
private String age;
private String experience;

private Formatter x;

public createMember(String name) {
    this.name = name;
}

public void setMembership() {
    try {           
        x = new Formatter(name);   
        out.println("File with name \"" + name + "\" has been created!");
    } catch (Exception e) {
        out.println("Could not create username.");
    }
}

public void setInfo () {
    Scanner keyboard = new Scanner(System.in);
    String fullNameIn, ageIn, experienceIn;

    out.println("Enter your Full Name");
    fullNameIn = keyboard.nextLine();
    fullName = fullNameIn;
    out.println("Enter your Age");
    ageIn = keyboard.nextLine();
    age = ageIn;
    out.println("Enter your lifting experience\n");
    experienceIn = keyboard.nextLine();
    experience = experienceIn;
    x.format("%s\t%s\t%s", fullName, age, experience );
 }

}

我输入的值(fullName,age,experience)不存储在用户名文件中。我如何解决这个问题以及它为什么会发生?

2 个答案:

答案 0 :(得分:4)

如果您希望在程序运行时写入输出,则可能看不到它,因为Formatter buffers the output

完成文件后必须close()文件(x.close()),以便写入任何剩余的缓冲数据(这不会自动发生,即使在程序退出时也是如此)。此外,如果您希望输出实际上立即写入flush(),只要您写一行(x.flush())。

答案 1 :(得分:0)

这个问题并不是最清楚的,但就我所知,你根本没有告诉任何事实要将数据实际写入文件。您应该使用PrintWriter。这会将您想要的数据加载到文件中。另外,在您的代码中,您的文件永远不会创建。

import java.io.PrintWriter;

public class createMember {

public String name;
private String fullName;
private String age;
private String experience;

// private Formatter x; not quite sure what this does. There seems to be no need for it.

PrintWriter write;

File f;    

public createMember(String name) {
    this.name = name;
}

public void setMembership() throws Exception {
    try {           
        // x = new Formatter(name);
        f = new File (name);
        f.createNewFile(); // This throws a HeadlessException (i believe, as it might be a FileNotFoundException instead)
        out.println("File with name \"" + name + "\" has been created!");
    } catch (Exception e) {
        out.println("Could not create username.");
    }
}

public void setInfo () throws Exception {
    Scanner keyboard = new Scanner(System.in);
    String fullNameIn, ageIn, experienceIn;
    write = new PrintWriter(f.toString()); // This throws a FileNotFoundException
    out.println("Enter your Full Name");
    fullNameIn = keyboard.nextLine();
    fullName = fullNameIn;
    write.println (fullName);
    out.println("Enter your Age");
    ageIn = keyboard.nextLine();
    age = ageIn;
    write.println(age);
    out.println("Enter your lifting experience\n");
    experienceIn = keyboard.nextLine();
    experience = experienceIn;
    write.println(experience);
    //x.format("%s\t%s\t%s", fullName, age, experience );
    write.close(); // Be sure to close it, or it gives a warning and the file is never written.
 }

}

我希望这是你正在寻找的和快乐的编码!