流讀取導(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對(duì)象每次追加字符串的時(shí)候都是創(chuàng)建了新的對(duì)象,消耗時(shí)間最長,性能最低,操作少量數(shù)據(jù)的時(shí)候可以用它;
StringBuilder和StringBuffer每次追加的都是同一個(gè)對(duì)象,消耗的時(shí)間短,性能優(yōu)良;
StringBuilder比StringBuffer更快一點(diǎn),因?yàn)镾tringBuffer是線程安全的,支持同步鎖,而StringBuilder是線程不安全的,所以在單線程的時(shí)候最好使用StringBuilder,多線程的時(shí)候則使用線程安全的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()方法用于表示此對(duì)象表示的字符串(當(dāng)我們創(chuàng)建一個(gè)新的字符串對(duì)象時(shí),首先創(chuàng)建并實(shí)例化它包含當(dāng)前由該對(duì)象表示的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)方法,只能通過類對(duì)象訪問,如果嘗試使用類名稱訪問該方法,則會(huì)收到錯(cuò)誤消息。
toString() method does not throw an exception at the time of string representation.
toString()方法在字符串表示時(shí)不會(huì)引發(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 ,它返回此對(duì)象表示的這組字符的字符串表示形式。
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
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java8中LocalDateTime與時(shí)間戳timestamp的互相轉(zhuǎn)換
這篇文章主要給大家介紹了關(guān)于Java8中LocalDateTime與時(shí)間戳timestamp的互相轉(zhuǎn)換,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
SpringBoot整合Redis哨兵模式的實(shí)現(xiàn)示例
Redis哨兵模式是Redis高可用方案的一種實(shí)現(xiàn)方式,通過哨兵來自動(dòng)實(shí)現(xiàn)故障轉(zhuǎn)移,從而保證高可用,本文主要介紹了SpringBoot整合Redis哨兵模式的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下2024-02-02
mybatis 解決從列名到屬性名的自動(dòng)映射失敗問題
這篇文章主要介紹了mybatis 解決從列名到屬性名的自動(dòng)映射失敗問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
Docker 存儲(chǔ)驅(qū)動(dòng)詳細(xì)介紹
這篇文章主要介紹了Docker 存儲(chǔ)驅(qū)動(dòng)詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下2016-10-10
Java實(shí)戰(zhàn)項(xiàng)目 圖書管理系統(tǒng)
這篇文章主要介紹了使用java SSM jsp mysql maven設(shè)計(jì)實(shí)現(xiàn)的精品圖書管理系統(tǒng),是一個(gè)很好的實(shí)例,對(duì)大家的學(xué)習(xí)和工作具有借鑒意義,建議收藏一下2021-09-09
java.text.DecimalFormat類十進(jìn)制格式化
這篇文章主要為大家詳細(xì)介紹了java.text.DecimalFormat類十進(jìn)制格式化的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03
詳解idea文件右鍵創(chuàng)建New沒有Create New Servlet的解決辦法
這篇文章主要介紹了詳解idea文件右鍵創(chuàng)建New沒有Create New Servlet的解決辦法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12

