Java语言第八章JavaIO系统.ppt

上传人:牧羊曲112 文档编号:6510236 上传时间:2023-11-07 格式:PPT 页数:33 大小:208.49KB
返回 下载 相关 举报
Java语言第八章JavaIO系统.ppt_第1页
第1页 / 共33页
Java语言第八章JavaIO系统.ppt_第2页
第2页 / 共33页
Java语言第八章JavaIO系统.ppt_第3页
第3页 / 共33页
Java语言第八章JavaIO系统.ppt_第4页
第4页 / 共33页
Java语言第八章JavaIO系统.ppt_第5页
第5页 / 共33页
点击查看更多>>
资源描述

《Java语言第八章JavaIO系统.ppt》由会员分享,可在线阅读,更多相关《Java语言第八章JavaIO系统.ppt(33页珍藏版)》请在三一办公上搜索。

1、第8章Java I/O系统,8.1I/O概述8.2文件8.3字节流和字符流处理8.4标准流 8.5其它常用的流8.6小结,8.1 I/O概述,8.1.1流的概念 8.1.2Java中的输入/输出流,8.1.1 流的概念,大部分程序都需要输入/输出处理,比如从键盘读取数据、向屏幕中输出数据、从文件中读或者向文件中写数据、在一个网络连接上进行读写操作等。在Java中,把这些不同类型的输入、输出源抽象为流(Stream),而其中输入或输出的数据则称为数据流(Data Stream),用统一的接口来表示,从而使程序设计简单明了。,8.1.2Java中的输入/输出流,流一般分为输入流(Input Str

2、eam)和输出流(Output Stream)两类,但这种划分并不是绝对的。比如一个文件,当向其中写数据时,它就是一个输出流;当从其中读取数据时,它就是一个输入流。当然,键盘只是一个输入流,而屏幕则只是一个输出流。在Java开发环境中,主要是由包java.io中提供的一系列的类和接口来实现输入/输出处理。标准输入/输出处理则是由包java.lang中提供的类来处理的,但这些类又都是从包java.io中的类继承而来。输入流:数据提供者,可从中读取数据出来输出流:数据接收者,可往其中写数据,8.2文件,8.2.1File类 8.2.2文件输入输出流 8.2.3读写文件中的基本数据类型 8.2.4随

3、机文件的读取,8.2.1File类,File(String pathname)File f=new File(“c:datatemp.dat”);File f=new File(“data temp.dat”);File f=new File(“temp.dat”);File(String parent,String child)File f=new File(“c:data”,“temp.dat”);File f=new File(“data”,“temp.dat”);File(File parent,String child)File f=new File(new File(“c:data

4、”),“temp.dat”);File f=new File(new File(“data”),“temp.dat”);,8.2.1File类,boolean canRead()boolean canWrite()boolean setReadOnly()boolean exists()boolean isDirectory()boolean isFile()boolean isHidden()long lastModified()boolean setLastModified(long time)long length(),String list()String list(FilenameF

5、ilter filter)File listFiles()File listFiles(FileFilter filter)File listFiles(FilenameFilter filter)static File listRoots()boolean mkdir()boolean mkdirs()(粉色的方法在JDK1.2之后才支持),8.2.1File类,boolean createNewFile()static File createTempFile(String prefix,String suffix)static File createTempFile(String pref

6、ix,String suffix,File directory)boolean delete()void deleteOnExit()boolean renameTo(File dest),8.2.1File类,String getName()File getParentFile()String getParent()String getPath()boolean isAbsolute()File getAbsoluteFile()String getAbsolutePath()File getCanonicalFile()String getCanonicalPath(),8.2.1File

7、类,8.2.2文件输入输出流,FileInputStream类和FileOutputStream类的构造函数是创建一个输入输出的对象,通过引用该对象的读写方法,来完成对文件的输入输出操作。在构造函数中,需要指定与所创建的输入输出对象相连接的文件。当然,要构造一个FileInputStream对象,所连接的文件必须存在而且是可读的;构造一个FileOutputStream对象如果输出文件已经存在且可写,该文件内容会被新的输出所覆盖。,8.2.3读写文件中的基本数据类型,boolean readBoolean()Byte readByte()Char readChar()double readDo

8、uble()float readFloat()int readInt()Long readLong()short readShort()int readUnsignedByte()int readUnsignedshort()Void readFully(byte b)Void readFully(byte b,int off,int len)int skipBytes(int n)String readUTF(),DataInputStream类的读方法,8.2.3读写文件中的基本数据类型,void writeBoolean(Boolean b)void writeByte(int v)vo

9、id writeBytes(String s)void writeChar(int v)void writeChars(String s)void writeDouble(double d)void writeFloat(float f)void writeInt(int v)void writeLong(int v)void writeShort(int v)void writeUTF(String str),DataOutputStream类的写方法,在生成一个随机文件对象时,除了要指明文件对象和文件名之外,还需要指明访问文件的模式。RandomAccessFile(File file,S

10、tring mode)RandomAccessFile(String name,String mode)mode 的取值:“r”只读.任何写操作都将抛出IOException。“rw”读写.文件不存在时会创建该文件,文件存在时,原文件内容不变,通过写操作改变文件内容。“rws”同步读写.等同于读写,但是任何写操作的内容都被直接写入物理文件,包括文件内容和文件属性。“rwd”数据同步读写.等同于读写,但任何内容写操作都直接写到物理文件,但对文件属性内容的修改不是这样。,8.2.4随机文件的读取,对于FileInputStream/FileOutputStream、FileReader/FileW

