流讀取導(dǎo)致StringBuilder.toString()亂碼的問題及解決
流讀取導(dǎo)致StringBuilder.toString()亂碼
亂碼問題
StringBuilder sb = new StringBuilder(); String s = new String(sb.toString().getBytes("ios8859-1"),"UTF-8");
順便回憶一下String, StringBuilder, StringBuffer直接的區(qū)別:
String對象每次追加字符串的時候都是創(chuàng)建了新的對象,消耗時間最長,性能最低,操作少量數(shù)據(jù)的時候可以用它;
StringBuilder和StringBuffer每次追加的都是同一個對象,消耗的時間短,性能優(yōu)良;
StringBuilder比StringBuffer更快一點,因為StringBuffer是線程安全的,支持同步鎖,而StringBuilder是線程不安全的,所以在單線程的時候最好使用StringBuilder,多線程的時候則使用線程安全的StringBuffer。
Java StringBuilder toString()方法與示例
StringBuilder類的toString()方法 (StringBuilder Class toString() method)
toString() method is available in java.lang package.
toString()方法在java.lang包中可用。
toString() method is used to represent string denoted by this object (when we create a new string object so first it is created and instantiated to contain the data[set of characters] denoted by this object currently).
toString()方法用于表示此對象表示的字符串(當我們創(chuàng)建一個新的字符串對象時,首先創(chuàng)建并實例化它包含當前由該對象表示的data [字符集])。
toString() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
toString()方法是一種非靜態(tài)方法,只能通過類對象訪問,如果嘗試使用類名稱訪問該方法,則會收到錯誤消息。
toString() method does not throw an exception at the time of string representation.
toString()方法在字符串表示時不會引發(fā)異常。
Syntax:
句法:
public String toString();
Parameter(s):
參數(shù):
- It does not accept any parameter.
- 它不接受任何參數(shù)。
Return value:
返回值:
- The return type of this method is String, it returns the string denotation of this set of characters represented by this object.
- 此方法的返回類型為String ,它返回此對象表示的這組字符的字符串表示形式。
Example:
例:
// Java program to demonstrate the example? // of String toString() method of StringBuilder? ? public class ToString { ? ? public static void main(String[] args) { ? ? ? ? ? // Creating an StringBuilder object ? ? ? ? StringBuilder st_b = new StringBuilder("Java "); ? ? ? ? ? // By using append() method is to append the given string to? ? ? ? ? // st_b object ? ? ? ? ? st_b.append("World"); ? ? ? ? ? // By using toString() method is to represent st_b ? ? ? ? // object to String ? ? ? ? System.out.println("st_b.toString() = " + st_b.toString()); ? ? } }
Output
輸出量
st_b.toString() = Java World
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java8中LocalDateTime與時間戳timestamp的互相轉(zhuǎn)換
這篇文章主要給大家介紹了關(guān)于Java8中LocalDateTime與時間戳timestamp的互相轉(zhuǎn)換,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-03-03SpringBoot整合Redis哨兵模式的實現(xiàn)示例
Redis哨兵模式是Redis高可用方案的一種實現(xiàn)方式,通過哨兵來自動實現(xiàn)故障轉(zhuǎn)移,從而保證高可用,本文主要介紹了SpringBoot整合Redis哨兵模式的實現(xiàn)示例,具有一定的參考價值,感興趣的可以了解一下2024-02-02java.text.DecimalFormat類十進制格式化
這篇文章主要為大家詳細介紹了java.text.DecimalFormat類十進制格式化的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-03-03詳解idea文件右鍵創(chuàng)建New沒有Create New Servlet的解決辦法
這篇文章主要介紹了詳解idea文件右鍵創(chuàng)建New沒有Create New Servlet的解決辦法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-12-12