Java FileWriter和PrintWriter赢得';t输出到文件

Java FileWriter和PrintWriter赢得';t输出到文件,java,flush,filewriter,printwriter,println,Java,Flush,Filewriter,Printwriter,Println,我有这个密码 public User createnewproflie() throws IOException { FileWriter fwriter = new FileWriter("users.txt",true); //creates new obj that permits to append text to existing file PrintWriter userfile = new PrintWriter(fwriter); //creates new ob

我有这个密码

public User createnewproflie() throws IOException
{
    FileWriter fwriter = new FileWriter("users.txt",true); //creates new obj that permits to append text to existing file
    PrintWriter userfile = new PrintWriter(fwriter); //creates new obj that prints appending to file as the arg of the obj is a pointer(?) to the obj that permits to append
    String filename= "users.txt";
    Scanner userFile = new Scanner(filename); //creates new obj that reads from file
    User usr=new User(); //creates new user istance
    String usrname = JOptionPane.showInputDialog("Please enter user name: "); //acquires usrname

    userfile.println("USER: "+usrname+"\nHIGHSCORE: 0\nLASTPLAY: 0");     //writes usrname in file
    userfile.flush();
    usr.setName(usrname); //gives usr the selected usname

    return usr;
}
而且它不会在文件上输出。。。有人能帮忙吗?
我知道flush将输出所有缓冲文本,但由于某种奇怪的原因,它似乎不起作用…

您可以将
字符串
文件编写器
一起使用,但a生成从指定字符串扫描的值(而不是从
文件
)。将
文件
传递给
扫描仪
构造函数(最好将相同的
文件
传递给
文件编写器
)。您需要
关闭()
它,然后才能阅读它;也许有一个


最后,我通常更喜欢
filef=newfile(System.getProperty(“user.home”),“users.txt”)
以便文件保存在用户主目录中。

是否引发异常?我能够运行您的代码并正确保存文件。在我的文件中,我得到了输出USER:adsfdsf HIGHSCORE:0 LASTPLAY:0尝试为users.txt提供一个完整的路径,比如“C:/Temp/users.txt”,这样您就可以确切地知道输出文件生成的位置。扫描仪上的观点很好,但OP在他的示例代码中并没有实际使用扫描仪(注意
userFile
userFile
),这让我相信是其他原因导致了这个问题。
File f = new File("users.txt");
try (FileWriter fwriter = new FileWriter(f,true);
    PrintWriter userfile = new PrintWriter(fwriter);) {
    // ... Write stuff to userfile
} catch (Exception e) {
    e.printStackTrace();
}
Scanner userFile = new Scanner(f);