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,关闭资源。
- import java.io.*;
- class CopyPic
- {
- public static void main(String[] args)
- {
- FileOutputStream fos = null;
- FileInputStream fis = null;
- try
- {
- fos = new FileOutputStream("e:\\2.bmp");
- fis = new FileInputStream("e:\\1.bmp");
- 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("写入关闭失败!");
- }
- }
- }
- }
演示mp3的复制,通过缓冲区。
BufferedOutputStream
BufferedInputStream
- import java.io.*;
- class CopyMp3
- {
- public static void main(String[] args) throws IOException
- {
- long start = System.currentTimeMillis();
- copy_2();
- long end = System.currentTimeMillis();
- System.out.println((end-start)+"毫秒");
- }
- public static void copy_2() throws IOException
- {
- MyBufferedInputStream bufis = new MyBufferedInputStream(new FileInputStream("e:\\1.mp3"));
- BufferedOutputStream bufos = new BufferedOutputStream(new FileOutputStream("e:\\3.mp3"));
- int by = 0;
- //System.out.println("第一个字节"+bufis.myRead());
- while((by=bufis.myRead())!=-1)
- {
- bufos.write(by);
- }
- bufis.myClose();
- bufos.close();
- }
- //通过字节流的缓冲区完成复制。
- public static void copy_1() throws IOException
- {
- BufferedInputStream bufis = new BufferedInputStream(new FileInputStream("e:\\1.mp3"));
- BufferedOutputStream bufos = new BufferedOutputStream(new FileOutputStream("e:\\2.mp3"));
- int by = 0;
- while((by=bufis.read())!=-1)
- {
- bufos.write(by);
- }
- bufis.close();
- bufos.close();
- }
- }
装饰和继承:
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
装饰模式比继承更灵活,避免了继承体系臃肿。
而且降低了类与类之间的关系。
装饰类因为增强以后对象,具备的功能和已有的是相同的,只不过提供了更强功能。
所以装饰类和被装饰类通常都属于同一个体系中。
当想要对已有对象惊醒功能增强时,
可以定义类,将以后对象传入,基于已有的功能,并提供加强功能。
那么自定义的该类称为装饰类。
装饰类通常会通过构造方法接收被装饰的对象。
并基于被装饰的对象的功能,提供更强的功能。
- class Person
- {
- public void chifan()
- {
- System.out.println("吃饭");
- }
- }
- class SuperPerson
- {
- private Person p;
- SuperPerson(Person p)
- {
- this.p = p;
- }
- public void superChifan()
- {
- System.out.println("开胃酒");
- p.chifan();
- System.out.println("甜点");
- System.out.println("来一根");
- }
- }
- class PersonDemo
- {
- public static void main(String[] args)
- {
- Person p = new Person();
- //p.chifan();
- SuperPerson sp = new SuperPerson(p);
- sp.superChifan();
- }
- }