【教学课件】第101章数据流的运用.ppt
《【教学课件】第101章数据流的运用.ppt》由会员分享,可在线阅读,更多相关《【教学课件】第101章数据流的运用.ppt(47页珍藏版)》请在三一办公上搜索。
1、第101章 数据流的运用,2,第101章 数据流的运用,101.1 输入输出方法101.2 输入输出流的基类101.3 File I/O101.4 数据输入输出流101.5 随机存取文件101.6 文件的其它操作101.7 java中的unicode101.8 管道流101.9 对象流101.10 流的分类101.11 小结,3,文件,程序,终端,文件,程序,网络端点,数据流,起点,终点,网络端点,文件,字符串存储区,101.1 输入输出方法,什么是数据流?,数据流是指所有的数据通信通道在java中有关流的操作使用java.io.*出于安全的考虑,小应用不能实现文件I/O流,4,101.1 输
2、入输出方法,System类管理标准输入输出流和错误流 System.out:把输出送到缺省的显示(通常是显示器)System.in从标准输入获取输入(通常是键盘)System.err把错误信息送到缺省的显示每当main方法被执行时,就自动生成上述三个对象,5,101.1 输入输出方法,public class ReadHello public static void main(String args)char inchar;System.out.println(“Enter a character:”);try inChar=(char)System.in.read();Syste.out.p
3、rintln(“+inChar);catch(IOException e)Sytem.out.println(“Error reading from user”);,6,101.1 输入输出方法,import java.io.*;class IOTest public statics void main(String args)try byte bArray=new byte128;System.out.println(“Enter something:”);System.in.read(bArray);System.out.print(“You entered:”);System.out.p
4、rintln(bArray);catch(IOException ioe)System.out.println(ioe.toString();,7,101.1 输入输出方法,为什么输入的是字符,输出是乱码?原因:System.out.println(bArray)输出的是数组的地址而不是字符改进:将字符数组变换成字符串原来是:System.out.println(bArray);现在为:String s=new String(bArray,0);System.out.println(s);System.in是属于BufferedInputStream类型System.out是属于PrintSt
5、ream类型System.err也是属于PrintStream类型,8,101.2 输入输出流的基类,Java中每一种流的基本功能依赖于基本类InputStream和OutputStream它们是抽象类,不能直接使用属于InputStream类的方法有:read():从流中读入数据skip():跳过流中若干字节数available():返回流中可用字节数mark():在流中标记一个位置reset():返回标记过得位置markSupport():是否支持标记和复位操作close():关闭流,9,101.2 输入输出流的基类,方法read()提供了三种从流中读数据的方法.int read():读一
6、个整数int read(byte b):读多个字节到数组中int read(byte,int off,int len);属于OutputStream类的方法有:write(int b):将一个整数输出到流中write(byte b):将数组中的数据输出到流中write(byte b,int off,int len):将数组b中从off指定的位置开始len长度的数据输出到流中,10,101.2 输入输出流的基类,flush():将缓冲区中的数据强制送出close():关闭流.PrintStream类println()不属于OutputStream类,它是PrintStream类的子类,能提供复杂
7、的输出PrintStream类的方法有:write,flush,checkError,print,println,close.其中println可以输出多种形式的数据.例如:println(String s),println(char c)等,11,read,write,101.3 File I/O,文件对象的建立File fp=new File(“tempfile.txt”);对文件操作要定义文件流FileInputStream类用来打开一个输入文件FileOutputStream类用来打开一个输出文件,12,101.3 File I/O,文件流的建立FileInputStream in=n
8、ew FileInputStream(fp);FileOutputStream out=new FileOutputStream(fp);例:文件拷贝(注意要捕获文件异常)输入流的参数是用于输入的文件名输出流的参数是用于输出的文件名,13,101.3 File I/O,import java.io.*;class filestream public static void main(String args)try File inFile=new File(”file1.txt);File outFile=new File(”file2.txt);FileInputStream fis=new
9、FileInputStream(inFile);FileOutputStream fos=new FileOutputStream(outFile);int c;while(c=fis.read()!=-1)fos.write(c);fis.close();fos.close();catch(FileNotFoundException e)System.out.println(FileStreamsTest:+e);catch(IOException e)System.err.println(FileStreamsTest:+e);,14,101.3 File I/O,增加缓冲区流,减少访问硬
10、盘的次数,提高效率,file1.txt,file2.txt,输入流,输出流,输入缓冲区,输出缓冲区,15,101.3 File I/O,缓冲区流:BufferedInputStream和 BufferedOutputStream将它们与文件流相接FileInputStream in=new FileInputStream(“file1.txt”);BufferedInputStream bin=new BufferedInputStream(in,25101)int len;byte bArray=new byte25101;len=bin.read(bArray);len中得到是长度,bAr
11、ray中得到的是数据,16,101.3 File I/O,只有缓冲区满时,才会将数据送到输出流.Java在输出数据流中,当对方尚未将数据取走时,程序就会被阻塞.有时要人为地将尚未填满的缓冲区中的数据送出,使用flush()方法.,文件,17,101.4 数据输入输出流,什么时候需要数据输入输出流?文件流和缓冲区流的处理对象是字节或字节数组,利用数据输入输出流可以实现对文件的不同数据类型的读写.DataInputStream、DataOutputStream一种较为高级的数据输入输出方式,除了字节和字节数组,还可以处理int,float,boolean等类型.还可以用readLine方法读取一行
12、信息可使用的方法:write,writeBoolean,read,readByte等,18,101.4 数据输入输出流,数据流的建立FileOutputStream fos=new FileOutputStream(”file2.txt);DataInputStream dis=new DataInputStream(fos)数据输出流可以是一个已经建立好的输入数据流对象,例如网络的连结,文件等.下面的例子显示如何利用数据输入输出流往文件中写不同类型的数据,19,101.4 数据输入输出流,class datainput_output public static void main(Strin
13、g args)throws IOException FileOutputStream fos=new FileOutputStream(“a.txt”);DataOutputStream dos=new DataOutputStream(fos);try dos.writeBoolean(true);dos.writeByte(byte)123);dos.writeChar(J);dos.writeFloat(2.7182f);dos.writeShort(short)11223);finally dos.close();,20,Streamdatainputandoutput-f5.bat,
14、101.4 数据输入输出流,DataInputStream dis=new DataInputStream(new FileInputStream(”a.txt);try System.out.println(t+dis.readBoolean();System.out.println(t+dis.readByte();System.out.println(t+dis.readChar();System.out.println(t+dis.readDouble();System.out.println(t+dis.readFloat();System.out.println(t+dis.rea
15、dInt();System.out.println(t+dis.readLong();System.out.println(t+dis.readShort();finallydis.close();,21,101.4 数据输入输出流,DateLine(InputStream in)(计算字符和行数)DataInputStream data=new DataInputStream(in);String currentLine;int lineCount=0;int charCount=0;while(currentLine=dataIn.readLine()!=null)+lineCount;c
16、harCount+=currentLine.length();return(charCount/(float)lineCount);,22,101.5 随机存取文件,类RandomAccessFile zip文件需要用随机方法处理文件目录给出个文件的入口,可以随机读取.创建一个随机文件new RandomAccessFile(“file1.txt”,“r”);new RandomAccessFile(“file2.txt”,“rw”);随机文件可以同时完成读和写操作.,23,101.5 随机存取文件,支持随机文件操作的方法:readXXX()或writeXXX()skipBytes();将指针
17、乡下移动若干字节seek():将指针调到所需位置getFilePointer():返回指针当前位置length():返回文件长度利用seek(long pos)方法查找随机文件中的信息例:把若干个32位的整数写到一个名为“temp.dat”的文件中,然后利用seek方法,以相反的顺序再读取这些数据,24,101.5 随机存取文件,public class random_file public static void main(String args)int data_arr=12,31,5101,23,27,1,43,1015,4,99;try RandomAccessFile randf=n
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 教学课件 教学 课件 101 数据流 运用
![提示](https://www.31ppt.com/images/bang_tan.gif)
链接地址:https://www.31ppt.com/p-5657586.html