如何在 Java 中使用 FileWriter 写入新行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46683251/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 09:19:45  来源:igfitidea点击:

How do I write to a new line using FileWriter in Java?

javafilewriter

提问by kr4m

I've been trying to make a simple bank account in Java and want to save the inputted users' name into a .txt doc. Only problem is that the name is replaced on the first line of the text doc each time I run the code.

我一直在尝试用 Java 创建一个简单的银行帐户,并希望将输入的用户名保存到 .txt 文档中。唯一的问题是,每次运行代码时,文本文档的第一行的名称都会被替换。

package bank.account;
import java.util.Scanner;
import java.io.*;

public class ATM 
{
    public static void main(String[] args){


    Scanner sc = new Scanner(System.in);
    BankAccount userAccount = new BankAccount();
    System.out.println("Please enter your name in order to make a new account:");


    String fileName = "name.txt";
    try {
        FileWriter fileWriter =
                new FileWriter(fileName);
        BufferedWriter bufferedWriter = 
                new BufferedWriter(fileWriter);

        String name = sc.nextLine();
        userAccount.setaccName(name);
                bufferedWriter.write(userAccount.getaccName());
                bufferedWriter.close();

    }
    catch(IOException ex) {
            System.out.println(
                "Error writing to file '"
                + fileName + "'");

        }

    System.out.println("Please enter the amount you would like to deposit");
    double money = sc.nextDouble();
    userAccount.deposit(money);
    System.out.println(userAccount.getaccBalance());
    System.out.println(userAccount.getaccName()+ " your balance is " + userAccount.getaccBalance());

    }

}

采纳答案by Thomas

You're overwriting the file contents due to the use of new FileWriter(fileName);(read the JavaDoc on that class/constructor). Use new FileWriter(fileName, true);to appendto the file instead.

由于使用new FileWriter(fileName);(阅读该类/构造函数上的 JavaDoc),您正在覆盖文件内容。使用new FileWriter(fileName, true);附加到文件来代替。

Also note that you'd need to append a newline character ("\n") before the name if the file is not empty otherwise you'll get all the names in one line.

另请注意,"\n"如果文件不为空,则您需要在名称前附加一个换行符 ( ),否则您将在一行中获得所有名称。

回答by LenglBoy

This Code will open or create a file and append the new text into a new line.

此代码将打开或创建一个文件并将新文本附加到新行中。

PrintStream fileStream = new PrintStream(new File("a.txt"));
fileStream.println(userAccount.getaccName());

Also you can create the FileWriterwith the param "append = true" and then the outcome will just be appended into a new line.

您也可以FileWriter使用参数“ append = true”创建,然后结果将被附加到一个新行中。

// FileWriter(File file, boolean append)
FileWriter fileWriter =
                new FileWriter(filePathName, shouldAppend);