找到你要的答案

Q:Java: PrintWriter object does not output the text on my file?

Q:java:PrintWriter对象不输出的文本在我的文件吗?

I'm trying to print out the usernames of a certain program into a file but PrintWriter is not printing anything on my file. I've tried everything mentioned on stackOverFlow none of them worked.

Users Class

private File usersListFile;
private PrintWriter usersListPrintWriter;
private Scanner usersListScanner;

Constructor:

Users(){
    try {
        this.usersListFile = new File("D:\\Dairy\\usersList.txt");
        if(usersListFile.exists()){
            this.usersListPrintWriter = new PrintWriter(new BufferedWriter(new FileWriter("D:\\Dairy\\usersList.txt", true)));
            this.usersListScanner = new Scanner("D:\\Dairy\\usersList.txt");
        }
        else
            System.err.println("File does not exist !");
    }
    catch(Exception e){
        System.err.println("Error: Users Class!");                   
    }
}

Method:

public void addToUsersList(String username){
        usersListPrintWriter.print(username);           
    }

Main Method:

public static void main(String[] args) {

    Users usersObject = new Users();
    usersObject.addToUsersList("USERNAME");

    }

我想打印出某程序文件但是PrintWriter不在我的文件打印任何用户名。我已经尽力在StackOverflow都提到过。

用户类

private File usersListFile;
private PrintWriter usersListPrintWriter;
private Scanner usersListScanner;

Constructor:

Users(){
    try {
        this.usersListFile = new File("D:\\Dairy\\usersList.txt");
        if(usersListFile.exists()){
            this.usersListPrintWriter = new PrintWriter(new BufferedWriter(new FileWriter("D:\\Dairy\\usersList.txt", true)));
            this.usersListScanner = new Scanner("D:\\Dairy\\usersList.txt");
        }
        else
            System.err.println("File does not exist !");
    }
    catch(Exception e){
        System.err.println("Error: 用户类!");                   
    }
}

方法:

public void addToUsersList(String username){
        usersListPrintWriter.print(username);           
    }

Main 方法:

public static void main(String[] args) {

    Users usersObject = new Users();
    usersObject.addToUsersList("USERNAME");

    }
answer1: 回答1:

usersListPrintWriter is buffered, so you need to flush the data (as Alexandro mentioned too).

You also likely will need to change the print into a println so newly added users are output on separate lines.

Your Scanner will not work, since you're scanning the given string, not the file content. Use new Scanner(this.usersListFile) instead.

You should also re-use your File object on the previous line: new FileWriter(this.usersListFile, true)

And I would say that having a Writer and a Scanner open on the same file at the same time is a bad idea, if it even works. You should probably just load all the users into memory and close the scanner before opening the writer, unless you have

userslistprintwriter缓冲,所以你需要刷新数据(亚历山德罗提到过)。

你也可能需要更改打印成一个println,新添加的用户在不同的行输出。

您的扫描仪将无法工作,因为您正在扫描给定的字符串,而不是文件内容。使用新的Scanner(这userslistfile代替。)。

你应该利用你的文件对象上:新的FileWriter(this.userslistfile,真的)

我想说,如果有一个作家和一个扫描器同时打开同一个文件,那就是一个坏主意,如果它能起作用的话。在打开作者之前,您可能只需要将所有用户加载到内存中并关闭扫描仪,除非您有

answer2: 回答2:
public void addToUsersList(String username){
    usersListPrintWriter.print(username);           
    usersListPrintWriter.flush();
}

Then, when you don't need anymore your printwriter, call close().

public void addToUsersList(String username){
    usersListPrintWriter.print(username);           
    usersListPrintWriter.flush();
}

然后,当你不再需要你的PrintWriter,叫()。

java  file  printwriter