简体   繁体   English

如何在Java中将每一行写到新行?

[英]How to write each line to a new line in Java?

I have written a method that prints out the arrayList into a text file, but for some reason it does not print on a new line each time, it prints everything onto a single line. 我编写了一种方法,可以将arrayList打印到文本文件中,但是由于某种原因,它不会每次都在新行上打印,而是将所有内容都打印到一行上。 below is my code. 下面是我的代码。

private static void writingToFile() {
        try {
            BufferedWriter writeArrayList = new BufferedWriter (new FileWriter("D:\\text.txt"));
            for(StoreCommands s : commandsList){
                writeArrayList.write(s + "\n");
            }
            writeArrayList.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

Also at the moment I have to enter path in manually, I want the user to choose where they want to place the file, how can i do that as well? 另外,现在我必须手动输入路径,我希望用户选择他们想要放置文件的位置,我该怎么办? thanks. 谢谢。

To append on a file, create with : 要附加在文件上,请使用创建:

new FileWriter("D:\\text.txt", true);

FileWriter public FileWriter(String fileName, boolean append) throws IOException Constructs a FileWriter object given a file name with a boolean indicating whether or not to append the data written. FileWriter public FileWriter(String fileName,boolean append)throws IOException使用给定的文件名和布尔值构造一个FileWriter对象,该布尔值指示是否要附加写入的数据。

Parameters fileName - String The system-dependent filename. 参数fileName-String与系统有关的文件名。 append - boolean if true, then data will be written to the end of the file rather than the beginning. append-布尔值(如果为true),则数据将被写入文件的末尾而不是开始。 Throws IOException if the specified file is not found or if some other I/O error occurs. 如果找不到指定的文件或发生其他一些I / O错误,则引发IOException。 More here 这里更多

Other example : 其他例子:

        FileOutputStream fileOutput = new FileOutputStream("myfile.txt", true);
        BufferedOutputStream bufferedOutput = new BufferedOutputStream(
                fileOutput);

        bufferedOutput.write(<your binaries here>);

How about trying something like this 尝试这样的事情怎么样

private static void writingToFile() {
    try {
        PrintWriter out = new PrintWriter (new FileWriter("D:\\text.txt"));
        for(StoreCommands s : commandsList){
            out.println(s);
        }
        out.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

I prefer PrintWriter because I like the println method. 我更喜欢PrintWriter,因为我喜欢println方法。 You can add System. 您可以添加系统。 before all your statements and print instead of writing to file. 在所有语句之前打印并打印而不是写入文件。

Edit, also I'm assuming your calling the String correctly. 编辑,我也假设您正确调用了String。 You might instead need something like 您可能需要像

        for(StoreCommands s : commandsList){
            out.println(s.toString());
        }

Lastly for fun Java8 version 最后好玩的Java8版本

private static void writeToFile(){
    try(PrintWriter out = new PrintWriter(new FileWrite("test.txt"))){
        list.stream().map(StoreCommands::toString).forEach(s -> out.println(s));
        out.close();
    } catch (IOException ex){
        System.err.out(ex);
    }
}

For writing new lines, check out the newLine() method of the BufferedWriter class. 要编写新行, 请查看 BufferedWriternewLine()方法

For reading user input, you can use the basic Scanner class as follows: 为了读取用户输入,可以使用基本的Scanner类,如下所示:

Scanner get = new Scanner(System.in);
String path = get.nextLine();
...

Question #1: 问题1:

Change "\\n" to "\\r\\n" "\\n"更改为"\\r\\n"

Question #2 : 问题2

Follow the answer posted by CB , and give him/her a vote-up for it (if possible)... 遵循CB发布的答案,并对其进行投票(如果可能)...


To answer your question about not having to hard code a file path, try using command line arguments, like this (in your class): 要回答有关不必对文件路径进行硬编码的问题,请尝试使用命令行参数,如下所示(在您的类中):

public static void main(String[] args){
...
writingToFile(args[0]);

Then when you run the java app you would just specify the File name on the command line like this: 然后,当您运行Java应用程序时,只需在命令行上指定文件名,如下所示:
java <classname>.java D:/text.txt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM