输入输出(1).ppt

上传人:laozhun 文档编号:2674031 上传时间:2023-02-22 格式:PPT 页数:40 大小:291KB
返回 下载 相关 举报
输入输出(1).ppt_第1页
第1页 / 共40页
输入输出(1).ppt_第2页
第2页 / 共40页
输入输出(1).ppt_第3页
第3页 / 共40页
输入输出(1).ppt_第4页
第4页 / 共40页
输入输出(1).ppt_第5页
第5页 / 共40页
点击查看更多>>
资源描述

《输入输出(1).ppt》由会员分享,可在线阅读,更多相关《输入输出(1).ppt(40页珍藏版)》请在三一办公上搜索。

1、输入输出流,目标,流概念Java用于输入输出流的类数据池流的使用 处理流的使用 文件随机读取总结,输入/输出的需求,Java把这些不同类型的输入、输出抽象为流(stream),分为输入流和输出流,用统一的接口来表示,Java开发环境中提供了包Java.io,其中包括一系列的类来实现输入输出处理。,流:是一个数据序列。有两种流:1.输入流2.输出流,输入流 通过打开一个到数据源(文件、内存或网络端口上的数据)的输入流,程序可以从数据源上顺序读取数据。,读数据的逻辑为:open a streamwhile more informationread informationclose the stre

2、am,输出流 通过打开一个到目标的输出流,程序可以向外部目标顺序写数据。,写数据的逻辑为:open a streamwhile more informationwrite informationclose the stream,Java用于输入输出流的类,按所读写的数据类型分两类:字符流类(Character Streams)字符流类用于向字符流读写16位二进制字符。字节流类(Byte Streams)字节流类用于向字节流读写8位二进制的字节。一般地,字节流类主要用于读写诸如图象或声音等的二进制数据。,java.io中的基本流类:说明:它们是抽象类,不能直接创建对象。,InputStream

3、方法,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 o

4、ther 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(c

5、har 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、OutputStreamFileInputStream、FileOutputStream顺序读取文件FilterInputStream、FilterOutputStream过滤流(有多线程同步)DataInputStream、DataOutputStream对数据类型读写,有

6、多线程同步BufferedInputStream、BufferedOutputStream,字符流,Reader、WriterInputStreamReader、OutputStreamWriterFileReader、FileWriterBufferedReader、BufferedWriterStringReader、StringWriter,对象流,接口ObjectInputObjectOutput提供的一组方法,直接支持对对象的读写ObjectInputStream、ObjectOutputStream用来直接进行对象的读写 readObject(),writeObject(),imp

7、ort 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 v

8、oid 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()

9、;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)

10、;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 fi

11、le 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流。此时要用到下述简单的静态方法调用:

12、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 BufferedOutputStr

13、eam(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();,DataInputStr

14、eam 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 d

15、escs=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 File

16、InputStream(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+unit*price;catch(EOFException e)System.out.println(For a TOTAL of:$+total);in.close();,作业,1.从标准设备中输入若干行英文句子,直到输入bye结束,将这些字符串写入文件。2.在刚才输入的文件中查找指定的单词。打印出包含了欲找单词的所有文本行。3.用DataInputStream 输入一个整型数,一个双精度型和一个字符串到文件中,然后用DataOutputStream将这些文件读出并打印到标准输出设备。,

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

当前位置:首页 > 建筑/施工/环境 > 项目建议


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号