IO输入和输出

1. java.io.File
用于表示文件(目录);只用于表示文件(目录)
的信息(名称,大小等)不能对文件的内容进行访问
2. java.io.File基本API


File(String)
long length()文件大小
long lastModified()最后修改时间
String getName()
String getPath()获得路径
String getAbsolutePath()得到绝对路径
boolean exists()是否存在
boolean dir.isFile()是否是文件
boolean dir.isDirectory()是否是目录
boolean mkdir()创建文件夹
boolean mkdirs()创建此抽象路径名指定的目录,包括所有必需但不存在的父目录
boolean delete();删除
boolean createNewFile()创建新文件 throw IOException
File[] listFile()显示目录的内容


3. 回调模式和FileFilter(从以下版本开始:1.2)
File[] listFile(FileFilter)
接口 FilenameFilter(从以下版本开始:JDK1.0)


4. java.io.RandomAccessFile
可以访问(读/写)一个文件中任意位置的字节信息
// arg0 要访问的文件
// arg1 访问的模式 "r" 只读模式,"rw" 读写模式
RandomAccessFile(File, String) 
   throws FileNotFoundException
RAF 维护一个指针,指向要读写的位置
指针会随着读写自动后移
int read()
void write(int)写出后八位,前面舍掉
void writeInt(int)
seek(long) 寻求
long getFilePointer() 获得当前文件指针




// 读取尽可能多的字节,填充作为参数的字节数组
int read(byte[])读取数据到缓冲区
write(byte[], int off, int len)把缓冲器数据写入,从off开始,len个写入


5. 
java.io.InputStream  输入流
InputStream抽象类
java.io.OutputStream 输出流


java.io.FileInputStream(File)    FIS
java.io.FileOutputStream(File)   FOS


java.io.BufferedInputStream(InputStream)  BIS
java.io.BufferedOutputStream(OutputStream) BOS


GBK(GB2312子集)一个汉字两个字节 中:d6 d0 A:41
UTF-16BE 定长 都是十六位 中:4e 2d A:00 41
UTF-8 三个字节


IO 
流(Stream): 将数据Byte By Byte的处理方式
InputStream 
|-- 节点流 (流开始的地方, 流的源泉)
|    |-- FileInputStream  
|    |-- ByteArrayInputStrem
|-- 过滤流
|    |-- BufferedInputStream 
|    |-- ObjectInputStream
|    |-- ZipInputStream
|    |-- InputStreamReader

OutputStream
|-- 节点流 (流结束的地方, 流的目的)
|    |-- FileOutputStream
|    |-- ByteArrayOutputStream
|-- 过滤流
|    |-- BufferedOutputStream
|    |-- ObjectOutputStream
|    |-- ZipOutputStream
|    |-- OutputStreamWriter


文本的处理(byte<->char, 编码转换)
Reader
  |-- InputStreamReader 核心 处理(byte-解码->char)
  |   如:  GBK(byte)-->Java Unicode(char)
  |-- FileReader=InputStreamReader+FileInputStream
  |-- BufferedReader 提供了readLine()


Writer
  |-- OutputStreamWriter 核心 处理(char-编码->byte) 
  |   如:  Java Unicode(char) --> GBK(byte)
  |-- FileWriter=OutputStreamWriter+FileOutputStream
  |-- PrintWriter 非常常用, 有println 方法




复制一个图片
思路:
1,用字节读取流对象和图片的关联。
2,用字节写入流对象创建一个图片文件,用于存储获取到的图片数据。
3,通过循环读写,完成数据的存储。
4,关闭资源。


