Java ByteArrayOutputStream toString() Method with Examples

文章推薦指數: 80 %
投票人數:10人

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



請為這篇文章評分?