IO(Input Output)流
IO流应用:
<span style="font-family:SimSun;"><span style="font-size:18px;">import java.io.*;
class FileWriterDemo
{
public static void main(String[] args) throws IOException
{
</span><span style="font-size:14px;color:#333333;">FileWriter fw=new FileWriter("Demo.txt");//创建流对象,传入要操作的文件</span><span style="font-size:18px;">
fw.write("haha");//向文件内写入数据
fw.flush();//<span style="font-family: SimSun;font-size:18px; line-height: 21px;">刷新流对象中的缓冲中的数据,将数据存到目的地中</span></span></span>
<span style="font-family:SimSun;"><span style="font-size:18px;"><span style="white-space:pre"> </span>fw.close();//关闭流对象
}
}
</span></span>
例二:创建一个文件读取流对象<span style="font-family:SimSun;font-size:14px;">import java.io.*;
class FileReaderDemo
{
public static void main(String[] args)
{
FileReader fr=null;
try
{
fr=new FileReader("Demo.txt");//<span style="line-height: 21px;">创建一个文件读取流对象,和指定名称的文件相关联</span>
int ch=0;
while((ch=fr.read())!=-1)
{
System.out.println("ch:"+(char)ch);
}
}
catch (IOException e)
{
System.out.println(e.toString());
}</span>
<span style="font-family:SimSun;font-size:14px;"><span style="white-space:pre"></span></span><pre name="code" class="java" style="line-height: 21px;"><span style="white-space:pre"> </span>//关闭流对象
finally{try{if(fr!=null)fr.close();}catch (IOException e){System.out.println(e.toString());}}}}
二、缓冲区
<span style="font-size:18px;">class BufferedDemo1
{
public static void main(String[] args)throws IOException
{
//创建一个读取流对象和文件相关联
FileReader fr=new FileReader("buf.txt");
//为了提高效率,加入缓冲技术,将字符读取流对象作为参数传递给缓冲构造函数
BufferedReader bfr=new BufferedReader(fr);
String line=null;
while((line=bfr.readLine())!=null)
{
System.out.println("line:"+line);
}
bfr.close();
}
}</span>
三、装饰设计模式
1、简述
当想对已有对象进行功能增强时,可定义类:将已有对象传入,基于已有对象的功能,并提供加强功能,那么自定义的该类称之为装饰类。
2、特点
装饰类通常都会通过构造方法接收被装饰的对象,并基于被装饰的对象的功能,提供更强的功能。
3、装饰和继承的区别:
1)装饰模式比继承要灵活。避免了继承体系的臃肿,且降低了类与类之间的关系。
2)装饰类因为增强已有对象,具备的功能和已有的是相同的,只不过提供了更强的功能,所以装饰类和被装饰的类通常都是属于一个体系。
3)从继承结构转为组合结构。
MyReader//专门用于读取数据的类
注:在定义类的时候,不要以继承为主;可通过装饰设计模式进行增强类功能。灵活性较强,当装饰类中的功能不适合,可再使用被装饰类的功能。
四 、字节流
一、概述
1、字节流和字符流的基本操作是相同的,但字节流还可以操作其他媒体文件。
2、由于媒体文件数据中都是以字节存储的,所以,字节流对象可直接对媒体文件的数据写入到文件中,而可以不用再进行刷流动作。
3、读写字节流:InputStream 输入流(读)
OutputStream 输出流(写)
4、为何不用进行刷流动作:
因为字节流操作的是字节,即数据的最小单位,不需要像字符流一样要进行转换为字节。所以可直接将字节数据写入到指定文件中。
5、InputStream特有方法:
int available();//返回文件中的字节个数
实例1、<span style="font-family:SimSun;font-size:18px;">class CopyMp3 {
public static void main(String[] args) throws IOException {
method_2();
}
public static void method_2() throws IOException {
FileInputStream fis = new FileInputStream("d://11.mp3");
FileOutputStream fos = new FileOutputStream("d://1234.mp3");
BufferedInputStream bfis = new BufferedInputStream(fis);
BufferedOutputStream bfos = new BufferedOutputStream(fos);
byte[] buf = new byte[1024 * 1024 * 4];
int len = 0;
while ((len = bfis.read(buf)) != -1) {
bfos.write(buf, 0, len);
}
bfis.close();
bfos.close();
}
}</span>
五、总结
<span style="font-size:18px;">需求:
把键盘录入文件中
字符缓冲=new 字符缓冲(new 字节流转换字符流(new 字节流()))
import java.io.*;
class SystemIODemo
{
public static void main(String[] args)throws IOException
{
BufferedReader bfr=new BufferedReader(new InputStreamReader(System.in));//键盘录入
BufferedWriter bfw=new BufferedWriter(new FileWriter("a.txt"));//写到目的地
//BufferedWriter bfw=
//new BufferedWriter(new OutputStreamWriter(new FileOutputStream("d://java0809//a.txt")));
String line=null;
while((line=bfr.readLine())!=null)
{
if("over".equals(line))
break;
bfw.write(line);
bfw.newLine();
bfw.flush();
}
bfr.close();
bfw.close();
}
}</span>