[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. import java.io.*;  
  2. class CopyPic   
  3. {  
  4.     public static void main(String[] args)   
  5.     {  
  6.         FileOutputStream fos = null;  
  7.         FileInputStream fis = null;  
  8.         try  
  9.         {  
  10.             fos = new FileOutputStream("e:\\2.bmp");  
  11.             fis = new FileInputStream("e:\\1.bmp");  
  12.             byte[] buf = new byte[1024];  
  13.             int len = 0;  
  14.             while((len=fis.read(buf))!=-1)  
  15.             {  
  16.                 fos.write(buf,0,len);  
  17.             }  
  18.   
  19.         }  
  20.         catch (IOException e)  
  21.         {  
  22.             throw new RuntimeException("复制文件失败!");  
  23.         }  
  24.         finally  
  25.         {  
  26.             try  
  27.             {  
  28.                 if(fis!=null)  
  29.                     fis.close();  
  30.             }  
  31.             catch (IOException e)  
  32.             {  
  33.                 throw new RuntimeException("读取关闭失败!");  
  34.             }  
  35.             try  
  36.             {  
  37.                 if(fos!=null)  
  38.                     fos.close();  
  39.             }  
  40.             catch (IOException e)  
  41.             {  
  42.                 throw new RuntimeException("写入关闭失败!");  
  43.             }  
  44.         }  
  45.   
  46.     }  
  47. }  



演示mp3的复制,通过缓冲区。
BufferedOutputStream
BufferedInputStream


[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. import java.io.*;  
  2. class CopyMp3  
  3. {  
  4.     public static void main(String[] args) throws IOException  
  5.     {  
  6.         long start = System.currentTimeMillis();  
  7.         copy_2();  
  8.         long end = System.currentTimeMillis();  
  9.         System.out.println((end-start)+"毫秒");  
  10.     }  
  11.   
  12.     public static void copy_2() throws IOException  
  13.     {  
  14.         MyBufferedInputStream bufis = new MyBufferedInputStream(new FileInputStream("e:\\1.mp3"));  
  15.         BufferedOutputStream bufos = new BufferedOutputStream(new FileOutputStream("e:\\3.mp3"));  
  16.           
  17.         int by = 0;  
  18.   
  19.         //System.out.println("第一个字节"+bufis.myRead());  
  20.   
  21.         while((by=bufis.myRead())!=-1)  
  22.         {  
  23.             bufos.write(by);  
  24.         }  
  25.   
  26.         bufis.myClose();  
  27.         bufos.close();  
  28.     }  
  29.   
  30.     //通过字节流的缓冲区完成复制。  
  31.     public static void copy_1() throws IOException  
  32.     {  
  33.         BufferedInputStream bufis = new BufferedInputStream(new FileInputStream("e:\\1.mp3"));  
  34.         BufferedOutputStream bufos = new BufferedOutputStream(new FileOutputStream("e:\\2.mp3"));  
  35.           
  36.         int by = 0;  
  37.         while((by=bufis.read())!=-1)  
  38.         {  
  39.             bufos.write(by);  
  40.         }  
  41.   
  42.         bufis.close();  
  43.         bufos.close();  
  44.     }  
  45. }  


装饰和继承:


MyReader//专门用于读取数据的类。
|--MyTextReader
|--MyBufferTextReader
|--MyMediaReader
|--MyBufferMediaReader
|--MyDataReader
|--MyBufferDataReader


class MyBufferReader
{
MyBufferReader(MyTextReader text)
{}
MyBufferReader(MyMediaReader media)
{}
}
上面这个类扩展性很差。
找到其参数的共同类型,通过多态的形式,可以提高扩展性。


class MyBufferReader extends MyReader
{
private MyReader r;
MyBufferReader(MyReader r)
{}
}


MyReader//专门用于读取数据的类。
|--MyTextReader
|--MyMediaReader
|--MyDataReader
|--MyBufferReader


装饰模式比继承更灵活,避免了继承体系臃肿。
而且降低了类与类之间的关系。


装饰类因为增强以后对象,具备的功能和已有的是相同的,只不过提供了更强功能。
所以装饰类和被装饰类通常都属于同一个体系中。


当想要对已有对象惊醒功能增强时,
可以定义类,将以后对象传入,基于已有的功能,并提供加强功能。
那么自定义的该类称为装饰类。


装饰类通常会通过构造方法接收被装饰的对象。
并基于被装饰的对象的功能,提供更强的功能。


[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. class Person  
  2. {  
  3.     public void chifan()  
  4.     {  
  5.         System.out.println("吃饭");  
  6.     }  
  7. }  
  8.   
  9. class SuperPerson  
  10. {  
  11.     private Person p;  
  12.     SuperPerson(Person p)  
  13.     {  
  14.         this.p = p;  
  15.     }  
  16.     public void superChifan()  
  17.     {  
  18.         System.out.println("开胃酒");  
  19.         p.chifan();  
  20.         System.out.println("甜点");  
  21.         System.out.println("来一根");  
  22.   
  23.     }  
  24. }  
  25.   
  26. class PersonDemo   
  27. {  
  28.     public static void main(String[] args)   
  29.     {  
  30.         Person p = new Person();  
  31.         //p.chifan();  
  32.         SuperPerson sp = new SuperPerson(p);  
  33.         sp.superChifan();  
  34.   
  35.     }  
  36. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值