Java StringWriter 类根据字符串缓冲区流的字符创建字符串。 Java 中StringWriter 类的方法也可以在关闭 Stream 后调用,因为这不会引发 IO 异常。
Java StringWriter 类中的声明
public class StringWriter extends Writer
Java 中StringWriter 类中的构造函数
Java中String Writer类中使用的构造函数如下:
- StringWriter():使用字符串缓冲区的初始或默认大小创建新的StringWriter
- StringWriter(int 大小):使用提到的字符串缓冲区大小创建一个新的StringWriter
Java StringWriter 类中的方法
下面提到了 Java StringWriter 类涉及的所有方法:
Method |
Description |
---|---|
append(char Sw) | 将单个字符追加到字符串缓冲区。 |
追加(CharSequence char_sq) | 将指定的字符序列附加到字符串缓冲区。 |
追加(CharSequence,char_sq,int开始,int结束) | 将字符序列的指定部分追加到字符串缓冲区。 |
flush() | 刷新StringWriter流。 |
getBuffer() | 返回 StringWriter 的StringBuffer。 |
write(int char) | 将单个字符写入字符串缓冲区。 |
write(String str) | 将字符串写入字符串缓冲区。 |
write(String str, int offset, int maxlen) | 将字符串的某些部分写入字符串缓冲区。 |
write(char[] carray, int 偏移量, int maxlen) | 将字符数组的某些部分写入字符串缓冲区。 |
toString() | 以字符串形式返回字符串缓冲区的值 |
close() | closes String Buffer。由于该方法还可以在关闭String Buffer之后调用,所以它不起作用。 |
1. 写(int char)
java.io.StringWriter.write(int char) 将单个字符写入字符串缓冲区。
Syntax: public void write(int char) Parameters : char : int value of the character to be written. Return : void
2. 写入(字符串str)
java.io.StringWriter.write(String str)将字符串写入字符串缓冲区。
Syntax: public void write(String str) Parameters : str : string to be written to the String Buffer. Return : void
3. 写入(字符串str,int偏移量,int maxlen)
java.io.StringWriter.write(String str, int offset, int maxlen)将字符串的某些部分写入字符串缓冲区。
Syntax: public void write(String str, int offset, int maxlen) Parameters : str : string to be written to the String Buffer. offset : start position of the String maxlen : maximum length upto which string has to written Return : void
4. write(char[] carray, int 偏移量, int maxlen)
java.io.StringWriter.write(char[] carray, int 偏移量, int maxlen)将字符数组的某些部分写入字符串缓冲区。
Syntax: public void write(char[] carray, int offset, int maxlen) Parameters : carray : character to be written to the String Buffer offset : start position of the character array maxlen : maximum no. of the character of the carray has to written Return : void
5.toString()
java.io.StringWriter.toString() 以字符串形式返回字符串缓冲区的值。
Syntax: public String toString() Parameters : ----------- Return : string value of the String Buffer
6.close()
java.io.StringWriter.close() 关闭字符串缓冲区。由于该方法也可以在关闭字符串缓冲区后调用,因此它不起作用。
Syntax: public void close() Parameters : ----------- Return : void
7. 追加(char Sw)
java.io.StringWriter.append(char Sw) 将单个字符追加到字符串缓冲区。
Syntax: public StringWriter append(char Sw) Parameters : Sw : character to be append Return : StringWriter
8. 追加(CharSequence char_sq)
java.io.StringWriter.append(CharSequence char_sq) 将指定的字符序列附加到字符串缓冲区。
Syntax: public StringWriter append(CharSequence char_sq) Parameters : char_sq : Character sequence to append. Return : StringWriter, if char sequence is null, then NULL appends to the StringWriter.
9. 追加(CharSequence char_sq, int开始,int结束)
java.io.StringWriter.append(CharSequence char_sq, int start, int end) 将字符序列的指定部分追加到字符串缓冲区。
Syntax: public StringWriter append(CharSequence char_sq, int start, int end) Parameters : char_sq : Character sequence to append. start : start of character in the Char Sequence end : end of character in the Char Sequence Return : void
10.flush()
java.io.StringWriter.flush() 刷新StringWriter 流。
Syntax: public void flush() Parameters : ----- Return : void
11.getBuffer()
java.io.StringWriter.getBuffer()返回StringWriter的字符串缓冲区。
Syntax: public StringBuffer getBuffer() Parameters : ----------- Return : String Buffer with its current value.
示例
Java 程序说明了 StringWriter 类方法的使用:
Java
// Java program illustrating the working of StringWriter
// class methods write(int char), write(String str), close()
// write(String str, int offset, int maxlen), toString()
// write(char[] carray, int offset, int maxlen)
import java.io.*;
public class NewClass {
public static void main(String[] args)
throws IOException
{
// Initializing String Writer
StringWriter geek_writer1 = new StringWriter();
StringWriter geek_writer2 = new StringWriter();
StringWriter geek_writer3 = new StringWriter();
StringWriter geek_writer4 = new StringWriter();
// Use of write(int char) : to write a character
geek_writer1.write(71);
geek_writer1.write(70);
geek_writer1.write(71);
// Use of toString() : Value written by write(int
// char)
System.out.println("Using write(int char) : "
+ geek_writer1.toString());
String str = "Hello Geeks";
// Use of write(String str) : to write string
geek_writer2.write(str);
// Value written by write(String str)
System.out.println("Using write(String str) : "
+ geek_writer2.toString());
// Use of write(String str, int offset, int maxlen)
// : to write part of string
geek_writer3.write(str, 2, 4);
geek_writer3.write(str, 5, 6);
// Value written by write(String str, int offset,
// int maxlen)
System.out.println(
"Using write(str, offset, maxlen) : "
+ geek_writer3.toString());
try {
// Use of Close() method : to close the
// geek_writer4 But no use as String Writer
// method is still working with it
geek_writer4.close();
}
catch (IOException except) {
System.out.println("ERROR");
except.printStackTrace();
;
}
// Initializing character array
char[] carray = str.toCharArray();
// Use of write(char[] carray, int offset, int
// maxlen): to write part of char array
geek_writer4.write(carray, 4, 6);
// Value written by write(String str, int offset,
// int maxlen)
System.out.println(
"Using write(carray, offset, maxlen) : "
+ geek_writer4.toString());
}
}
输出
Using write(int char) : GFG Using write(String str) : Hello Geeks Using write(str, offset, maxlen) : llo Geeks Using write(carray, offset, maxlen) : o Geek
说明 StringWriter 类方法的使用的 Java 程序:append(CharSequence char_sq)、append(char Sw)、append(CharSequence char_sq、int start、int end)、flush()、getgetBuffer()
Java
// Java program illustrating the working of StringWriter
// class methods append(CharSequence char_sq), append(char
// Sw) append(CharSequence char_sq, int start,int end)
// flush(), getgetBuffer()
import java.io.*;
public class NewClass {
public static void main(String[] args)
throws IOException
{
// Initializing String Writer
StringWriter geek_writer1 = new StringWriter();
StringWriter geek_writer2 = new StringWriter();
StringWriter geek_writer3 = new StringWriter();
// Use of write(int char) : to write a character
geek_writer1.write(71);
geek_writer1.write(70);
geek_writer1.write(71);
// Use of toString() : Value written by write(int
// char)
System.out.println("Using write(int char) : "
+ geek_writer1.toString());
// Using flush() method
geek_writer1.flush();
System.out.println("Using flush() : "
+ geek_writer1.toString());
// Use of append(char Sw)
System.out.println("append(char Sw) : "
+ geek_writer1.append(" 1GFG1"));
// Use of getBuffer() :
System.out.println("Using geek_writer1 : "
+ geek_writer1.getBuffer());
// Initializing Character Sequence
CharSequence char_sq1 = "1 Hello 1";
CharSequence char_sq2 = " : 2 Geeks 2";
// Use of append(CharSequence char_sq)
geek_writer2.append(char_sq1);
geek_writer2.append(char_sq2);
System.out.println("append(char_sq) : "
+ geek_writer2.toString());
// Use of append(CharSequence char_sq,int start,int
// end)
geek_writer3.append(char_sq1, 0, 3);
geek_writer3.append(char_sq2, 3, 6);
System.out.println("append(char_sq,start,end) : "
+ geek_writer3.toString());
}
}
输出
Using write(int char) : GFG Using flush() : GFG append(char Sw) : GFG 1GFG1 Using geek_writer1 : GFG 1GFG1 append(char_sq) : 1 Hello 1 : 2 Geeks 2 append(char_sq,start,end) : 1 H2 G
相关用法
- Java Java.io.StringWriter.append()用法及代码示例
- Java Java.io.StringWriter.close()用法及代码示例
- Java Java.io.StringWriter.flush()用法及代码示例
- Java Java.io.StringWriter.getBuffer()用法及代码示例
- Java Java.io.StringWriter.toString()用法及代码示例
- Java Java.io.StringWriter.write()用法及代码示例
- Java Java.io.StringReader.close()用法及代码示例
- Java Java.io.StringReader.mark()用法及代码示例
- Java Java.io.StringReader.markSupported()用法及代码示例
- Java Java.io.StringReader.read()用法及代码示例
- Java Java.io.StringReader.ready()用法及代码示例
- Java Java.io.StringReader.reset()用法及代码示例
- Java Java.io.StringReader.skip()用法及代码示例
- Java Java.io.StringBufferInputStream用法及代码示例
- Java Java.io.StringReader用法及代码示例
- Java Java.io.StreamTokenizer.commentChar()用法及代码示例
- Java Java.io.StreamTokenizer.eolIsSignificant()用法及代码示例
- Java Java.io.StreamTokenizer.lineno()用法及代码示例
- Java Java.io.StreamTokenizer.lowerCaseMode()用法及代码示例
- Java Java.io.StreamTokenizer.nextToken()用法及代码示例
- Java Java.io.StreamTokenizer.ordinaryChar()用法及代码示例
- Java Java.io.StreamTokenizer.ordinaryChars()用法及代码示例
- Java Java.io.StreamTokenizer.parseNumbers()用法及代码示例
- Java Java.io.StreamTokenizer.pushBack()用法及代码示例
- Java Java.io.StreamTokenizer.quoteChar()用法及代码示例
注:本文由纯净天空筛选整理自佚名大神的英文原创作品 Java.io.StringWriter class in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。