07、字符流的缓冲区-BufferedWriter,BufferedReader
(1)缓冲区的出现提高了对数据的读写效率。
(2)对应类:BufferedWriter,BufferedReader
(3)缓冲区要结合流才可以使用。
(4)在流的甚而上对流的功能进行了增强。
(5)写入缓冲区BufferedWriter实现例子
import java.io.*;
class BufferedWriterDemo
{
public static void main(String[] args) throws IOException
{
//创建一个字符写入流对象
FileWriter fw = new FileWriter("aa.java");
//为了提高字符写入流效率,加入了缓冲技术。
BufferedWriter bufw = new BufferedWriter(fw);
bufw.write("abdcd");
bufw.flush();
//其实关闭缓冲区,就是在关闭缓冲区中的流对象
bufw.close();
}
}
(6)读取缓冲区BufferedReader实现例子
import java.io.*;
class BufferedReaderDemo
{
public static void main(String[] args) throws IOException
{
//创建一个字符读取流对象
FileReader fr = new FileReader("aa.java");
//为了提高字符写入流效率,加入了缓冲技术。
BufferedReader bufr = new BufferedReader(fr);
String line = null;
while ((line=bufr.readLine())!=null)
{
System.out.println(line);
}
//其实关闭缓冲区,就是在关闭缓冲区中的流对象
bufr.close();
}
}
(7)通过字符流缓冲区复制文本文件
import java.io.*;
class CopyTextByBuf
{
public static void main(String[] args)
{
Copy();
}
public static void Copy()
{
BufferedReader bufr = null;
BufferedWriter bufw = null;
try
{
bufr = new BufferedReader(new FileReader("CopyTextByBuf.java"));
bufw = new BufferedWriter(new FileWriter("复本CopyTextByBuf.java"));
String line = null;
while ((line=bufr.readLine())!=null)
{
bufw.write(line);
bufw.newLine();
}
}
catch (IOException e)
{
throw new RuntimeException("读写失败!");
}
finally
{
try
{
if(bufr!=null)
bufr.close();
}
catch (IOException e)
{
throw new RuntimeException("读取关闭失败!");
}
try
{
if(bufw!=null)
bufw.close();
}
catch (IOException e)
{
throw new RuntimeException("写入关闭失败!");
}
}
}
}
(8)模拟读取缓冲区BufferedReader
import java.io.*;
class MyBufferedReader
{
private FileReader r;
MyBufferedReader(FileReader r)
{
this.r = r;
}
//可以一次读一行数据的方法。
public String myReadLine() throws IOException
{
//定义一个临时容器。原BufferReader封装的是字符数组。
//为了演示方便,定义一个StringBuilder容器,因为最终还是把数据变成字符串。
StringBuilder sb = new StringBuilder();
int ch = 0;
while((ch=r.read())!=-1)
{
if(ch=='\r')
continue;
if(ch=='\n')
return sb.toString();
else
sb.append((char)ch);
}
if(sb.length()!=0)
return sb.toString();
return null;
}
public void Myclose()throws IOException
{
r.close();
}
}
class MyBufferedReaderDemo
{
public static void main(String[] args) throws IOException
{
FileReader fr = new FileReader("0663.txt");
MyBufferedReader myBuf = new MyBufferedReader(fr);
String line = null;
while ((line=myBuf.myReadLine())!=null)
{
System.out.println(line);
}
myBuf.Myclose();
}
}
08、装饰设计模式
(1)当想要对已有的对象进行功能增强时,可以定义类,将已有对象传入,基于已有的功能,并提供加强功能,那么自定义的该类称为装饰类。
(2)装饰类通常会通过构造方法接收衩装饰的并基于被装饰的对象的功能,提供更强的功能。
(3)装饰设计模式和继承的区别
——装饰模式比继承要灵活,避免了继承体系臃肿,而且降低了类于类之间的关系。
——装饰类因为增强已有对象,具备的功能和已有的是相同的,只不过提供了更强的功能,所以装饰类和被装饰类通常是属于一个体系中的。
(4)行号—LineNumberReader,模拟LineNumberReader的功能
import java.io.*;
class MyLineNumberReader extends BufferedReader
{
private int lineNumber;
MyLineNumberReader(Reader r)
{
super(r);
}
public String myReadLine() throws IOException
{
lineNumber++;
return super.readLine();
}
public void setLinewNumber(int lineNumber)
{
this.lineNumber = lineNumber;
}
public int getLineNumber()
{
return lineNumber;
}
}
09、字节流(InputStream、OutputStream)
需求:想要操作图片数据,这里就要用到字节流。
(1) 读取实例
import java.io.*;
class FileStream
{
public static void main(String[] args) throws IOException
{
readFile1();
}
//一个一个字节数组的读取
public static void readFile1() throws IOException
{
FileInputStream fis = new FileInputStream("0663.txt");
byte[] buf = new byte[1024];
int len = 0;
while ((len=fis.read(buf))!=-1)
{
System.out.println(new String(buf,0,len));
}
fis.close();
}
//一个一个字节的读取
public static void readFile() throws IOException
{
FileInputStream fis = new FileInputStream("0663.txt");
int ch = 0;
while ((ch=fis.read())!=-1)
{
System.out.print((char)ch);
}
fis.close();
}
public static void writeFile() throws IOException
{
FileOutputStream fos = new FileOutputStream("0663.txt");
fos.write("sldkfjewoifj".getBytes());
fos.close();
}
}
(2) 复制图片文件实例
/*
思路:
1,用字节讲到流和图片关联。
2,用字节写入流对象创建一个图片文件。用于存储获取到的图片数据。
3,通过循环读写,完成数据的存储。
4,关闭资源。
*/
import java.io.*;
class Copy_zj
{
public static void main(String[] args)
{
FileInputStream fis = null;
FileOutputStream fos = null;
try
{
fis = new FileInputStream("Koala.jpg");
fos = new FileOutputStream("Koala1.jpg");
byte[] buf = new byte[1024];
int len = 0;
while ((len=fis.read(buf))!=-1)
{
fos.write(buf,0,len);
}
}
catch (IOException e)
{
throw new RuntimeException("读写失败!");
}
finally
{
try
{
if(fis!=null)
fis.close();
}
catch (IOException e)
{
throw new RuntimeException("读取关闭失败");
}
try
{
if(fos!=null)
fos.close();
}
catch (IOException e)
{
throw new RuntimeException("写入关闭失败");
}
}
}
}
(3) 通过字节流的缓冲区完成非文本文件复制
import java.io.*;
class Copy3
{
public static void main(String[] args)
{
Copy();
}
public static void Copy()
{
BufferedInputStream bufr = null;
BufferedOutputStream bufw = null;
try
{
bufr = new BufferedInputStream(new FileInputStream("666888.flac"));
bufw = new BufferedOutputStream(new FileOutputStream("复本666888.flac"));
int by = 0;
while ((by=bufr.read())!=-1)
{
bufw.write(by);
}
}
catch (IOException e)
{
throw new RuntimeException("读写失败!");
}
finally
{
try
{
if(bufr!=null)
bufr.close();
}
catch (IOException e)
{
throw new RuntimeException("读取关闭失败!");
}
try
{
if(bufw!=null)
bufw.close();
}
catch (IOException e)
{
throw new RuntimeException("写入关闭失败!");
}
}
}
}
10、读取键盘录入
(1)操作的对象
System.out:对应的是标准输出设备,控制台。
System.in:对应的标准输入设备:键盘。
(2)实例
/*
需求:
通过键盘录入数据。
当录入一行数据后,就将该行数据进行打印。
如果录入的数据是over,那么停止录入。
*/
import java.io.*;
import java.lang .*;
class ReadIn
{
public static void main(String[] args) throws IOException
{
InputStream in = System.in;
StringBuilder sb = new StringBuilder();
while(true)
{
int ch = in.read();
if(ch=='\r')
continue;
if(ch=='\n')
{
String s = sb.toString();
if("over".equals(s))
break;
System.out.println(s.toUpperCase());
sb.delete(0,sb.length());
}else
sb.append((char)ch);
}
}
}
11、转换流
(1)将字节流对象转成字符流对象实例
import java.io.*;
class TransStreamDemo
{
public static void main(String[] args) throws IOException
{
//获取键盘录入对象
InputStream in = System.in;
//将字节流输入对象转成字符流输入对象
InputStreamReader isr = new InputStreamReader(in);
//为了提高效率,将字符串进行缓冲区技术高效操作。使用BufferedReader
BufferedReader bufr = new BufferedReader(isr);
//获取键盘录入对象
OutputStream out = System.out;
//将字节输出流对象转成字符流输出对象
OutputStreamWriter osw = new OutputStreamWriter(out);
BufferedWriter bufw = new BufferedWriter(osw);
String line = null;
while ((line=bufr.readLine())!=null)
{
if("over".equals(line))
break;
bufw.write(line.toUpperCase());
bufw.newLine();
bufw.flush();
}
bufr.close();
}
}
12、流操作的基本规律:最痛苦的就是流对象有很多,不知道该用哪一个。
通过两个明确来完成。
(1)、明确源和目的。
源:输入流。InputStream(字节) Reader(字符)
目的:输出流。OutputStream(字节) Writer(字符)
(2)操作的数据是否是纯文本。
是:字符流。
否:字节流。
(3)当体系明确后,在明确要使用哪个具体的对象。通过设备来进行区分:
源设备:内存,硬盘,键盘
目的设备:内存,硬盘,控制台
13、异常的日志信息,实例
import java.io.*;
import java.util.*;
import java.text.*;
class ExceptionInfo
{
public static void main(String[] args)
{
try
{
int[] arr =new int[2];
System.out.println(arr[3]);
}
catch (Exception e)
{
try
{
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
String s = sdf.format(d);
PrintStream ps = new PrintStream("exeception.log");
ps.println(s);
System.setOut(ps);
}
catch (IOException ex)
{
throw new RuntimeException("创建日志文件失败!");
}
e.printStackTrace(System.out);
}
}
}