ByteArrayOutputStream (Java SE 10 & JDK 10 )
文章推薦指數: 80 %
This method is equivalent to #toString(charset) that takes a charset . An invocation of this method of the form ByteArrayOutputStream b = ... b.toString("UTF-8") java.lang.Object java.io.OutputStream java.io.ByteArrayOutputStream AllImplementedInterfaces: Closeable,Flushable,AutoCloseable publicclassByteArrayOutputStream extendsOutputStream Thisclassimplementsanoutputstreaminwhichthedatais writtenintoabytearray.Thebufferautomaticallygrowsasdata iswrittentoit. ThedatacanberetrievedusingtoByteArray()and toString(). ClosingaByteArrayOutputStreamhasnoeffect.Themethodsin thisclasscanbecalledafterthestreamhasbeenclosedwithout generatinganIOException. Since: 1.0 FieldSummary Fields ModifierandType Field Description protectedbyte[] buf Thebufferwheredataisstored. protectedint count Thenumberofvalidbytesinthebuffer. ConstructorSummary Constructors Constructor Description ByteArrayOutputStream() Createsanewbytearrayoutputstream. ByteArrayOutputStream(int size) Createsanewbytearrayoutputstream,withabuffercapacityof thespecifiedsize,inbytes. MethodSummary AllMethods InstanceMethods ConcreteMethods DeprecatedMethods ModifierandType Method Description void close() ClosingaByteArrayOutputStreamhasnoeffect. void reset() Resetsthecountfieldofthisbytearrayoutput streamtozero,sothatallcurrentlyaccumulatedoutputinthe outputstreamisdiscarded. int size() Returnsthecurrentsizeofthebuffer. byte[] toByteArray() Createsanewlyallocatedbytearray. String toString() Convertsthebuffer'scontentsintoastringdecodingbytesusingthe platform'sdefaultcharacterset. String toString(int hibyte) Deprecated. Thismethoddoesnotproperlyconvertbytesintocharacters. String toString(String charsetName) Convertsthebuffer'scontentsintoastringbydecodingthebytesusing thenamedcharset. String toString(Charset charset) Convertsthebuffer'scontentsintoastringbydecodingthebytesusing thespecifiedcharset. void write(byte[] b, int off, int len) Writeslenbytesfromthespecifiedbytearray startingatoffsetofftothisbytearrayoutputstream. void write(int b) Writesthespecifiedbytetothisbytearrayoutputstream. void writeTo(OutputStream out) Writesthecompletecontentsofthisbytearrayoutputstreamto thespecifiedoutputstreamargument,asifbycallingtheoutput stream'swritemethodusingout.write(buf,0,count). Methodsdeclaredinclass java.lang.Object clone,equals,finalize,getClass,hashCode,notify,notifyAll,wait,wait,wait Methodsdeclaredinclass java.io.OutputStream flush,write FieldDetail buf protected byte[]buf Thebufferwheredataisstored. count protected intcount Thenumberofvalidbytesinthebuffer. ConstructorDetail ByteArrayOutputStream public ByteArrayOutputStream() Createsanewbytearrayoutputstream.Thebuffercapacityis initially32bytes,thoughitssizeincreasesifnecessary. ByteArrayOutputStream public ByteArrayOutputStream(int size) Createsanewbytearrayoutputstream,withabuffercapacityof thespecifiedsize,inbytes. Parameters: size-theinitialsize. Throws: IllegalArgumentException-ifsizeisnegative. MethodDetail write public void write(int b) Writesthespecifiedbytetothisbytearrayoutputstream. Specifiedby: write inclass OutputStream Parameters: b-thebytetobewritten. write public void write(byte[] b, int off, int len) Writeslenbytesfromthespecifiedbytearray startingatoffsetofftothisbytearrayoutputstream. Overrides: write inclass OutputStream Parameters: b-thedata. off-thestartoffsetinthedata. len-thenumberofbytestowrite. writeTo public void writeTo(OutputStream out) throwsIOException Writesthecompletecontentsofthisbytearrayoutputstreamto thespecifiedoutputstreamargument,asifbycallingtheoutput stream'swritemethodusingout.write(buf,0,count). Parameters: out-theoutputstreamtowhichtowritethedata. Throws: IOException-ifanI/Oerroroccurs. reset public void reset() Resetsthecountfieldofthisbytearrayoutput streamtozero,sothatallcurrentlyaccumulatedoutputinthe outputstreamisdiscarded.Theoutputstreamcanbeusedagain, reusingthealreadyallocatedbufferspace. SeeAlso: ByteArrayInputStream.count toByteArray public byte[] toByteArray() Createsanewlyallocatedbytearray.Itssizeisthecurrent sizeofthisoutputstreamandthevalidcontentsofthebuffer havebeencopiedintoit. Returns: thecurrentcontentsofthisoutputstream,asabytearray. SeeAlso: size() size public int size() Returnsthecurrentsizeofthebuffer. Returns: thevalueofthecountfield,whichisthenumber ofvalidbytesinthisoutputstream. SeeAlso: count toString public String toString() Convertsthebuffer'scontentsintoastringdecodingbytesusingthe platform'sdefaultcharacterset.ThelengthofthenewString isafunctionofthecharacterset,andhencemaynotbeequaltothe sizeofthebuffer. Thismethodalwaysreplacesmalformed-inputandunmappable-character sequenceswiththedefaultreplacementstringfortheplatform's defaultcharacterset.TheCharsetDecoder classshouldbeusedwhenmorecontroloverthedecodingprocessis required. Overrides: toString inclass Object Returns: Stringdecodedfromthebuffer'scontents. Since: 1.1 toString public String toString(String charsetName) throwsUnsupportedEncodingException Convertsthebuffer'scontentsintoastringbydecodingthebytesusing thenamedcharset. Thismethodisequivalentto#toString(charset)thattakesa charset. Aninvocationofthismethodoftheform ByteArrayOutputStreamb=... b.toString("UTF-8") behavesinexactlythesamewayastheexpression ByteArrayOutputStreamb=... b.toString(StandardCharsets.UTF_8) Parameters: charsetName-thenameofasupported charset Returns: Stringdecodedfromthebuffer'scontents. Throws: UnsupportedEncodingException-Ifthenamedcharsetisnotsupported Since: 1.1 toString public String toString(Charset charset) Convertsthebuffer'scontentsintoastringbydecodingthebytesusing thespecifiedcharset.Thelengthofthenew Stringisafunctionofthecharset,andhencemaynotbeequal tothelengthofthebytearray. Thismethodalwaysreplacesmalformed-inputandunmappable-character sequenceswiththecharset'sdefaultreplacementstring.TheCharsetDecoderclassshouldbeusedwhenmorecontrol overthedecodingprocessisrequired. Parameters: charset-thecharset tobeusedtodecodethebytes Returns: Stringdecodedfromthebuffer'scontents. Since: 10 toString @Deprecated public String toString(int hibyte) Deprecated. Thismethoddoesnotproperlyconvertbytesintocharacters. AsofJDK 1.1,thepreferredwaytodothisisviathe toString(StringcharsetName)ortoString(Charsetcharset) method,whichtakesanencoding-nameorcharsetargument, orthetoString()method,whichusestheplatform'sdefault characterencoding. Createsanewlyallocatedstring.Itssizeisthecurrentsizeof theoutputstreamandthevalidcontentsofthebufferhavebeen copiedintoit.Eachcharactercintheresultingstringis constructedfromthecorrespondingelementbinthebyte arraysuchthat: c==(char)(((hibyte&0xff)<<8)|(b&0xff)) Parameters: hibyte-thehighbyteofeachresultingUnicodecharacter. Returns: thecurrentcontentsoftheoutputstream,asastring. SeeAlso: size(), toString(String), toString() close public void close() throwsIOException ClosingaByteArrayOutputStreamhasnoeffect.Themethodsin thisclasscanbecalledafterthestreamhasbeenclosedwithout generatinganIOException. Specifiedby: close ininterface AutoCloseable Specifiedby: close ininterface Closeable Overrides: close inclass OutputStream Throws: IOException-ifanI/Oerroroccurs.
延伸文章資訊
- 1Java Code Examples of java.io.ByteArrayOutputStream
getResourceAsStream("transform-0-1.xslt"); ByteArrayOutputStream os=new ... toByteArray(),"utf-8"...
- 2java.io.ByteArrayOutputStream.toString(String charsetName ...
ByteArrayOutputStream.toString(String charsetName)方法實例 ... toString("UTF-8"); System.out.println(...
- 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 ByteArrayOutputStream.toString()指定字符集转换为字符串
charsetName:指定字符集(utf-8,GB2312等)。 返回. 从缓冲区内容解码的字符串。 异常. UnsupportedEncodingException :不支持指定的字符集 ...
- 5使用ByteArrayOutputStream解决输出乱码问题 - CSDN博客
先来看一段看似没问题的代码这是一个使用utf-8编码的文本内容,下面使用输入流读取文件内容,打印到控制台。