📅  最后修改于: 2020-09-27 06:54:48             🧑  作者: Mango
CharArrayWriter类可用于将公共数据写入多个文件。该类继承Writer类。当数据写入此流时,其缓冲区自动增长。在此对象上调用close()方法无效。
让我们看一下Java.io.CharArrayWriter类的声明:
public class CharArrayWriter extends Writer
Method | Description |
---|---|
int size() | It is used to return the current size of the buffer. |
char[] toCharArray() | It is used to return the copy of an input data. |
String toString() | It is used for converting an input data to a string. |
CharArrayWriter append(char c) | It is used to append the specified character to the writer. |
CharArrayWriter append(CharSequence csq) | It is used to append the specified character sequence to the writer. |
CharArrayWriter append(CharSequence csq, int start, int end) | It is used to append the subsequence of a specified character to the writer. |
void write(int c) | It is used to write a character to the buffer. |
void write(char[] c, int off, int len) | It is used to write a character to the buffer. |
void write(String str, int off, int len) | It is used to write a portion of string to the buffer. |
void writeTo(Writer out) | It is used to write the content of buffer to different character stream. |
void flush() | It is used to flush the stream. |
void reset() | It is used to reset the buffer. |
void close() | It is used to close the stream. |
在此示例中,我们将通用数据写入4个文件a.txt,b.txt,c.txt和d.txt。
package com.javatpoint;
import java.io.CharArrayWriter;
import java.io.FileWriter;
public class CharArrayWriterExample {
public static void main(String args[])throws Exception{
CharArrayWriter out=new CharArrayWriter();
out.write("Welcome to javaTpoint");
FileWriter f1=new FileWriter("D:\\a.txt");
FileWriter f2=new FileWriter("D:\\b.txt");
FileWriter f3=new FileWriter("D:\\c.txt");
FileWriter f4=new FileWriter("D:\\d.txt");
out.writeTo(f1);
out.writeTo(f2);
out.writeTo(f3);
out.writeTo(f4);
f1.close();
f2.close();
f3.close();
f4.close();
System.out.println("Success...");
}
}
输出量
Success...
执行该程序后,您可以看到所有文件都具有公用数据:欢迎使用javaTpoint。
a.txt:
Welcome to javaTpoint
b.txt:
Welcome to javaTpoint
c.txt:
Welcome to javaTpoint
d.txt:
Welcome to javaTpoint