输入输出.ppt_第1页
输入输出.ppt_第2页
输入输出.ppt_第3页
输入输出.ppt_第4页
输入输出.ppt_第5页
已阅读5页,还剩35页未读 继续免费阅读

下载本文档

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

文档简介

输入输出流,目标,流概念 java用于输入输出流的类 数据池流的使用 处理流的使用 文件随机读取 总结,输入/输出的需求,java把这些不同类型的输入、输出抽象为流(stream),分为输入流和输出流,用统一的接口来表示, java开发环境中提供了包java.io,其中包括一系列的类来实现输入输出处理。,流:是一个数据序列。有两种流: 1. 输入流 2. 输出流,输入流 通过打开一个到数据源(文件、内存或网络端口上的数据)的输入流,程序可以从数据源上顺序读取数据。,读数据的逻辑为: open a stream while more information read information close the stream,输出流 通过打开一个到目标的输出流,程序可以向外部目标顺序写数据。,写数据的逻辑为: open a stream while more information write information close the stream,java用于输入输出流的类,按所读写的数据类型分两类: 字符流类(character streams) 字符流类用于向字符流读写16位二进制字符。 字节流类(byte streams) 字节流类用于向字节流读写8位二进制的字节。一般地,字节流类主要用于读写诸如图象或声音等的二进制数据。,java.io中的基本流类: 说明:它们是抽象类,不能直接创建对象。,inputstream 方法,the three basic read methods: int read() int read( byte buffer) int read( byte buffer, int offset, int length) the other methods: void close() int available() skip( long n),outputstream 方法,the three basic write methods: void write( int c) void write( byte buffer) void write( byte buffer, int offset, int length) the other methods: void close() void flush(),reader methods,the three basic read methods: int read() int read( char cbuf) int read( char cbuf, int offset, int length) the other methods: void close() boolean ready() skip( long n),writer 方法,the three basic write methods: void write( int c) void write( char cbuf) void write( char cbuf, int offset, int length) void write( string string) void write( string string, int offset, int length) the other methods: void close() void flush(),字节流,inputstream、outputstream fileinputstream、fileoutputstream顺序读取文件 filterinputstream、filteroutputstream过滤流(有多线程同步) datainputstream、dataoutputstream对数据类型读写,有多线程同步 bufferedinputstream、bufferedoutputstream,字符流,reader、writer inputstreamreader、outputstreamwriter filereader、filewriter bufferedreader、bufferedwriter stringreader、stringwriter,对象流,接口objectinputobjectoutput提供的一组方法,直接支持对对象的读写 objectinputstream、objectoutputstream 用来直接进行对象的读写 readobject(),writeobject(),import java.io.*; import java.util.date; public class serializedate serializedate() date d = new date (); try fileoutputstream f =new fileoutputstream (“date.ser“); objectoutputstream s = new objectoutputstream (f); s.writeobject(“today“); s.writeobject (d); s.close (); catch (ioexception e) e.printstacktrace (); public static void main (string args) new serializedate(); ,import java.io.*; import java.util.date; public class unserializedate unserializedate () date d = null; string today; try fileinputstream f = new fileinputstream (“date.ser“); objectinputstream s = new objectinputstream (f); today = (string)s.readobject(); d = (date) s.readobject (); s.close (); catch (exception e) e.printstacktrace (); ,system.out.println( “unserialized date object from date.ser“); system.out.println(today); system.out.println(d); public static void main (string args) new unserializedate(); ,文件流,例题:将一个文件的内容拷贝到另一个文件。,file类用来访问本地文件系统中的文件和目录。 1. 创建file类 myfile = new file(“ myfile. txt“); file mydir = new file(“ mydocs“); myfile = new file( mydir, “myfile. txt“);,file类中的方法 file names: string getname() string getpath() string getabsolutepath() string getparent() boolean renameto( file newname),file tests: boolean exists() boolean canwrite() boolean canread() boolean isfile() boolean isdirectory(),general file information and utilities: long length() boolean delete() directory utilities: boolean mkdir() string list(),处理流的使用,buffered streams,对标准输出输出的操作。 system. out :an object of printstream类 system. in:object of inputstream类 system. err:printstream,对标准输出输出的操作。 重导向标准io 在system类中允许我们重新定向标准输入、输出以及错误io流。此时要用到下述简单的静态方法调用: setin(inputstream) setout(printstream) seterr(printstream),import java.io.*; class redirecting public static void main(string args) try bufferedinputstream in = new bufferedinputstream( new fileinputstream( “redirecting.java“); / produces deprecation message: printstream out = new printstream( new bufferedoutputstream( new fileoutputstream(“test.out“); system.setin(in); system.setout(out); system.seterr(out);,bufferedreader br = new bufferedreader( new inputstreamreader(system.in); string s; while(s = br.readline() != null) system.out.println(s); out.close(); / remember this! catch(ioexception e) e.printstacktrace(); ,datainputstream and dataoutputstream 提供了允许从流读写任意对象与基本数据类型功能的方法。,public class dataiotest public static void main(string args) throws ioexception / write the data out dataoutputstream out = new dataoutputstream(new fileoutputstream(“invoice1.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“ ;,for (int i = 0; i prices.length; i +) out.writedouble(pricesi); out.writechar(t); out.writeint(unitsi); out.writechar(t); out.writechars(descsi); out.writechar(n); out.close(); / read it in again datainputstream in = new datainputstream(new fileinputstream(“invoice1.txt“); double price; int unit; string desc; double total = 0.0;,try while (true) price = in.readdouble(); in.readchar(); / throws out the tab unit = in.readint(); in.readchar(); / throws out the tab desc = in.readline(); system.out.println(“youve ordered “ + unit + “ units of “ + desc + “ at $“ + price); total = total

温馨提示

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

评论

0/150

提交评论