IO流:
对二进制格式数据进行读写操作的流(字节流):
两个抽象基类:InputStream和OutputStream 仅仅读写单个字节和字节数组,要想读写字符串、数值等类型就要到他们的子类。
对Unicode字符进行读写操作的的流(字符流):
两个抽象基类:Reader和Wiriter ,读写Unicode字符单元(0~65536(2^16)的整数),要读写其他类型的数据,也是用到他们的子类。
以上四个抽象类组成了流“动物园”。
垃圾回收器只能回收java程序产生的那些类实例对象,不能回收系统产生的资源(比如流),所以要自己调用close()方法释放资。
4个接口:Closeable、Flushable、Readable、Appendable---(java.lang包下):
InputStream、OutputStream、Reader、Writer实现了Colseable接口,OutputStream和Writer实现了Flushable接口。
Readable接口只有一个方法:int read(CharBuffer cb) CharBuffer类具有能够顺序读写和随机读写的方法,它能够表示一个内存缓冲区或者一个内存映射文件。
Appendable接口有两个方法,追加一个字符或者字符序列:
Appendable append(char); Appendable append(CharSequence s);
CharSequence是另外一个接口,它描述了一个char值序列的最基本属性,并被String、CharBuffer、StringBuffer/StringBuilder加以实现。
在流“动物园”中,只有Writer实现了Appendable接口
InputStream和OutputStream的子类:
文件流: FileInputStream fis=new FileInputStream("文件路径");(方式一)
File f=new File("文件路径");FileInputStream fin=new FileInputStream(f);(方式二)
FileOutputStream fos=new FileOutputStream();
fos.write(int b)方法只是将字节写入了内存缓冲区,并没有写到文件当中去,fos.close()之后才真正写入文件中去。
fos.write(byte[] b)方法内部调用了flush()方法,所以不用fos.close()就会写入到文件中
小提示:System.getProperty("user.dir") (获取当前目录)
数据流: DataInputStream dis=new DataInputStream(); dis.readXXX();
DataOutputStream dos=new DataOutputStream();dos.writeXXX();
对文件进行缓冲和数据输入操作:
DataInputStream dis=new DataInputStream(
new BufferdInputStream(
new FileInputStream("文件路径")));
管道流: 用于在应用程序中的创建管道通信,使用管道流可以实现各程序模块之间的松耦合通信:
PipedInputStream pis=new PipedInputStream();
PipedOutputStream pos=new PipedOutputStream();
pos.connect(pis);或者pis.connect(pos);
pis.start();pos.start();
字节数组流:用于以IO流的方式来完成字节数组内容的读写,形成内存缓冲区,来支持类似内存虚拟文件或者内存映像文件的功能:
ByteArrayInputStream(byte[] buff)
ByteArrayInputStream(byte[] buff,int offset,int length)
ByteArrayOutputStream()
ByteArrayOutputStream(int)
ZIP流: ZipInputStream zis=new ZipInputStream(new FileInputStream("xxx.zip"));
DataInputStream dis=new DataInputStream(zis);
随机存取文件流:
RandomAccessFile类在随机(相对顺序而言)读写等长记录格式的文件时有很大优势。
RandomAccessFile类仅限于操作文件,不能访问其他的IO设备,如网络、内存映像等。
四种构造方法:
new RandomAccessFile(f,"rw");// 读写模式
new RandomAccessFile(f,"r");// 只读模式
new RandomAccessFile(f,"rws");//每次更新采用同步方式将数据或元数据写入磁盘的读写模式
new RandomAccessFile(f,"rwd");//采用同步模式将数据写入磁盘的读写模式
long getFilePointer()返回文件指针的当前位置
void seek(long pos)将文件指针的位置设定为文件开始的pos字节处
long length()返回文件的长度
Reader和Writer的子类:
文本流:InputStreamReader isr=new InputStreamReader(new FileInputStream("xxx.dat"),"ISO8859-1");
读取器/写入器(把字节转换成Unicode字符):
FileReader fr=new FileReader("xxx.txt");
FileWriter fw=new FileWriter("xxx.txt")相当于 FileWriter fw=new FileWriter(new FileOutputStream("xxx.txt"));
fw.write(String str)跟上边一样,要想写入文件中去,必须fw.close()
文本管道流:PipedReader pr=new PipedReader();
PipedWriter pw=new PipedWriter();
pr.connect(pw);或pw.connect(pr);
pr.start();pw.start();
字符串字符流:StringReader和StringWriter
PrintWriter pw=new PrintWriter(new FileWriter("xxx.txt"),true)//autoflush
pw.println()打印一个字符串,如果处于自动刷新模式,则刷新这个流
文本输入:
我们知道:当以二进制格式写入数据时,应当使用DataOutputStream
当以文本格式写入数据时,应当使用PrintWriter
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
BufferedReader br=new BufferedReader(new InputStreamReader(url.openStream));
String s=br.readLine()
IO程序的复用:
System.in连接到键盘,是InputStream类型的对象。System.out连接到显示器,是printpStream类型的对象。
不管各种底层物理设备用什么方式实现数据的终止时,InputStream的read方法总是返回-1来表示输入流的结束。
在Windows下,按下Ctrl+Z组合键可以产生键盘输入流的结束标记,在linux下则是按下Ctrl+D组合键来产生输入流的结束标记。