1、文件与IO流,2,主要内容,流的概念 流的分类 流的使用(以字节流为例) 标准输入/输出流 文件输入/输出流 字符流的使用,3,流(Stream)的概念,流是从源到目的地的有序字节序列,具有先进先出的特征。 根据流与程序的关系将流分为输入流和输出流两类。 程序从输入流读取数据;向输出流写出数据。,4,流的概念,源 输入流的源可以是文件、标准输入(键盘)、其他外部输入设备或者其他输入流。 目的地 输出流的目的地可以是文件、标准输出(显示器)、其他外部输出设备或者其他输出流。 Java中输入输出是通过流来实现的。相关的类都在java.io包中。,5,流的分类,输入流/输出流 按流与程序的关系分。
2、字节流/字符流 按流中处理的数据是以字节(8位)为单位还是以字符(16位)为单位分为字节流和字符流。 Java中字节流和字符流分属两个不同的体系。,6,字节流的层次结构,过滤流,结点流,抽象类,7,字符流的类层次结构,8,InputStream类的常用方法,读一个字节,并返回该字节。未读到返回-1 public int read() throws IOException关闭流 public void close( ) throws IOException将数据读入字节数组b, 返回所读的字节数 int read(byte b ) throws IOException 将数据读入字节数组b, 返
3、回所读的字节数,offset和length指示byte中存放读入字节的位置。 int read( byte b, int offset, int length ) throws IOException,9,OutputStream的常用方法,写一个字节 void write(int) throws IOException关闭输出流 void close( ) throws IOException将缓冲区的数据写到目的地。 void flush( ) throws IOException 写一个字节数组 void write(byte b) throws IOException void wri
4、te(byte b, int offset, int length ) throws IOException,10,标准输入输出流,System.out: 把输出送到缺省的显示(通常是显示器) System.in 从标准输入获取输入(通常是键盘) System是final类,in,out是System的静态成员变量,因此可以用System.in等形式直接使用。,11,标准输入 System.in,在System中,in的完整定义是: public static final InputStream in; in的主要方法: public int read() throws IOException
5、 public int read(byte b) throws IOException 使用注意事项: 前者返回读入的一字节的数据,但返回的是int整型值,取值范围是0-255。 后者返回读入的字节数,读入的各字节保存在作为参数的字节型数组对象中。 执行read时,程序会等待用户的输入。输入完成后再接着执行后面的语句。,12,流的使用过程,输入/输出流的使用过程: 实例化一个输入/输出流对象 使用该输入/输出流对象的方法读入/写出数据 关闭该输入/输出流对象注意事项 输入/输出流的方法会抛出异常,因此必须进行异常处理。 标准输入/输出流对象System.in, System.out始终存在,不
6、需要实例化,也不需要关闭。,13,例:使用System.in实现键盘数据输入,import java.io.*; public class TestInput public static void main(String args)try byte bArray=new byte128;System.out.println(“请输入一些东西:“);System.in.read(bArray);String s= new String(bArray,0,bArray.length).trim();System.out.println(“你输入的是:“+s);catch(IOException i
7、oe) System.out.println(ioe.toString(); ,14,从文件读数据:FileInputStream,FileInputStream是InputStream的子类,可以生成实例。 FileInputStream有三个构造方法,最常用的构造方法如下: Public FileInputStream(String fileName) throws FileNotFoundExceptionPublic FileInputStream(File file) throws FileNotFoundExceptionfileName用来指定输入文件名及其路径,file是一个F
8、ile对象,15,例:读出一个文本文件的内容并显示,import java.io.*; class ReadFromFile public static void main(String args) InputStream in; tryin=new FileInputStream(“test.txt“);int aByte;aByte=in.read();while (aByte!=-1)System.out.print(char)aByte);aByte=in.read();in.close();catch(FileNotFoundException e)System.out.printl
9、n(“当前目录下文件test.txt不存在!“);catch(IOException e)System.out.println(“发生输入输出错误!“); ,16,向文件写数据:FileOutputStream,FileOutputStream是OutputStream的子类,可以生成实例。 FileOutputStream有5个构造方法,最常用的构造方法如下: Public FileOutputStream(String name) throws FileNotFoundException 和 Public FileOutputStream(String name,boolean appen
10、d) throws FileNotFoundExceptionName用来指定输入文件名及其路径,append为true时数据将添加到文件已有内容的末尾。,17,例题:使用FileOutputStream实现文件复制。,import java.io.*; class CopyAFile public static void main(String args) InputStream in;OutputStream out;tryin=new FileInputStream(“test.txt“);out=new FileOutputStream(“copyResult.txt“);int aB
11、yte;aByte=in.read();while (aByte!=-1)out.write(aByte);aByte=in.read();in.close();out.close();System.out.println(“文件复制完毕。test.txt已经复制到copyResult.txt中。“);catch(FileNotFoundException e)System.out.println(“当前目录下文件test.txt不存在!“);catch(IOException e)System.out.println(“发生输入输出错误!“); ,18,BufferedInputStream
12、,数据流从原始流成块读入数据,放在一个内部字节数组中。通过减少IO次数提高效率。 构造方法 BufferedInputStream(InputStream in) BufferedInputStream(InputStream in, int buffersize) 基本方法: int read() throws IOException int read(byte, int offset, int length) throws IOException void close() throws IOException,19,BufferedOutputStream,将数据积累到一个大数据块后再成批
13、输出。通过减少IO次数提高效率。 构造方法: BufferedOutputStream(OutputStream out) BufferedOutputStream(OutputStream out, int buffersize) 基本方法: void write(int c) throws IOException void write(byte , int offset, int length ) throws IOException void close() throws IOException,20,例子:缓冲数据输入流,import java.io.*; class TestBuff
14、eredInput public static void main(String args) throws IOExceptionInputStream in;in=new BufferedInputStream(new FileInputStream(“test.txt“);int aByte;aByte=in.read();while (aByte!=-1)System.out.print(char)aByte);aByte=in.read();in.close(); ,21, 按java的基本数据类型读写流中的数据: DataInputStream方法byte readByte( ) b
15、oolean readBoolean( )long readLong( ) char readChar( )double readDouble( ) float readFloat( )short readshort( ) int readInt( )String readUTF( )/读取以UTF格式保存的字符串 DataOutputStream 方法void writeByte(byte) void writeBoolean(boolean)void writeLong( long ) void writeChar(char)void writeDouble(double) void wr
16、iteFloat( float)void writeshort(short) void writeInt ( int) void writeBytes(String) void writeChars(String )void WriteUTF(String str)/将字符串以UTF格式写出,数据输入输出流,22,例子:数据输入输出流,/使用DataOutputStream将一些数据写入文件,再用DataInputStream读入 /DataIOTeat.java import java.io.*;public class DataIOTest public static void main(
17、String args) throws IOException DataOutputStream out = new DataOutputStream(newFileOutputStream(“invoice.txt“);double prices = 19.99, 9.99, 15.99, 3.99, 4.99 ;int units = 12, 8, 13, 29, 50 ;String descs = “Java T-shirt“,“Java Mug“,“Duke Juggling Dolls“,“Java Pin“,“Java Key Chain“ ;,23,for (int i = 0
18、; i prices.length; i +) out.writeDouble(pricesi);out.writeChar(t);out.writeInt(unitsi);out.writeChar(t);out.writeUTF(descsi);out.writeChar(n);out.close();/ read it in againDataInputStream in = new DataInputStream(newFileInputStream(“invoice.txt“);double price;int unit;String desc;double total = 0.0;
19、,24,for (int i = 0; i prices.length; i +) price = in.readDouble();in.readChar(); / 读入tab键unit = in.readInt();in.readChar(); /读入tab键desc = in.readUTF();in.readChar(); /读入tab键System.out.println(“Youve ordered “ +unit + “ units of “ +desc + “ at $“ + price);total = total + unit * price;System.out.print
20、ln(“For a TOTAL of: $“ + total);in.close(); ,25,字符流, Reader和Writer是两个字符流的顶层抽象类。定义了读写16位字符的通用API。能够处理Unicode的所有字符。 Reader和Writer 类实现字节和字符间的自动转换。如果是读写文本类的数据使用字符流更佳。,26,字符流的常用类,输入流 FileReader InputStreamReader BufferedReader输出流 FileWriter OutputStreamWriter BufferedWriter,27,使用字符输入流从文件读取,import java.io
21、.*;public class StandardIOpublic static void main(String args)try FileReader in = new FileReader (“test.txt“);for(int i=in.read();i!=-1;i=in.read()System.out.print(char)i);in.close();catch(IOException e)e.printStackTrace(); ,28,作业,任务1:编写一个程序,统计一个磁盘文本文件中的单词数。 任务2:使用缓冲流(BufferedInputStream和BufferedOutputStream)改写前面的文件读写程序。,