《java输入输出流》ppt课件

上传人:tian****1990 文档编号:74812001 上传时间:2019-01-29 格式:PPT 页数:48 大小:306.46KB
返回 下载 相关 举报
《java输入输出流》ppt课件_第1页
第1页 / 共48页
《java输入输出流》ppt课件_第2页
第2页 / 共48页
《java输入输出流》ppt课件_第3页
第3页 / 共48页
《java输入输出流》ppt课件_第4页
第4页 / 共48页
《java输入输出流》ppt课件_第5页
第5页 / 共48页
点击查看更多>>
资源描述

《《java输入输出流》ppt课件》由会员分享,可在线阅读,更多相关《《java输入输出流》ppt课件(48页珍藏版)》请在金锄头文库上搜索。

1、第九章 输入输出流,输入输出流:,Java的输入输出功能必须借助于输入输出类库Java.io包来实现,这个包中的类大部分是用来完成流式输入输出的流类。 在java中,把能够读取一个字节序列的对象称为一个输入流,把能够写一个字节的对象称为一个输出流。分别由InputStream和OutputStream表示。 处理Unicode码的字符流,由抽象类Reader和Writer派生。,流,流是指在计算机的输入与输出之间的数据的序列,而Java中的数据流有位流(字节流)和字符流之分; 就流的运动方向而言,流可分为输入流(input stream)和输出流(output stream),输入流代表从外设

2、流入计算机的数据序列;输出流代表从计算机流向外设的数据序列。,9.1 java.io包简介,java.io包通过数据流、序列和文件系统为系统提供输入输出。,9.2 InputStream类和 OutputStream类,InputStream类的实现: public abstract class InputStream extends Object InputStream类的方法 public int available() throws IOException 返回目前输入数据流中已有几个字符准备好被读取了 public void close()throws IOException 将这个输

3、入数据流关闭 public abstract int read()throws IOException 从输入数据流读取下一个字节并返回返回值的范围从到,但如果已经到达数据流的结尾,没有数据可以读取了,则会返回抛出异常,InputStream类的方法,public int read(byte b)throws IOException 从输入数据流中读取数个字节放进数组b中,并返回所读取到的字符数目,最多可以读取和b长度相同的字符数,但如果没有读取到则返回 public long skip(long n)throws IOException 略过n个字节不读取,会返回实际略过的字节数目因为数据流

4、中剩下的数据可能不到n个字节那么多,所以此时返回值会小于n,InputStream类的继承关系,InputStream FileInputStream(文件输入字节) FilterInputStream DataInputStream(格式转换) BufferedInputStream (缓冲功能),System.in是System的一个静态属性,属于InputStream类对象,用于输入字节数据流,对应标准输入设备:键盘。 Java实例标准输入 import java.io.*; public class StandardIn1 public static void main(String

5、args) throws IOException char c; System.out.println(“ 输入一个字符“); c=(char)System.in.read(); System.out.print( “输入的字符是:“+c); ,标准输入System.in,OutputStream类,OutputStream类是所有输出数据流的父类,所以它也被实现成抽象类,定义了所有输出数据流都具有的共同特性 OutputStream类的实现: public abstract class OutputStream extends Object,OutputStream类的继承关系,Output

6、Stream FileOutputStream(文件输出) FilterOutputStream PrintStream(输出到屏幕) DataOutputStream(格式转换) BufferedOutputStream (缓冲功能),OutputStream类的method,public void close( ) throws IOException 关闭与输出数据流的连系 public void flush( )throws IOException 将写入的数据确实送到接受数据的装置去因为写入的数据通常会先放到高速缓存(cache)里面,等到数量达到某一程度时,用这个方法将强迫数据写进

7、去 public void write(byte b)throws IOException 将数组b中的数据写入输出数据流,OutputStream类的method,public void write(byte b,int off,int len)throws IOException 将b数组中从第off个字节开始,共写入len个字节到输出数据流 public abstract void write(int b) throws IOException 写入一个字符到输出数据流这个method只会将b的个低位写入,将24个高位忽略掉,标准输出System.out,System.out是它的一个静