11、riter来说,它们的实例都是顺序访问流,即只能进行顺序读/写。而类RandomAccessFile则允许对文件内容同时完成读和写操作,它直接继承object,并且同时实现了接口DataInput和DataOutput,提供了支持随机文件操作的方法:readXXX()或writeXXX():如ReadInt(),ReadLine(),WriteChar(),WriteDouble()等。int skipBytes(int n):将指针乡下移动若干字节 length():返回文件长度 long getFilePointer():返回指针当前位置 void seek(long pos):将指针调到

12、所需位置,8.2.4随机文件的读取,File f=new File(“file.txt”);new RandomAccessFile(f,“r”);new RandomAccessFile(f,“rw”);new RandomAccessFile(“file1.txt”,“r”);new RandomAccessFile(“file2.txt”,“rw”);,8.2.4随机文件的读取,public class Random_file public static void main(String args)int data_arr=12,31,56,23,27,1,43,65,4,99;try

13、RandomAccessFile randf=new RandomAccessFile(“temp.dat”);for(int i=0;i=0;i-)randf.seek(i*4L);/int数据占4个字节 System.out.println(randf.readInt();randf.close();catch(IOException e)System.out.println(“File access error:“+e);,8.2.4随机文件的读取,8.3字节流和字符流处理,8.3.1字节流8.3.2字符流8.3.3InputStreamReader类和OutputStreamWrite

14、r类 8.3.4BufferedReader类和BufferedWriter类,8.3.1字节流,基本字节输入流InputStream public abstract int read()thows IOExceptionpublic int read(byte b)throws IOExceptionpublic int read(byte b,int offset,int length)throws IOExceptionpublic int available()throws IOException public long skip(long n)throws IOException,8

15、.3.2字符流,关闭流 public void close()throws IOException使用输入流中的标记 public mark(int readlimit)public void reset()public boolean markSupported(),8.3.2字符流,基本输入字符流Reader 读取字符 public int read()throws IOExceptionpublic int read(char chbuf,int offset,int length)throws IOExceptionpublic int read(char chbuf)throws I

16、OException标记流 public Boolean markSupported()public void mark(int readAheadLimit)throws IOExceptionpublic void reset()throws IOException关闭流 public abstract void close()throws IOException,8.3.2字符流,基本输出字符流Writer 向输出流写入字符 public void write(int a)throws IOExceptionpublic void write(char chbuf)throws IOEx

17、ceptionpublic abstract void write(char chbuf,int offset,int length)throws IOException public void write(String str)throws IOExceptionpublic void write(String str,int offset,int length)throws IOException刷新 public abstract void flush()关闭流 public abstract void close()throws IOException,8.3.3InputStream

18、Reader类和OutputStreamWriter类,InputStreamReader 类的主要构造函数 public InputStreamReader(InputStream in)public InputStreamReader(IntputStream in,String code)throws UnSupportedEncodingException OutputStreamWriter类的两个主要的构造函数 public OutputStreamWriter(OutputStream out)public OutputStreamWriter(OutputStream out,

19、String code)throws UnSupportedEncodingExceptioon,8.3.3InputStreamReader类和OutputStreamWriter类,InputStreamReader类和OutputStreamWriter类的方法读入和写出字符 获取当前编码方式 关闭流,8.3.4BufferedReader类和BufferedWriter类,BufferReader类 public BufferedReader(Reader in)public BufferedReader(Reader in,int bufSize)public String read

20、Line()throws IOException,8.3.4BufferedReader类和BufferedWriter类,BufferWriter类 public BufferedWriter(Writer out)public BufferedWriter(Writer out,int bufSize)public void newLine()throws IOException,8.4标准流,标准输入 标准输入System.in作为InputStream类的一个实例来实现,可以使用read()和skip(long n)两个方法。read()实现从输入中读一个字节,skip(long n)

21、实现在输入中跳过n个字节。标准输出 标准输出System.out是类System的数据成员out,它属于PrintStream类。PrintStream类和OutputStream类的关系是:OutputStream类是一个抽象类,FilterOutputStream是由抽象类OutputStream派生的,PrintStream类又是这个抽象类FilterOutputStream的一个子类。,8.5其它常用的流,管道流 构造方法中连接 PipedInputStream(PipedOutputStream send);PipedOutputStream(PipedInputStream rec

22、eive)connect()方法进行连接 类PipedInputStream中定义为:void connect(PipedOutputStream send);类PipedOutputStream中定义为:void connect(PipedInputStream receive);,8.5其它常用的流,内存的读/写 ByteArrayInputStream和ByteArrayOutputStream StringBufferInputStream和StringBufferOutputStream 顺序输入流 SequenceInputStream 把几个输入流顺序连接起来。顺序输入流提供了把

23、若干不同的流统一为同一个流的功能,使得程序变得更加简洁。,8.6小结,在这一章中我们只是介绍了I/O流的一些主要功能。另外,讲述了字节流和字符流的处理最后,介绍了其他的常用流,如管道流、顺序输入流等。,练习(1),1.简述Java流的概念,特点以及表示。2.试想想有哪些构造File类的方法。3.编写一个程序,完成文件路径和URL之间的转换。4.编写一个程序,确定文件名路径是文件还是目录。,练习(2),5.编写一个程序,将Fibonacci数列的前20项写入一个磁盘文件,要求每行只能输出5个数。6.如何通过InputStream、OutputStream和PrintStream类实现键盘输入和屏幕输出。7.编写程序,将一个给定文件中的英文单词和数据显示出来,其他的符号被看作是分隔符,每个单词或者数字占一行,分别统计单词和数字的个数,并输出到屏幕上。,下课!,

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号