Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write the sample code for the conversion between String and Inputstreem

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

The content of this article mainly focuses on how to write the sample code for the conversion between String and Inputstreem. The content of the article is clear and clear. It is very suitable for beginners to learn and is worth reading. Interested friends can follow the editor to read together. I hope you can get something through this article!

URLConnection urlConn = url.openConnection (); / / Open the website link s

BufferedReader reader = new BufferedReader (new InputStreamReader (urlConn.getInputStream (), "UTF-8")); / / instantiate the input stream and get the web page code

String s; / / cycle in turn, until the value to read is empty

StringBuilder sb = new StringBuilder ()

While ((s = reader.readLine ())! = null) {

Sb.append (s)

}

Reader.close ()

String str = sb.toString ()

The following method is a little disgusting, changed, looks much better =

Original: http://blog.csdn.net/soundtravel/article/details/6927006

String str = ""; / / add your string content

InputStream inputStream = new ByteArrayInputStream (str.getBytes ())

1 package org.kodejava.example.io

two

3 import java.io.ByteArrayInputStream

4 import java.io.InputStream

five

6 publicclass StringToStream {

7 publicstaticvoid main (String [] args) {

8 String text = "Converting String to InputStream Example"

nine

10 /

11 Convert String to InputString using ByteArrayInputStream class.

12 This class constructor takes the string byte array which can be

13 done by calling the getBytes () method.

14 * /

15 try {

16 InputStream is = new ByteArrayInputStream (text.getBytes ("UTF-8"))

17} catch (UnsupportedEncodingException e) {

18 e.printStackTrace ()

19}

20}

21}

twenty-two

1. Convert the string to inputStream

Java Code Collection Code

String string

/ /.

InputStream is = new ByteArrayInputStream (string.getBytes ())

2. InputStream to string

Java Code Collection Code

ByteArrayOutputStream baos = new ByteArrayOutputStream ()

Int i

While ((I = is.read ())! =-1) {

Baos.write (I)

}

String str = baos.toString ()

System.out.println (str)

3. String writes to OutputStream

Java Code Collection Code

OutputStream os = System.out

Os.write (string.getBytes ())

4. OutputStream writes to String

This sounds ridiculous, OutputStream is the output source, but also written to String?

However, recently, a similar problem has been encountered in the project. For example, the method of SOAPMessage.writeTo (OutputStream os) is to write the content of SOAPMessage to an output stream. If I want to get the content of this stream, I can't write it into a file and then read it. After studying it for a long time, I finally remembered that it can be as follows:

Java Code Collection Code

ByteArrayOutputStream baos = new ByteArrayOutputStream ()

/ / write to OutPutStream, such as message.writeTo (baos)

String str = baos.toString ()

Convert InputStream/OutputStream to string

Here we need to use a special class ByteArrayOutputStream, with which we can convert the output stream directly to the String type in memory.

The specific code is as follows:

First read the data from the input stream and write it to ByteArrayOutputStream, and then convert it to String.

Java Code Collection Code

InputStream in = urlconn.getInputStream (); / / get the input stream

ByteArrayOutputStream bos = new ByteArrayOutputStream ()

/ / read cache

Byte [] buffer = new byte [2048]

Int length = 0

While ((length = in.read (buffer))! =-1) {

Bos.write (buffer, 0, length); / / write output stream

}

In.close (); / / after reading, close the input stream

/ / create a string object based on the output stream

New String (bos.toByteArray (), "UTF-8")

/ / or

/ / bos.toString ("UTF-8")

According to the same principle, we can convert outputstream directly into String objects.

Specify the character set

Byte [] b = str.getBytes ("utf-8")

String s = new String (b, "utf-8")

Usage of method WRITE in OUTPUTSTREAM

Void write (byte [] b)

Writes b.length bytes from the specified byte array to this output stream.

Void write (byte [] b, int off, int len)

Writes the len bytes from the offset off in the specified byte array to this output stream.

Abstract void write (int b)

Writes the specified bytes to this output stream.

Thank you for your reading. I believe you have some understanding of "how to write the sample code for the conversion between String and Inputstreem". Go and practice it. If you want to know more about it, you can follow the website! The editor will continue to bring you better articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report