StringWriter 示例
Java StringWriter 类是一个字符流,它从字符串缓冲区收集输出,可用于构造字符串。
StringWriter 类扩展了 Writer 类。
在 StringWriter 类中,不使用网络套接字和文件等系统资源,因此不需要关闭 StringWriter。
import java.io.*;
public class StringWriterDemo {
public static void main(String[] args) throws IOException {
char[] ary = new char[1024];
StringWriter writer = new StringWriter();
FileInputStream input = null;
BufferedReader buffer = null;
input = new FileInputStream("c://stringwriter.txt");
buffer = new BufferedReader(new InputStreamReader(input, "UTF-8"));
int x;
while ((x = buffer.read(ary)) != -1) {
writer.write(ary, 0, x);
}
System.out.println(writer.toString());
writer.close();
buffer.close();
}
}
上面的示例帮助我们了解 StringWriter 的简单示例,使用 BufferedReader 从流中读取文件数据。