簡體   English   中英

編寫新行記事本

[英]Writing new line notepad

在記事本中打印每個元素后,只需嘗試移至新行。 一些搜索產生了\\ r \\ n的用法,但這似乎也不起作用。

for(String element : misspelledWords)
                {
                    writer.write(element + "\n");
                }

嘗試這個

BufferedWriter writer = new BufferedWriter(new FileWriter("result.txt"));       
    for (String element : misspelledWords) {
        writer.write(element);
        writer.newLine();
    }

在大多數操作系統上都可以在末尾添加行分隔符(例如“ \\ n”),但為了安全起見,應使用System.getProperty(“ line.separator”)

當您要創建FileWrite類的對象時,以這種FileWriter(String fileName, boolean append)追加模式打開文件

在構造函數中。

 File file = new File("C:\\Users\\Izak\\Documents\\NetBeansProjects\\addNewLinetoTxtFile\\src\\addnewlinetotxtfile\\a.txt");
    try (Writer newLine = new BufferedWriter(new FileWriter(file, true));) {
       newLine.write("New Line!");
       newLine.write(System.getProperty( "line.separator" ));
    } catch (IOException e) {

    }

注意:

line.separator ”是操作系統用來分隔文本文件中的行的序列

來源: http : //docs.oracle.com/javase/tutorial/essential/environment/sysprop.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM