簡體   English   中英

字符串中的換行符不會傳遞到文件

[英]Newlines in string not passing out to file

我看到過類似的問題

字符串中的換行符未寫入文件

但這並沒有幫助我,這是我的代碼

 try { 
           String StringBudy =String.format(mailBuddy,System.getProperty("line.separator")); 

            FileOutputStream fOut = openFileOutput("My_Sms.txt",
                                                    MODE_WORLD_READABLE);
            OutputStreamWriter osw = new OutputStreamWriter(fOut); 

            osw.write(StringBudy);
            osw.flush();
            osw.close();
        } catch (IOException ioe) {
            ioe.printStackTrace();
    }

任何想法為什么字符串中的換行符不寫出到文件?

String.format()只會用換行符替換前%s

例如:

String.format("this%sis%sa%stest", System.getProperty("line.separator"))

將產生

this
isatest

一種解決方案是用換行符替換%s所有出現次數:

String str = mailBuddy.replace("%s", System.getProperty("line.separator"));

創建使用\\ n換行的字符串時,請按照Windows的要求使用\\ r \\ n。

暫無
暫無

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

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