Java ByteArrayOutputStream toString() Method with Examples
文章推薦指數: 80 %
The toString() method of Java ByteArrayOutputStream class is used to convert the buffer's contents ... toString("UTF-8");; System.out.println(str);; }; }. ⇧SCROLLTOTOP Home Java Programs OOPs String Exception Multithreading Collections JavaFX JSP Spring SpringBoot Projects InterviewQuestions ByteArrayOutputStream ByteArrayOutputStream close() reset() size() toByteArray() toString() write() writeTo() next→ ←prev JavaByteArrayOutputStreamtoString()method ThetoString()methodofJavaByteArrayOutputStreamclassisusedtoconvertthebuffer'scontentsintoastringbydecodingthebytesusingthenamedcharset.ThelengthofthenewStringisafunctionofthecharset,andhencemaynotbeequaltothelengthofthebytearray. Syntax: publicStringtoString(StringcharsetName)throwsUnsupportedEncodingException Parameters: charsetName-thenameofasupportedcharset Returns ThetoString()methodreturnstheStringdecodedfromthebuffer'scontents. Throws: UnsupportedEncodingException-Ifthenamecharsetisnotsupported. Example1 importjava.io.ByteArrayOutputStream; importjava.io.IOException; publicclassJavaByteArrayOutputStreamToStringExample1{ publicstaticvoidmain(String[]args)throwsIOException{ bytebs[]={65,66,67,68,69}; ByteArrayOutputStreambaos=newByteArrayOutputStream(); baos.write(bs); Stringstr=baos.toString("Cp1047"); System.out.println(str); str=baos.toString("UTF-8"); System.out.println(str); } } TestitNow Output: âäàá ABCDE Example2 importjava.io.ByteArrayOutputStream; importjava.io.IOException; publicclassByteArrayOutputStreamToStringExample2{ publicstaticvoidmain(String[]args)throwsIOException{ bytebs[]={86,87,88,89,90}; ByteArrayOutputStreambaos=newByteArrayOutputStream(); baos.write(bs); Stringstr=baos.toString("Cp1047"); System.out.println(str); str=baos.toString("UTF-8"); System.out.println(str); } } TestitNow Output: ÎÏÌβ! VWXYZ JavaByteArrayOutputStreamtoString()method ThetoString()methodofJavaByteArrayOutputStreamclassisusedtoconvertthebuffer?scontentsintoastringdecodingbytesusingtheplatform?sdefaultcharacterset. Thismethodtransformsanobjectintoastring.Itisusedtoconstructastringcontainingtheinformationonanobjectthatcanbeprinted,andwecanredefineitforaspecificclass. Syntax: publicStringtoString() Overrides toString()methodinclassObject Returns ThetoString()methodreturnsthestringrepresentationoftheobject. Example1 importjava.io.ByteArrayOutputStream; importjava.io.IOException; publicclassJavaByteArrayOutputStreamToStringExample1{ publicstaticvoidmain(String[]args)throwsIOException{ byte[]bs={86,87,88,89,90}; ByteArrayOutputStreambaos=newByteArrayOutputStream(); baos.write(bs); Stringstr=baos.toString(); System.out.println(str); } } TestitNow Output: VWXYZ Example2 importjava.io.ByteArrayOutputStream; importjava.io.IOException; publicclassJavaByteArrayOutputStreamToStringExample2{ publicstaticvoidmain(String[]args)throwsIOException{ byte[]bs={75,76,77,78,79}; ByteArrayOutputStreambaos=newByteArrayOutputStream(); baos.write(bs); Stringstr=baos.toString(); System.out.println(str); } } TestitNow Output: KLMNO NextTopicJavaByteArrayOutputStreamClass ←prev next→ ForVideosJoinOurYoutubeChannel:JoinNow Feedback SendyourFeedbackto[email protected] HelpOthers,PleaseShare LearnLatestTutorials Splunk SPSS Swagger Transact-SQL Tumblr ReactJS Regex ReinforcementLearning RProgramming RxJS ReactNative PythonDesignPatterns PythonPillow PythonTurtle Keras Preparation Aptitude Reasoning VerbalAbility InterviewQuestions CompanyQuestions TrendingTechnologies ArtificialIntelligence AWS Selenium CloudComputing Hadoop ReactJS DataScience Angular7 Blockchain Git MachineLearning DevOps B.Tech/MCA DBMS DataStructures DAA OperatingSystem ComputerNetwork CompilerDesign ComputerOrganization DiscreteMathematics EthicalHacking ComputerGraphics SoftwareEngineering WebTechnology CyberSecurity Automata CProgramming C++ Java .Net Python Programs ControlSystem DataMining DataWarehouse JavatpointServicesJavaTpointofferstoomanyhighqualityservices.Mailuson[email protected],togetmoreinformationaboutgivenservices.WebsiteDesigningWebsiteDevelopmentJavaDevelopmentPHPDevelopmentWordPressGraphicDesigningLogoDigitalMarketingOnPageandOffPageSEOPPCContentDevelopmentCorporateTrainingClassroomandOnlineTrainingDataEntryTrainingForCollegeCampusJavaTpointofferscollegecampustrainingonCoreJava,AdvanceJava,.Net,Android,Hadoop,PHP,WebTechnologyandPython.Pleasemailyourrequirementat[email protected]Duration:1weekto2weekLike/Subscribeusforlatestupdatesornewsletter
延伸文章資訊
- 1Java ByteArrayOutputStream toString() Method with Examples
The toString() method of Java ByteArrayOutputStream class is used to convert the buffer's content...
- 2How to convert a String to and fro from UTF8 byte array
Following example will showcase conversion of a Unicode String to UTF8 byte[] and UTF8 byte[] to ...
- 3java - Convert a byte array from Encoding A to Encoding B
1) raw: ByteArrayOutputStream containing bytes of a BINARY object sent to us from clients. The by...
- 4java.io.ByteArrayOutputStream.toString(String charsetName ...
ByteArrayOutputStream.toString(String charsetName)方法實例 ... toString("UTF-8"); System.out.println(...
- 5一文让你读懂JAVA.IO、字符编码 - 51CTO
用ByteArrayOutputStream暂时缓存来自其他渠道的数据 ByteArrayOutputStream data = new ... 如“程”的utf-8编码格式,由[-25][-8...