I/O java学习 Lesson7
2012-05-27 15:07
225 查看
File类
一个File类的对象,表示磁盘上的文件或目录。
File类提供了与平台无关的方法来对磁盘上的文件或目录进行操作。
流式I/O
流(Stream)是字节的源或目的。
两种基本的流是:输入流(Input Stream)和输出流(Output Stream)。可以从中读出一系列字节对象称为输入流。而能向其中写入一些列字节的对象称为输出流。
流的分类
节点流:从特定的地方读写的流类,例如:磁盘或一块内存区域。
过滤流:使用节点流作为输入或输出。过滤流是使用一个已经存在的输入流或输出流连接创建的。
InputStream
三个基本的读方法
abstract int read() :读取一个字节数据,并返回读到的数据,如果返回-1,表示读到输入流的末尾。
int read(byte[] b) :将数据读入一个字节数组,同时返回实际读取的字节数。如果返回-1,表示读到输入流的末尾。
int read(byte[] b, int off, int len):将数据读入一个字节数组,同时返回实际读取的字节数。如果返回-1,表示读到输入流的末尾。off指定在数组b中存放数据的起始偏移位置;len指定读取的最大字节数。
其他方法
long skip(long n):在输入流中跳过n个字节,并返回实际跳过的字节数。
int available():返回在不发生阻塞的情况下,可读取的字节数。
void close():关闭输入流,释放和这个留相关的系统资源。
void mark(int readLimit):在输入流的当前位置放置一个标记,如果读取的字节数多于readLimit设置的值,则忽略这个标记。
void reset():返回到上一个标记。
boolean markSupported():测试当前流是否支持mark和reset方法。如果支持,返回true,否则返回false。
OutputStream
三个基本的写方法
abstract void write(int b) :往输出流中写入一个字节。
void write(byte[] b) :往输出流中写入数组b中的所有字节。
void write(byte[] b, int off, int len) :往输出流中写入数组b中从偏移量off开始的len个字节的数据。
其它方法
void flush() :刷新输出流,强制缓冲区中的输出字节被写出。
void close() :关闭输出流,释放和这个流相关的系统资源。
基本的流类
FileInputStream和FileOutputStream
节点流,用于从文件中读取或往文件中写入字节流。如果在构造FileOutputStream时,文件已经存在,则覆盖这个文件。
BufferedInputStream和BufferedOutputStream
过滤流,需要使用已经存在的节点流来构造,提供带缓冲的读写,提高了读写的效率。
DataInputStream和DataOutputStream
过滤流,需要使用已经存在的节点流来构造,提供了读写Java中的基本数据类型的功能。
PipedInputStream和PipedOutputStream
管道流,用于线程间的通信。一个线程的PipedInputStream对象从另一个线程的PipedOutputStream对象读取输入。要使管道流有用,必须同时构造管道输入流和管道输出流。
Java I/O库的设计原则
Java的I/O库提供了一个称作链接的机制,可以将一个流与另一个流首尾相接,形成一个流管道的链接。这种机制实际上是一种被称为Decorator(装饰)设计模式的应用。
通过流的链接,可以动态的增加流的功能,而这种功能的增加是通过组合一些流的基本功能而动态获取的。
要获取一个I/O对象,往往需要产生多个I/O对象,这也是Java I/O不太容易掌握的原因,但在I/O库中Decorator模式的运用,提供了实现上的灵活性。
Reader和Writer
Java程序语言使用Unicode来表示字符串和字符。
Reader和Writer这两个抽象类主要用来读写字符流。
Reader的类层次
Writer的类层次
字符集的编码
ASCII(American Standard Code for Information Interchange,美国信息互换标准代码),是基于常用的英文字符的一套电脑编码系统。我们知道英文中经常使用的字符、数字符号被计算机处理时都是以二进制码的形式出现的。这种二进制码的集合就是所谓的ASCII码。每一个ASCII码与一个8位(bit)二进制数对应。其最高位是0,相应的十进制数是0-127。如,数字“0”的编码用十进制数表示就是48。另有128个扩展的ASCII码,最高位都是1,由一些制表符和其它符号组成。ASCII是现今最通用的单字节编码系统。
GB2312:GB2312码是中华人民共和国国家汉字信息交换用编码,全称《信息交换用汉字编码字符集-基本集》。主要用于给每一个中文字符指定相应的数字,也就是进行编码。一个中文字符用两个字节的数字来表示,为了和ASCII码有所区别,将中文字符每一个字节的最高位置都用1来表示。
GBK:为了对更多的字符进行编码,国家又发布了新的编码系统GBK(GBK的K是“扩展”的汉语拼音第一个字母)。在新的编码系统里,除了完全兼容GB2312 外,还对繁体中文、一些不常用的汉字和许多符号进行了编码。
ISO-8859-1:是西方国家所使用的字符编码集,是一种单字节的字符集 ,而英文实际上只用了其中数字小于128的部分。
Unicode:这是一种通用的字符集,对所有语言的文字进行了统一编码,对每一个字符都用2个字节来表示,对于英文字符采取前面加“0”字节的策略实现等长兼容。如 “a” 的ASCII码为0x61,UNICODE就为0x00,0x61。
UTF-8:Eight-bit UCS Transformation Format,(UCS,Universal Character Set,通用字符集,UCS 是所有其他字符集标准的一个超集)。一个7位的ASCII码值,对应的UTF码是一个字节。如果字符是0x0000,或在0x0080与0x007f之间,对应的UTF码是两个字节,如果字符在0x0800与0xffff之间,对应的UTF码是三个字节。
RandomAccessFile
RandomAccessFile类同时实现了DataInput和DataOutput接口,提供了对文件随机存取的功能,利用这个类可以在文件的任何位置读取或写入数据。
RandomAccessFile类提供了一个文件指针,用来标志要进行读写操作的下一数据的位置。
对象序列化
将对象转换为字节流保存起来,并在日后还原这个对象,这种机制叫做对象序列化。
将一个对象保存到永久存储设备上称为持续性。
一个对象要想能够实现序列化,必须实现Serializable接口或Externalizable接口。
当一个对象被序列化时,只保存对象的非静态成员变量,不能保存任何的成员方法和静态的成员变量。
如果一个对象的成员变量是一个对象,那么这个对象的数据成员也会被保存。
如果一个可序列化的对象包含对某个不可序列化的对象的引用,那么整个序列化操作将会失败,并且会抛出一个NotSerializableException。我们可以将这个引用标记为transient,那么对象仍然可以序列化。
FileTest
ObjectSerialTest
PipedStreamTest
StreamTest
CharsetTest
一个File类的对象,表示磁盘上的文件或目录。
File类提供了与平台无关的方法来对磁盘上的文件或目录进行操作。
流式I/O
流(Stream)是字节的源或目的。
两种基本的流是:输入流(Input Stream)和输出流(Output Stream)。可以从中读出一系列字节对象称为输入流。而能向其中写入一些列字节的对象称为输出流。
流的分类
节点流:从特定的地方读写的流类,例如:磁盘或一块内存区域。
过滤流:使用节点流作为输入或输出。过滤流是使用一个已经存在的输入流或输出流连接创建的。
InputStream
三个基本的读方法
abstract int read() :读取一个字节数据,并返回读到的数据,如果返回-1,表示读到输入流的末尾。
int read(byte[] b) :将数据读入一个字节数组,同时返回实际读取的字节数。如果返回-1,表示读到输入流的末尾。
int read(byte[] b, int off, int len):将数据读入一个字节数组,同时返回实际读取的字节数。如果返回-1,表示读到输入流的末尾。off指定在数组b中存放数据的起始偏移位置;len指定读取的最大字节数。
其他方法
long skip(long n):在输入流中跳过n个字节,并返回实际跳过的字节数。
int available():返回在不发生阻塞的情况下,可读取的字节数。
void close():关闭输入流,释放和这个留相关的系统资源。
void mark(int readLimit):在输入流的当前位置放置一个标记,如果读取的字节数多于readLimit设置的值,则忽略这个标记。
void reset():返回到上一个标记。
boolean markSupported():测试当前流是否支持mark和reset方法。如果支持,返回true,否则返回false。
OutputStream
三个基本的写方法
abstract void write(int b) :往输出流中写入一个字节。
void write(byte[] b) :往输出流中写入数组b中的所有字节。
void write(byte[] b, int off, int len) :往输出流中写入数组b中从偏移量off开始的len个字节的数据。
其它方法
void flush() :刷新输出流,强制缓冲区中的输出字节被写出。
void close() :关闭输出流,释放和这个流相关的系统资源。
基本的流类
FileInputStream和FileOutputStream
节点流,用于从文件中读取或往文件中写入字节流。如果在构造FileOutputStream时,文件已经存在,则覆盖这个文件。
BufferedInputStream和BufferedOutputStream
过滤流,需要使用已经存在的节点流来构造,提供带缓冲的读写,提高了读写的效率。
DataInputStream和DataOutputStream
过滤流,需要使用已经存在的节点流来构造,提供了读写Java中的基本数据类型的功能。
PipedInputStream和PipedOutputStream
管道流,用于线程间的通信。一个线程的PipedInputStream对象从另一个线程的PipedOutputStream对象读取输入。要使管道流有用,必须同时构造管道输入流和管道输出流。
Java I/O库的设计原则
Java的I/O库提供了一个称作链接的机制,可以将一个流与另一个流首尾相接,形成一个流管道的链接。这种机制实际上是一种被称为Decorator(装饰)设计模式的应用。
通过流的链接,可以动态的增加流的功能,而这种功能的增加是通过组合一些流的基本功能而动态获取的。
要获取一个I/O对象,往往需要产生多个I/O对象,这也是Java I/O不太容易掌握的原因,但在I/O库中Decorator模式的运用,提供了实现上的灵活性。
Reader和Writer
Java程序语言使用Unicode来表示字符串和字符。
Reader和Writer这两个抽象类主要用来读写字符流。
Reader的类层次
Writer的类层次
字符集的编码
ASCII(American Standard Code for Information Interchange,美国信息互换标准代码),是基于常用的英文字符的一套电脑编码系统。我们知道英文中经常使用的字符、数字符号被计算机处理时都是以二进制码的形式出现的。这种二进制码的集合就是所谓的ASCII码。每一个ASCII码与一个8位(bit)二进制数对应。其最高位是0,相应的十进制数是0-127。如,数字“0”的编码用十进制数表示就是48。另有128个扩展的ASCII码,最高位都是1,由一些制表符和其它符号组成。ASCII是现今最通用的单字节编码系统。
GB2312:GB2312码是中华人民共和国国家汉字信息交换用编码,全称《信息交换用汉字编码字符集-基本集》。主要用于给每一个中文字符指定相应的数字,也就是进行编码。一个中文字符用两个字节的数字来表示,为了和ASCII码有所区别,将中文字符每一个字节的最高位置都用1来表示。
GBK:为了对更多的字符进行编码,国家又发布了新的编码系统GBK(GBK的K是“扩展”的汉语拼音第一个字母)。在新的编码系统里,除了完全兼容GB2312 外,还对繁体中文、一些不常用的汉字和许多符号进行了编码。
ISO-8859-1:是西方国家所使用的字符编码集,是一种单字节的字符集 ,而英文实际上只用了其中数字小于128的部分。
Unicode:这是一种通用的字符集,对所有语言的文字进行了统一编码,对每一个字符都用2个字节来表示,对于英文字符采取前面加“0”字节的策略实现等长兼容。如 “a” 的ASCII码为0x61,UNICODE就为0x00,0x61。
UTF-8:Eight-bit UCS Transformation Format,(UCS,Universal Character Set,通用字符集,UCS 是所有其他字符集标准的一个超集)。一个7位的ASCII码值,对应的UTF码是一个字节。如果字符是0x0000,或在0x0080与0x007f之间,对应的UTF码是两个字节,如果字符在0x0800与0xffff之间,对应的UTF码是三个字节。
RandomAccessFile
RandomAccessFile类同时实现了DataInput和DataOutput接口,提供了对文件随机存取的功能,利用这个类可以在文件的任何位置读取或写入数据。
RandomAccessFile类提供了一个文件指针,用来标志要进行读写操作的下一数据的位置。
对象序列化
将对象转换为字节流保存起来,并在日后还原这个对象,这种机制叫做对象序列化。
将一个对象保存到永久存储设备上称为持续性。
一个对象要想能够实现序列化,必须实现Serializable接口或Externalizable接口。
当一个对象被序列化时,只保存对象的非静态成员变量,不能保存任何的成员方法和静态的成员变量。
如果一个对象的成员变量是一个对象,那么这个对象的数据成员也会被保存。
如果一个可序列化的对象包含对某个不可序列化的对象的引用,那么整个序列化操作将会失败,并且会抛出一个NotSerializableException。我们可以将这个引用标记为transient,那么对象仍然可以序列化。
FileTest
import java.io.*; class FileTest{ public static void main (String[] args) throws Exception{ //File f=new File("1.txt"); //f.createNewFile(); //f.mkdir(); //File f=new File("G:\\yule\\java\\Lesson7\\1.txt"); //f.createNewFile(); /*File fDir=new File(File.separator); String strFile="yule"+File.separator+"java"+File.separator+"Lesson7"+File.separator+"1.txt"; File f=new File(fDir, strFile); f.createNewFile(); //f.delete(); f.deleteOnExit(); Thread.sleep(3000);*/ /*for(int i=0;i<5;i++){ File f=File.createTempFile("winsun", ".tmp"); Thread.sleep(1000); f.deleteOnExit(); }*/ File fDir=new File(File.separator); String strFile="yule"+File.separator+"java"+File.separator+"Lesson6"; File f=new File(fDir, strFile); String[] names=f.list(new FilenameFilter(){ public boolean accept(File dir, String name){ return name.indexOf(".java") != -1; } } ); for(int i=0;i<names.length;i++){ System.out.println(names[i]); } } }
ObjectSerialTest
import java.io.*; class ObjectSerialTest{ public static void main (String[] args) throws Exception{ Employee e1=new Employee("zhangsan", 25, 3000.50); Employee e2=new Employee("lisi", 24, 3200.40); Employee e3=new Employee("wangwu", 27, 3800.55); FileOutputStream fos=new FileOutputStream("employee.txt"); ObjectOutputStream oos=new ObjectOutputStream(fos); oos.writeObject(e1); oos.writeObject(e2); oos.writeObject(e3); oos.close(); FileInputStream fis=new FileInputStream("employee.txt"); ObjectInputStream ois=new ObjectInputStream(fis); Employee e; for(int i=0;i<3;i++){ e=(Employee)ois.readObject(); System.out.println(e.name+" "+e.age+" "+e.salary); } } } class Employee implements Serializable{ String name; int age; double salary; public Employee(String name, int age, double salary){ this.name=name; this.age=age; this.salary=salary; } }
PipedStreamTest
/** * @(#)PipedStreamTest.java * * * @author * @version 1.00 2012/5/7 */ import java.io.*; public class PipedStreamTest { public static void main (String[] args) { PipedOutputStream pos=new PipedOutputStream(); PipedInputStream pis=new PipedInputStream(); try{ pos.connect(pis); new Producer(pos).start(); new Consumer(pis).start(); }catch(Exception e){ e.printStackTrace(); } } } class Producer extends Thread { private PipedOutputStream pos; public Producer(PipedOutputStream pos) { this.pos=pos; } public void run() { try{ pos.write("hello,welcome you!".getBytes()); pos.close(); }catch(Exception e){ e.printStackTrace(); } } } class Consumer extends Thread { private PipedInputStream pis; public Consumer(PipedInputStream pis){ this.pis=pis; } public void run(){ try{ byte[] buf=new byte[100]; int len=pis.read(buf); System.out.println(new String(buf, 0, len)); pis.close(); }catch(Exception e){ e.printStackTrace(); } } }
StreamTest
/** * @(#)StreamTest.java * * * @author * @version 1.00 2012/5/6 */ import java.io.*; public class StreamTest { public static void main(String[] args) throws Exception{ /*int data; while((data=System.in.read()) != -1){ System.out.write(data); }*/ /*FileOutputStream fos=new FileOutputStream("1.txt"); //fos.write("hsdafajgagfajhttp://www.zzu.edu.cn".getBytes()); //fos.close(); BufferedOutputStream bos=new BufferedOutputStream(fos); //bos.write("http://www.baidu.com".getBytes()); //bos.flush(); //bos.close(); DataOutputStream dos=new DataOutputStream(bos); byte b=3; int i=79; char ch='a'; float f=4.5f; dos.writeByte(b); dos.writeInt(i); dos.writeChar(ch); dos.writeFloat(f); dos.close(); FileInputStream fis=new FileInputStream("1.txt"); BufferedInputStream bis=new BufferedInputStream(fis); /*byte[] buf=new byte[100]; //int len=fis.read(buf); int len=bis.read(buf); System.out.println(new String(buf, 0, len)); bis.close();*/ /*DataInputStream dis=new DataInputStream(bis); System.out.println(dis.readByte()); System.out.println(dis.readInt()); System.out.println(dis.readChar()); System.out.println(dis.readFloat()); dis.close();*/ /*FileOutputStream fos=new FileOutputStream("1.txt"); OutputStreamWriter osw=new OutputStreamWriter(fos); BufferedWriter bw=new BufferedWriter(osw); bw.write("http://www.baidu.com"); bw.close(); FileInputStream fis=new FileInputStream("1.txt"); InputStreamReader isr=new InputStreamReader(fis); BufferedReader br=new BufferedReader(isr); String strLine; while((strLine=br.readLine()) != null) System.out.println(strLine); /*char[] ch=new char[100]; int len=br.read(ch, 0, 20); System.out.println(ch); br.close();*/ InputStreamReader isr=new InputStreamReader(System.in); BufferedReader br=new BufferedReader(isr); String strLine; while((strLine=br.readLine()) != null) System.out.println(strLine); } }
CharsetTest
import java.util.*; import java.nio.charset.*; class CharsetTest{ public static void main (String[] args) throws Exception { /*Map m=Charset.availableCharsets(); Set names=m.keySet(); Iterator it=names.iterator(); while(it.hasNext()){ System.out.println(it.next()); }*/ Properties pps=System.getProperties(); //pps.list(System.out); int data; byte[] buf=new byte[100]; int i=0; while((data=System.in.read()) != 'q'){ buf[i]=(byte)data; i++; } String str=new String(buf, 0, i); System.out.println(str); } }
相关文章推荐
- Java学习lesson 07
- Java学习lesson 04
- Java学习笔记--Lesson2(Java面向对象编程)
- Java学习lesson 05
- java里的常用包 java学习 Lesson4
- 【安卓学习笔记】JAVA基础Lesson5-关键字this
- 网络编程 java学习 Lesson 10
- java里的常用包 java学习 Lesson4
- Java学习lesson 11
- 网络编程 java学习 Lesson 10
- 【安卓学习笔记】JAVA基础Lesson1-简介与变量
- 【安卓学习笔记】JAVA基础Lesson6-关键字static
- Java学习lesson 08
- 线程 java学习 Lesson 5
- Java学习lesson 10
- Java学习lesson 12
- 线程 java学习 Lesson 5
- Java学习lesson 06
- Java学习 lesson2
- 【安卓学习笔记】JAVA基础Lesson4-函数重载与构造函数