缓冲技术是为了提高数据的读写效率而提出的。
(1)字符流的缓冲读
在字符流的缓冲技术中提供了一个newLine()方法,这个方法是跨平台的
在读数据的时候采用读完直接刷新的方式可以保证断电后数据不会丢失
package com.songyan.bufferedwriter;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class Demo1 {
public static void main(String[] args) throws IOException {
FileWriter writer=new FileWriter("demo1.txt");
BufferedWriter bwriter=new BufferedWriter(writer);
for(int i=0;i<5;i++)
{
bwriter.write("annnn");
//newLine()是一个跨平台的换行符--》这个方法只有缓冲区中才有
bwriter.newLine();
//写一次刷新一次才能保证断电数据不丢失
bwriter.flush();
}
//缓冲区关闭其实就是流的关闭:看源码中,在缓冲区的关闭操作中关闭了流
if(bwriter!=null)
bwriter.close();
}
}
(2)字符流的缓冲写
在使用缓冲区进行读数据的时候,读一行的操作读到结尾的时候返回null
package com.songyan.bufferedreader;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
/**
* Buffered提供了读取一行的操作,当读取到结尾时返回null
* @author Administrator
*
*/
public class Demo1 {
public static void main(String[] args) throws IOException {
FileReader reader=new FileReader("demo1.txt");
BufferedReader breader=new BufferedReader(reader);
String line="";
while((line=breader.readLine())!=null)
{
System.out.println(line);
}
breader.close();
}
}
package com.songyan.bufferedreader;
import java.awt.BufferCapabilities;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
public class Demo2 {
/**
* 通过缓冲区复制一个.java 文件
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
BufferedWriter bwriter;
FileReader reader=null;
FileWriter writer=null;
BufferedReader breader=null;
bwriter =null;
try {
reader=new FileReader("src/com/songyan/bufferedreader/Demo2.java");
writer=new FileWriter("Demo2.java");
breader=new BufferedReader(reader);
bwriter = new BufferedWriter(writer);
String line="";
while((line=breader.readLine())!=null)
{
bwriter.write(line);
bwriter.newLine();
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(bwriter!=null)
bwriter.flush();
if(breader!=null)
bwriter.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
package com.songyan.bufferedreader;
import java.awt.BufferCapabilities;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
public class Demo3 {
/**
* 通过缓冲区复制一个.java 文件
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
BufferedWriter bwriter;
FileReader reader=null;
FileWriter writer=null;
BufferedReader2 breader=null;
bwriter =null;
try {
reader=new FileReader("src/com/songyan/bufferedreader/Demo2.java");
writer=new FileWriter("Demo3.java");
breader=new BufferedReader2(reader);
bwriter = new BufferedWriter(writer);
String line="";
while((line=breader.readLine())!=null)
{
bwriter.write(line);
bwriter.newLine();
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(bwriter!=null)
bwriter.flush();
if(breader!=null)
bwriter.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
/**
* 自己实现ReadLine()方法
* 这个方法实际上是对FileReader类中read()方法的增强
* 这里使用到了装饰设计模式
* 定义一个被装饰者的对象作为成员变量
* 对其某个方法进行增强
* 装饰类通常会通过构造函数将被装饰对象传入
* 通常装饰对象与被装饰对象继承自同一个类或者实现同一个接口
* @author Administrator
*
*/
class BufferedReader2 extends Reader{
/**
* 装饰设计模式与继承的区别:
* 装饰设计模式比继承更灵活,避免了继承体系的臃肿,降低了雷雨类之间的联系
*
* 装饰类因为是增强原有对象的已有功能,所以装饰类通常与被装饰类同属于一个体系
*
*/
private Reader reader;
public String readLine() throws IOException
{
StringBuilder sb=new StringBuilder();
int ch=0;
while((ch=reader.read())!=-1)
{
if((char)ch=='\r')
continue;
if((char)ch=='\n')
return sb.toString();
sb.append((char)ch);
}
return null;
}
public BufferedReader2(Reader reader) {
this.reader=reader;
}
@Override
public int read(char[] cbuf, int off, int len) throws IOException {
return reader.read(cbuf, off, len);
}
@Override
public void close() throws IOException {
reader.close();
}
}
package com.songyan.lineumberbufferedreader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.io.Reader;
public class Demo1 {
public static void main(String[] args) throws IOException {
MyLineNumberReader lnReader=new MyLineNumberReader(new FileReader("src/com/songyan/bufferedreader/Demo1.java"));
String line="";
lnReader.setLineNumber(100);
while((line=lnReader.readLine())!=null)
{
System.out.println(lnReader.getLineNumber()+":"+line);
}
}
}
class MyLineNumberReader extends Reader{
private Reader reader;
private int lineNumber=0;
public MyLineNumberReader(Reader reader)
{
this.reader=reader;
}
public int getLineNumber() {
return lineNumber;
}
public String readLine() throws IOException {
lineNumber++;
StringBuilder sb=new StringBuilder();
int ch;
while((ch= reader.read())!=-1)
{
if(((char)ch)=='\r')
continue;
if((char)ch=='\n')
return sb.toString();
sb.append((char)ch);
}
return null;
}
public void setLineNumber(int i) {
this.lineNumber=i;
}
@Override
public int read(char[] cbuf, int off, int len) throws IOException {
return reader.read(cbuf, off, len);
}
@Override
public void close() throws IOException {
reader.close();
}
}
(3)装饰类LineNumberReader
这里用到的装饰设计模式,对Reader的read()方法进行修饰,使其功能更加强大。
package com.songyan.lineumberbufferedreader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.LineNumberReader; import java.io.Reader; public class Demo1 { public static void main(String[] args) throws IOException { MyLineNumberReader lnReader=new MyLineNumberReader(new FileReader("src/com/songyan/bufferedreader/Demo1.java")); String line=""; lnReader.setLineNumber(100); while((line=lnReader.readLine())!=null) { System.out.println(lnReader.getLineNumber()+":"+line); } } } class MyLineNumberReader extends Reader{ private Reader reader; private int lineNumber=0; public MyLineNumberReader(Reader reader) { this.reader=reader; } public int getLineNumber() { return lineNumber; } public String readLine() throws IOException { lineNumber++; StringBuilder sb=new StringBuilder(); int ch; while((ch= reader.read())!=-1) { if(((char)ch)=='\r') continue; if((char)ch=='\n') return sb.toString(); sb.append((char)ch); } return null; } public void setLineNumber(int i) { this.lineNumber=i; } @Override public int read(char[] cbuf, int off, int len) throws IOException { return reader.read(cbuf, off, len); } @Override public void close() throws IOException { reader.close(); } }