java语言第八章javaio系统

上传人:tian****1990 文档编号:74601658 上传时间:2019-01-28 格式:PPT 页数:33 大小:281.31KB
返回 下载 相关 举报
java语言第八章javaio系统_第1页
第1页 / 共33页
java语言第八章javaio系统_第2页
第2页 / 共33页
java语言第八章javaio系统_第3页
第3页 / 共33页
java语言第八章javaio系统_第4页
第4页 / 共33页
java语言第八章javaio系统_第5页
第5页 / 共33页
点击查看更多>>
资源描述

《java语言第八章javaio系统》由会员分享,可在线阅读,更多相关《java语言第八章javaio系统(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

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

3、 8.2.4随机文件的读取,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

4、File(new File(“c:data”) ,“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()

5、,String list() String list(FilenameFilter 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

6、suffix) static File createTempFile(String prefix, 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()

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

8、an readBoolean() Byte readByte() Char readChar() double readDouble() 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类的读方

9、法,8.2.3读写文件中的基本数据类型,void writeBoolean(Boolean b) void writeByte(int v) void 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),DataOutputS

10、tream类的写方法,在生成一个随机文件对象时,除了要指明文件对象和文件名之外,还需要指明访问文件的模式。 RandomAccessFile(File file, String mode) RandomAccessFile(String name, String mode) mode 的取值: “r” 只读. 任何写操作都将抛出IOException。 “rw” 读写. 文件不存在时会创建该文件,文件存在时,原文件内容不变,通过写操作改变文件内容。 “rws” 同步读写. 等同于读写,但是任何写操作的内容都被直接写入物理文件,包括文件内容和文件属性。 “rwd” 数据同步读写. 等同于读写,但

11、任何内容写操作都直接写到物理文件,但对文件属性内容的修改不是这样。,8.2.4随机文件的读取,对于FileInputStream/FileOutputStream、FileReader/FileWriter来说,它们的实例都是顺序访问流,即只能进行顺序读/写。而类RandomAccessFile则允许对文件内容同时完成读和写操作,它直接继承object,并且同时实现了接口DataInput和DataOutput,提供了支持随机文件操作的方法: readXXX()或writeXXX(): 如ReadInt(), ReadLine(), WriteChar(), WriteDouble()等。 i

12、nt skipBytes(int n):将指针乡下移动若干字节 length():返回文件长度 long getFilePointer():返回指针当前位置 void seek(long pos):将指针调到所需位置,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

13、随机文件的读取,public class Random_file public static void main(String args) int data_arr=12, 31, 56, 23, 27, 1, 43, 65, 4, 99; try 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 (IOExcep

14、tion e) System.out.println(“File access error: “+e); ,8.2.4随机文件的读取,8.3字节流和字符流处理,8.3.1字节流 8.3.2字符流 8.3.3InputStreamReader类和OutputStreamWriter类 8.3.4BufferedReader类和BufferedWriter类,8.3.1字节流,基本字节输入流InputStream public abstract int read() thows IOException public int read(byte b) throws IOException publi

15、c int read(byte b,int offset,int length)throws IOException public int available() throws IOException public long skip(long n) throws IOException,8.3.2字符流,关闭流 public void close() throws IOException 使用输入流中的标记 public mark(int readlimit) public void reset() public boolean markSupported(),8.3.2字符流,基本输入字符

16、流Reader 读取字符 public int read() throws IOException public int read(char chbuf,int offset ,int length) throws IOException public int read(char chbuf) throws IOException 标记流 public Boolean markSupported() public void mark(int readAheadLimit)throws IOException public void reset() throws IOException 关闭流 public abstract void close() throws IOException,8.3.2字符流,基本输出字符流Writer 向输出流写入字符 public void write(int a) throws IOException public void write(c

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


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

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