簡體   English   中英

我在Java中使用Writer類進行UTF8編碼輸出。 編寫時如何插入新行?

[英]I am using Writer class in Java for UTF8 encoding output. How do I insert new line when writing?

我的代碼片段如下:

        FileOutputStream fos = new FileOutputStream("newFile.txt"); 
        OutputStreamWriter out = new OutputStreamWriter(fos, "UTF-8");
        Writer w = out;

        for(String str : arrayName) 
        {
            w.write("sample");
            //I wish to insert new line here
        }

        w.close();

除了手動寫入空格還有其他方法嗎? iewwrite( “\\ n”);

編輯。 很抱歉引起誤解,如果我要尋找新行或空白。

嘗試這個

PrintWriter bw = new PrintWriter(new File("newFile.txt"), "UTF-8");
...
bw.println("sample");

w.write( “\\ n”);

顯然,這個答案簡而言之。

對於獨立於平台的方式,請使用以下命令:

w.write(System.getProperty("line.separator"));

閱讀有關特殊字符的 Java文檔對於空白(空格字符),只需執行以下操作:

w.write(" ");

暫無
暫無

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

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