8、态属性,属于PrintStream类对象,用于输出字节数据流,对应标准输出设备:屏幕。,9.3 File类,File类指的是磁盘上的目录或文件.“”表示分隔符。例如:D:myjavaHello.java,文件类的主要方法:,public File(File parent,String child):建立一个以parent加上child为路径的File组件。 public File(String pathname):建立一个以pathname为路径的File组件 public String getName():得到文件的名字 public String getPath():得到文件的路径名 pu

9、blic String getParent():得到文件的上级路径名 public boolean exists():判断文件或目录是否存在 public long length():返回文件的字节数 public boolean createNewFile() throws IOException:以File组件的内容为文件名建立一个新文件,如果原文件已存在,返回false,如果建立成功,则返回true. public void delete():删除文件,Java实例,import java.awt.*; import java.io.*; public class FileTest pu

10、blic static void main(String args) File f=new File(“D:“,“c.txt“); try boolean b=f.createNewFile(); System.out.println(f.getPath(); System.out.println(f.getParent(); System.out.println(f.getName(); System.out.println(b); catch(Exception e) ,9.4 RandomAccessFile类,RandomAccessFile流由于实现了DataInput和DataOu

11、tput两个接口,所以既可以用来处理文件的输入,又可以处理文件的输出。 主要方法: public RandomAccessFile(String name,String mode)构造RandomAccessFile流对文件进行读写。name为文件名, mode为读写模式,包括r(只读)rw(可读写).,public RandomAccessFile(File name,String mode)构造RandomAccessFile流对文件对象进行读写。name为文件名, mode为读写模式,包括r(只读)rw(可读写). close( )关闭RandomAccessFile流,并释放资源。 g

12、etFilePointer()返回文件指针位置。 length()返回文件长度 read()从文件中读取一个字节的数据 readInt()从文件中读取一个int值(4个字节) readLine()读取一行 seek(long pos)定位参数pos在文件中指定的位置,skipBytes():在文件中跳过指定数量的字节,数量由n指定。 writeChars(String s):向文件写入一个作为字符数据的字符串。 writeInt():向文件写入一个int型数据。,Java实例:,将一组数据写入到名为test.dat的文件中,然后再按相反顺序读出这些数据。 import java.io.*; p

13、ublic class Io public static void main(String args) RandomAccessFile test=null; int data=100,101,102,103,104,105; try test=new RandomAccessFile(“test.dat“,“rw“); catch(Exception e),try for(int i=0;i=0;i=i-4) test.seek(i); System.out.println(test.readInt(); test.close(); catch(Exception e) ,Reader类,R

14、eader子类的继承关系,Reader BufferedReader(缓冲功能的字符输入类) InputStreamReader(输入字节转换为字符) FileReader (从文件读入字符),Reader类的method,public abstract void close()throws IOException 关闭数据流 public int read()throws IOException 从输入数据流读取下一个字节并返回返回值的范围从到,但如果已经到达数据流的结尾,没有数据可以读取了,则会返回 public int read(char cbuf) throws IOException

15、 从数据流读取数个字符放进数组cbuf中,并返回所读取到的字符数目,最多可读取和cbuf长度相同的字符数,如果没有读取到则返回-1.这个method会一直等待直到读取到字符产生IOException或数据读完为止,Reader类的method,public long skip(long n)throws IOException 略过n个字节不读取,会返回实际略过的字节数目因为数据流中剩下的数据可能不到n个字节那么多,所以此时返回值会小于n public boolean ready()throws IOException 检查数据流是否已经准备好要被读取,是则返回 true,否则返回false如

16、果返回值是true则紧跟着的read指令将不会受到阻碍,Writer类,Writer类的实现: public abstract class Writer extends Object Writer子类的继承关系 Writer PrintWriter (格式化输出功能) BufferedWriter(缓冲功能) OutputStreamWriter (字符字节转换) FileWriter (向文件读入字符),Writer类的method,public abstract void close()throws IOException 关闭与输出数据流的连接,但关闭之前会先调用一次flush method.如果程序结束前没有调用这个method则写入的数据可能会流失调 public abstract void flush()th

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 高等教育 > 大学课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号