java_IO类学习笔记.doc_第1页
java_IO类学习笔记.doc_第2页
java_IO类学习笔记.doc_第3页
java_IO类学习笔记.doc_第4页
java_IO类学习笔记.doc_第5页
已阅读5页,还剩8页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Java IO类1、File 类:一个File类的对象,表示了磁盘上的文件或目录。File类提供了与平台无关的方法来对磁盘上的文件或目录进行操作。File 类的构造方法有4种,创建文件:File f = new File(“1.txt”);如果在Windows平台下,绝对路径的 必须经过转译“”。Separator 是File类中的一个static 常量。可以进行跨平台的分隔符,作用相当于。具体用法:File fdir = new File(File.separator); /代表根目录 String strFile =”java”+ File.separator+ 1.txt ; File f = new File(fdir,strFile); /相当于 E:java1.txt f.createNewFile(); / 创建文件的方法 createNewFile();mkdir( ) 创建一个目录;delete() 删除文件; deleteOnExit(); 程序中止时删除文件,如删除tmp 文件.createTmpFile(Stringprefix, Stringsuffix) 生成临时文件。List() 以 String 返回文件目录下的所有文件及目录找出指定后缀名的文件String strName = f.list(new FilenameFilter()/将.java的文件找出 public boolean accept(File dir,String name) /传递文件名到过滤器 return name.indexOf(.java)!= -1; ); for(int i=0;istrName.length;i+) System.out.println(strNamei); FilenameFilter 文件名过虑器。调用accept() 方法.2、流式I/On 流(Stream)是字节的源或目的。n 两种基本的流是:输入流(Input Stream)和输出流(Output Stream)。可从中读出一系列字节的对象称为输入流。而能向其中写入一系列字节的对象称为输出流。流的分类:n 节点流:从特定的地方读写的流类,例如:磁盘或一块内存区域。n 过滤流:使用节点流作为输入或输出。过滤流是使用一个已经存在的输入流或输出流连接创建的。InputStreamn 三个基本的读方法 abstract int read() :读取一个字节数据,并返回读到的数据,如果返回-1,表示读到了输入流的末尾。 int read(byteb) :将数据读入一个字节数组,同时返回实际读取的字节数。如果返回-1,表示读到了输入流的末尾。 int read(byteb, intoff, intlen) :将数据读入一个字节数组,同时返回实际读取的字节数。如果返回-1,表示读到了输入流的末尾。off指定在数组b中存放数据的起始偏移位置;len指定读取的最大字节数。n 其它方法 long skip(longn) :在输入流中跳过n个字节,并返回实际跳过的字节数。 int available() :返回在不发生阻塞的情况下,可读取的字节数。 void close() :关闭输入流,释放和这个流相关的系统资源。 void mark(intreadlimit) :在输入流的当前位置放置一个标记,如果读取的字节数多于readlimit设置的值,则流忽略这个标记。 void reset() :返回到上一个标记。 boolean markSupported() :测试当前流是否支持mark和reset方法。如果支持,返回true,否则返回false。InputStreamStringBufferInputStreamFileInputStreamByteArrayInputStreamFilterInputStreamObjectInputStreamPipedInputStreamSequenceInputStreamLineNumberInputStreamDataInputStreamBufferedInputStreamPushbackInputStreamOutputStreamn 三个基本的写方法 abstract void write(intb) :往输出流中写入一个字节。 void write(byteb) :往输出流中写入数组b中的所有字节。 void write(byteb, intoff, intlen) :往输出流中写入数组b中从偏移量off开始的len个字节的数据。n 其它方法 void flush() :刷新输出流,强制缓冲区中的输出字节被写出。 void close() :关闭输出流,释放和这个流相关的系基本的流类n FileInputStream和FileOutputStream 节点流,用于从文件中读取或往文件中写入字节流。如果在构造FileOutputStream时,文件已经存在,则覆盖这个文件。n BufferedInputStream和BufferedOutputStream 过滤流,需要使用已经存在的节点流来构造,提供带缓冲的读写,提高了读写的效率。n DataInputStream和DataOutputStream 过滤流,需要使用已经存在的节点流来构造,提供了读写Java中的基本数据类型的功能。n PipedInputStream和PipedOutputStream 管道流,用于线程间的通信。一个线程的PipedInputStream对象从另一个线程的PipedOutputStream对象读取输入。要使管道流有用,必须同时构造管道输入流和管道输出流。(1)、FileInputStream 、FileOutputStreamFileOutputStream(File file) FileOutputStream(String FileName) 向指定文件对象或文件名写入输入出流。FileOutputStream fos = new FileOutputStream(1.txt); /向文件中输出节点流fos.write(.getBytes(); /String.getBytes() 返回平台默认字符集,将String 解析为字节序列,并存储到 byte 中; fos.close(); /关闭输出流FileInputStream fis = new FileInputStream(1.txt);byte buf = new byte100;int len = fis.read(buf); /将输入流读取到一个字节数组中System.out.println(new String(buf,0,len); /从0到len长度的实际读取fis.close(); /关闭输入流(2)、BufferedInputStream BufferedOutputStream 过滤流,带缓冲功能,必须用已经存在的节点流来构造。FileOutputStream fos = new FileOutputStream(1.txt);BufferedOutputStream bos=new BufferedOutputStream(fos); /必须以已经存在的节点流来构造bos.write(Ill never give up!.getBytes();bos.flush(); /强制输出缓冲区的数据bos.close();FileInputStream fis = new FileInputStream(1.txt);BufferedInputStream bus = new BufferedInputStream(fis);byte buf = new byte100;int len = bus.read(buf);System.out.println(new String(buf,0,len);bus.close(); (3)、DataInputStream DataOutputStream 也是过滤流类,具有读写java基本数据类似的功能FileOutputStream fos = new FileOutputStream(“1.txt”);BufferedOutStream bos = new BufferedOutputStream(fos);DataOutputStream dos = new DataOutputStream(bos); /提供了java基本数据类型的读取int a = 12;char c = 2;double d = 30.00;dos.writeInt(a);dos.writeChar(c);dos.writeDouble(d);dos.close();FileInputStream fis = new FileInputStream(“1.txt”);BufferedInputStream bis = new BufferedInputStream(fis);DataInputStream dis = new DataInputStream(bis);System.out.println(dis.readInt();System.out.println(dis.readChar();System.out.println(dis.readDouble();dis.close()/String类的数据读取有readByte,readChar,readUTF .witeChar,writeByte,writeUTFReader和Writern Java程序语言使用Unicode来表示字符串和字符。n Reader和Writer这两个抽象类主要用来读写字符流。/使用Writer 进行文件写入数据FileOutputStream fos = new FileOutputStream(1.txt);OutputStreamWriter osw = new OutputStreamWriter(fos); /OutputStreamWriter是字节流到字符流的桥梁BufferedWriter bw = new BufferedWriter(osw);bw.write(haha,i am the number one);bw.close();/使用Reader进行文件读取FileInputStream fis = new FileInputStream(1.txt);InputStreamReader ism = new InputStreamReader(fis);BufferedReader br = new BufferedReader(ism);System.out.println(br.readLine(); /readLine() 按行读取数据br.close();/多行文本的读取 InputStreamReader ism = new InputStreamReader(System.in);BufferedReader br = new BufferedReader(ism);String strLine;while(strLine=br.readLine()!=null) System.out.println(strLine);br.close();*I/O流的链接Java I/O库的设计原则n Java的I/O库提供了一个称做链接的机制,可以将一个流与另一个流首尾相接,形成一个流管道的链接。这种机制实际上是一种被称为Decorator(装饰)设计模式的应用。n 通过流的链接,可以动态的增加流的功能,而这种功能的增加是通过组合一些流的基本功能而动态获取的。n 我们要获取一个I/O对象,往往需要产生多个I/O对象,这也是Java I/O库不太容易掌握的原因,但在I/O库中Decorator模式的运用,给我们提供了实现上的灵活性。管道流类 PipedInputStream PipedOuputStream管道流,用于线程间的通信。一个线程的PipedInputStream对象从另一个线程的PipedOutputStream对象读取输入。要使管道流有用,必须同时构造管道输入流和管道输出流。import java.io.*;public class PipeStreamTest public static void main(String args) PipedOutputStream pos = new PipedOutputStream(); /同时构建输入和输出线程 PipedInputStream pis = new PipedInputStream(); try pos.connect(pis); / connect()进行线程的链接 new Producer(pos).start() /启动PipeOutputStream; 线程 new Consumer(pis).start(); /启动PiepInputStream 线程 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.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 byte100; int len = pis.read(buf); System.out.println(new String(buf,0,len); pis.close(); catch(Exception e) e.printStackTrace(); 字符集编码GBK,ISO-8859-1,GB2312,ASCII,UTF-8, Unicodejava.nio.charset类String(bytebytes, StringcharsetName) 构造一个新的 String,方法是使用指定的字符集解码指定的字节数组。String(bytebytes, intoffset, intlength, StringcharsetName) 构造一个新的 String,方法是使用指定的字符集解码字节的指定子数组。RandomAccessFile n RandomAccessFile类同时实现了DataInput和DataOutput接口,提供了对文件随机存取的功能,利用这个类可以在文件的任何位置读取或写入数据。n RandomAccessFile类提供了一个文件指针,用来标志要进行读写操作的下一数据的位置。import java.io.*;public class RandomFileTest public static void main(String args) throws Exception Student s1 = new Student(1,zhangsan,89); Student s2 = new Student(2,zhangsan2,80); Student s3 = new Student(3,zhangsan3,87); RandomAccessFile raf = new RandomAccessFile(student.txt,rw); /创建student.txt s1.writerStudent(raf); s2.writerStudent(raf); s3.writerStudent(raf); Student s = new Student(); raf.seek(0); /控制文件指针偏移量 for(long i = 0;i raf.length(); i=raf.getFilePointer() /getFilePointer() 返回此文件中的当前偏移量。 s.readerStudent(raf); System.out.println(num: +s.num+ nmae: ++ score +s.score); raf.close(); class Student int num; String name; double score; public Student() public Student(int num,String name,double score) this.num = num; = name; this.score = score; public void writerStudent(RandomAccessFile raf) throws IOException raf.writeInt(num); raf.writeUTF(name); raf.writeDouble(score); public void readerStudent(RandomAccessFile raf) throws IOException num = raf.readInt(); name = raf.readUTF(); score = raf.readDouble(); 对象序列化n 将对象转换为字节流保存起来,并在日后还原这个对象,这种机制叫做对象序列化。n 将一个对象保存到永久存储设备上称为持续性。n 一个对象要想能够实现序列化,必须实现Serializable接口或Externalizable接口。Serializable接口中没定义任何的方法, 使用ObjectOutputStream, ObjectInputStream进行对象序列化.,调用的方法是writeObject(object),反序列化,readObject(object)import java.io.*;public class ObjectSerialTest public static void main(String args) throws Exception Employee e1 = new Employee(ab,21,2333); Employee e2 = new Employee(yy,25,2367); Employee e3 = new Employee(zz,23,4444); FileOutputStream fos = new FileOutputStream(Manager.txt); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(e1); oos.writeObject(e2); oos.writeObject(e3); oos.close(); FileInputStream fis = new FileInputStream(Manager.txt); ObjectInputStream ois = new Obj

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论