对应的测试类
public static void main(String[] args) {
String path = "D:" + File.separator + "data.txt";
File file = new File(path);
try(
FileReader fr = new FileReader(file);
// need add append args
FileWriter fw = new FileWriter(file, true);
BufferedReader reader = new BufferedReader(fr);
BufferedWriter writer = new BufferedWriter(fw)){
String data;
int count = 0;
for (int i = 0; i < 100; i++) {
writer.append(String.valueOf(i));
writer.flush();
writer.newLine();
}
while ((data = reader.readLine()) != null) {
System.out.println(++count + " : " + data);
}
} catch (FileNotFoundException e) {
System.out.println("file not Fund[" + e.getMessage() + "]");
} catch (IOException e) {
System.out.println("IOException [" + e.getMessage() + "]");
}
}
/**
* StringReader ??? [A character stream whose source is a string.]
*/
@Test
public void testStringReader(){
StringReader sr = new StringReader("asdasdasd");
int c;
try {
while((c = sr.read()) != -1){
System.out.println((char) c);
}
} catch (IOException e) {
System.out.println("IO Exception ");
}
}
@Test
public void testStringWriter() throws IOException {
StringWriter sw = new StringWriter(20);
char[] chars = {'a' , 'b' , 'c', 'a' , 'b' , 'c', 'a' , 'b' , 'c', 'a' , 'b' , 'c', 'a' , 'b' , 'c'};
// 这个offset 指的是 StringWriter的偏移量
sw.write(chars, 10, 3);
System.out.println(sw.toString());
}
/**
* 可以通过getLineNumber() 和 setLineNumber() 来控制读取
*/
@Test
public void testLineNumberReader(){
String path = "D:" + File.separator + "data.txt";
File file = new File(path);
try(
FileReader fr = new FileReader(file);
// need add append args
FileWriter fw = new FileWriter(file, true);
BufferedReader reader = new BufferedReader(fr);
BufferedWriter writer = new BufferedWriter(fw);
LineNumberReader lnr = new LineNumberReader(reader)){
int data ;
lnr.setLineNumber(100);
while((data = lnr.read()) != -1){
System.out.println(lnr.getLineNumber());
//System.out.println((char)data);
}
} catch (FileNotFoundException e) {
System.out.println("file not Fund[" + e.getMessage() + "]");
} catch (IOException e) {
System.out.println("IOException [" + e.getMessage() + "]");
}
}
PipedRead PipedWriter测试
public class TestPipedWriterReader{
public static void main(String[] args) {
CountDownLatch cdl = new CountDownLatch(1);
WriteRunnable wr = new WriteRunnable(cdl);
ReadRunnable rr = new ReadRunnable(cdl);
try {
wr.getPipedWriter().connect(rr.getPipedReader());
} catch (IOException e) {
System.out.println("通道启动连接失败" + e.getMessage());
}
new Thread(wr).start();
new Thread(rr).start();
}
static class WriteRunnable implements Runnable {
private PipedWriter pipedWriter = new PipedWriter();
private CountDownLatch cb;
public WriteRunnable(CountDownLatch cb) {
this.cb = cb;
}
public PipedWriter getPipedWriter() {
return this.pipedWriter;
}
@Override
public void run() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 102; i++) {
sb.append("0123456789");
}
try {
synchronized (this) {
System.out.println("----------writer start------------");
pipedWriter.write(sb.toString());
System.out.println("----------writer end ------------");
cb.countDown();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
static class ReadRunnable implements Runnable {
private PipedReader pipedReader = new PipedReader();
private CountDownLatch cb;
public ReadRunnable(CountDownLatch cb) {
this.cb = cb;
}
public PipedReader getPipedReader() {
return pipedReader;
}
@Override
public void run() {
try {
readMessageOnce();
} catch (BrokenBarrierException | InterruptedException e) {
e.printStackTrace();
}
}
// 从“管道输入流”中读取1次数据
private void readMessageOnce() throws BrokenBarrierException, InterruptedException {
cb.await();
System.out.println("......Read start...");
boolean readabled = false;
// 虽然buf的大小是2048个字符,但最多只会从“管道输入流”中读取1024个字符。
// 因为,“管道输入流”的缓冲区大小默认只有1024个字符。
while (! readabled) {
try {
if (pipedReader.ready()) {
char[] buf = new char[1024];
try {
int len = pipedReader.read(buf);
System.out.println(new String(buf, 0, len));
pipedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
readabled = true;
}
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
System.out.println("......Read end...");
}
}
}