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

What is the use of StringWriter streams in the basics of Java

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

Share

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

This article shows you how to use the StringWriter stream in the basic knowledge of Java. The content is concise and easy to understand. It will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

The use of Java StringWriter flow I. StringWriter flow definition

API description: the character stream that collects the output in the string buffer can be used to construct strings. Closing the stream is not valid. Calling other methods after closing will not report an exception.

2. StringWriter stream instance domain / / string buffer pass area private StringBuffer buf; 3. StringWriter stream constructor / * create character output stream with default size StringBuffer * / public StringWriter () {buf = new StringBuffer (); lock = buf } / * create a character output stream using StringBuffer of the specified size * / public StringWriter (int initialSize) {if (initialSize)

< 0) { throw new IllegalArgumentException("Negative buffer size"); } buf = new StringBuffer(initialSize); lock = buf; }四、StringWriter流方法 1)write(int c):写入一个字符到字符串缓冲区中 write(int c) 2)write(char cbuf[], int off, int len):从指定字符数组的下标off开始写len个字节到流中 /** * 从指定字符数组的下标off开始写len个字节到流中 */ public void write(char cbuf[], int off, int len) { if ((off < 0) || (off >

Cbuf.length) | | (len

< 0) || ((off + len) >

Cbuf.length) | ((off + len) < 0) {throw new IndexOutOfBoundsException ();} else if (len = = 0) {return;} buf.append (cbuf, off, len);}

3) write (String str): write a string to the stream

/ * write a string to stream * / public void write (String str) {buf.append (str);}

4) write (String str, int off, int len): write part of a string to the stream

/ * write part of a string to the stream * / public void write (String str, int off, int len) {buf.append (str.substring (off, off + len));}

5) retrieve the data of the character output stream

/ * convert the value of the buffer object to a string output * / public String toString () {return buf.toString ();} / * return the character buffer object StringBuff * * @ return StringBuffer holding the current buffer value. * / public StringBuffer getBuffer () {return buf;}

6) close (): it is invalid to close the stream. Calling this method after closing will not report an exception.

/ * it is invalid to close the stream. Calling this kind of method after closing will not report an exception * / public void close () throws IOException {} 5. The function of StringWriter stream

It has not been used yet, so it is not clear when and where to use it, so understand the function first.

Benefits of using StringWriter and StringReader

When you have a set of application programming interfaces (API) that only allow Writer or Reader as input, but you want to use String, you can use StringWriter or StringReader.

Suppose there is a process method that passes a Person object to the method and writes the processing result to a Writer object:

Public void process_ (Person person, Writer writer)

This is a good way to design API, because API doesn't have to care about object construction, and it's simple and generic.

The Writer class outputs data to a file; however, sometimes the data must be kept in memory, such as if you want to display it on the graphical user interface (GUI) before the data is output, you can use StringWriter. Instead of writing data to some form of output device, StringWriter writes to memory. It has an empty constructor and a toString method to get StringBuffer. For example:

Writer writer =...; StringWriter out = new StringWriter (writer); process (person, out); StringBuffer result = out.getBuffer ()

The same is true when reading a file. You can use StringReader instead of Reader to trick API without having to read it from some form of file. The constructor of StringReader requires a String parameter. For example:

Reader in = new StringReader (""); alarm.loadConfig (in)

The Writer and Reader classes are designed to handle string-based input and output. The InputStream and OutputStream classes should be used to deal with byte-based Imax O.

In classes at this level, you can use ByteArrayInputStream and ByteArrayOutputStream to achieve similar effects of StringReader and StringWriter.

The above is how to use StringWriter streams in the basics of Java. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

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