第08章 输入输出n课件.ppt
《第08章 输入输出n课件.ppt》由会员分享,可在线阅读,更多相关《第08章 输入输出n课件.ppt(49页珍藏版)》请在三一办公上搜索。
1、1,Java技术基础,2,第八章 输入输出,概念Java I/O类Java I/O操作标准输入/输出文件读写操作目录管理随机访问文件文件属性,3,概念,I/O来源控制台(console,如DOS窗口)打印/读入文件(file)读/写网络接口(TCP/UDP端口)读/写针对数据的读写以流(stream)的方式对数据进行操作,流的重要特性顺序读/写sequentially,4,概念,读/写流的一般流程读(Reading)open a stream/打开读出流while more information/判断 read information/读close the stream/关闭流写(Writi
2、ng)open a stream/打开写入流while more information/判断 write information/写close the stream/关闭流,5,概念,两种流的定义(读取信息的基本数据单位)字节流(byte stream):一个字节(8-bit)一个字节读/写字符流(character stream):一个字符一个字符读/写(具有特定字符编码的数据),j a v a 语 言6A 61 76 61 D3 EF D1 D4以字节流的方式读:读8次,8个字节以字符流的方式读:读6次,6个字符,6,第八章 文件输入输出,概念Java I/O类Java I/O操作标准输
3、入/输出文件读写操作目录管理随机访问文件文件属性,7,Java I/O类,字节流的读/写操作(来自JDK1.0)java.io.InputStream(抽象类)public abstract int read()public int read(byte b)public int read(byte b,int offset,int length)到达流的终点,无数据读出则返回-1java.io.OutputStream(抽象类)public abstract void write(int b)public void write(byte b)public void write(byte b,i
4、nt offset,int length)所有的读/写函数都抛出java.io.IOException,8,Java I/O类,字符流的读/写操作(来自JDK1.1)java.io.Reader(抽象类)public int read()public int read(char cbuf)public abstract int read(char cbuf,int offset,int length)到达流的终点,无数据读出则返回-1java.io.Writer(抽象类)public void write(int c)public void write(char cbuf)public voi
5、d write(char cbuf,int offset,int length)所有的读/写函数都抛出java.io.IOException,9,Java I/O类,I/O流的层次关系class java.io.InputStream的子类class java.io.ByteArrayInputStreamclass java.io.FileInputStreamclass java.io.FilterInputStreamclass java.io.BufferedInputStreamclass java.io.DataInputStream class java.io.ObjectInp
6、utStream class java.io.PipedInputStreamclass java.io.SequenceInputStream,10,Java I/O类,I/O流的层次关系class java.io.OutputStream的子类class java.io.ByteArrayOutputStreamclass java.io.FileOutputStreamclass java.io.FilterOutputStreamclass java.io.BufferedOutputStreamclass java.io.DataOutputStream class java.io.
7、PrintStreamclass java.io.ObjectOutputStreamclass java.io.PipedOutputStream,11,Java I/O类,I/O流的层次关系class java.io.Reader的子类class java.io.BufferedReaderclass java.io.CharArrayReaderclass java.io.FilterReader class java.io.InputStreamReaderclass java.io.FileReaderclass java.io.PipedReaderclass java.io.St
8、ringReader,12,Java I/O类,I/O流的层次关系class java.io.Writer的子类class java.io.BufferedWriterclass java.io.CharArrayWriterclass java.io.FilterWriterclass java.io.OutputStreamWriter class java.io.FileWriterclass java.io.PipedWriterclass java.io.PrintWriterclass java.io.StringWriter,13,Java I/O类,I/O流的分类(12个功能类
9、),14,Java I/O类,I/O流的分类(12个功能类),15,Java I/O类,I/O流的分类(12个功能类),16,第八章 文件输入输出,概念Java I/O类Java I/O操作标准输入/输出文件读写操作目录管理随机访问文件文件属性,17,Java I/O操作,主要内容标准输入/输出控制台屏幕打印和键盘读入文件I/O操作文件读写如何提高文件读写效率流的包装(Wrap)基本数据转换流目录管理随机访问文件(Random Access File)文件属性网络流操作,18,输出:控制台屏幕打印class Test public static void main(String args)Sy
10、stem.out.println(“Hello World!”);,标准输入/输出,19,输入:键盘读入import java.io.IOException;class Test public static void main(String args)throws IOException byte b=new byte10;System.out.println(Received number=+System.in.read(b);,标准输入/输出,C:java TestAReceived number=3C:,20,文件读/写流程打开文件流条件判断读出/写入关闭文件流两种类型文件FileInp
11、utStream/FileOutputStream(字节流)FileReader/FileWriter(字符流),文件读写操作,21,字节流构造方法public FileInputStream(File file)throws FileNotFoundExceptionpublic FileInputStream(String name)throws FileNotFoundExceptionpublic File(String pathname)public FileOutputStream(File file)throws FileNotFoundExceptionpublic FileO
12、utputStream(File file,boolean append)throws FileNotFoundException/是否向已存在的文件后添加public FileOutputStream(String name)throws FileNotFoundExceptionpublic FileOutputStream(String name,boolean append)throws FileNotFoundException,文件读写操作,22,文件读写-实例1,文件读写操作,import java.io.*;public class CopyBytes public stati
13、c void main(String args)throws IOException File inputFile=new File(“original.txt);File outputFile=new File(“result.txt);FileInputStream in=new FileInputStream(inputFile);FileOutputStream out=new FileOutputStream(outputFile);int c;while(c=in.read()!=-1)out.write(c);in.close();out.close();,public int
14、read()throws IOExceptionpublic int read(byte b)throws IOExceptionpublic int read(byte b,int off,int len)throws IOException,FileInputStream in=new FileInputStream(“original.txt);FileOutputStream out=new FileOutputStream(“result.txt);,public void write(int b)throws IOExceptionpublic void write(byte b)
15、throws IOExceptionpublic void write(byte b,int off,int len)throws IOException,23,字符流构造方法public FileReader(File file)throws FileNotFoundExceptionpublic FileReader(String fileName)throws FileNotFoundExceptionpublic File(String pathname)public FileWriter(File file)throws IOExceptionpublic FileWriter(Fi
16、le file,boolean append)throws IOExceptionpublic FileWriter(String fileName)throws IOExceptionpublic FileWriter(String fileName,boolean append)throws IOException,文件读写操作,24,文件读写-实例2,文件读写操作,import java.io.*;public class Copy public static void main(String args)throws IOException File inputFile=new File
17、(“original.txt);File outputFile=new File(“result.txt);FileReader in=new FileReader(inputFile);FileWriter out=new FileWriter(outputFile);int c;while(c=in.read()!=-1)out.write(c);in.close();out.close();,FileReader in=new FileReader(“original.txt);FileWriter out=new FileWriter(“result.txt);,public int
18、read()throws IOExceptionpublic int read(char cbuf)throws IOExceptionpublic int read(char cbuf,int off,int len)throws IOException,public void write(int b)throws IOExceptionpublic void write(char cbuf)throws IOExceptionpublic void write(char cbuf,int off,int len)throws IOException,25,两类缓冲流针对字节流java.io
19、.BufferedInputStream类java.io.BufferedOutputStream类针对字符流java.io.BufferedReader类java.io.BufferedWriter类,如何提高文件读写效率,26,构造方法public BufferedInputStream(InputStream in)2048 bytespublic BufferedInputStream(InputStream in,int size)public BufferedOutputStream(OutputStream out)512 bytespublic BufferedOutputSt
20、ream(OutputStream out,int size)public BufferedReader(Reader in)8192 bytespublic BufferedReader(Reader in,int sz)public BufferedWriter(Writer out)8192 bytepublic BufferedWriter(Writer out,int sz),如何提高文件读写效率,27,String filename=“test.txt”;FileInputStream fis=new FileInputStream(filename);int count=0;in
21、t c;while(c=fis.read()!=-1)if(c=A)count+;fis.close();System.out.println(count);,如何提高文件读写效率-实例1,如何提高文件读写效率,String filename=“test.txt”;FileInputStream fis=new FileInputStream(filename);BufferedInputStream bis=new BufferedInputStream(fis);int count=0;int c;while(c=bis.read()!=-1)if(c=A)count+;fis.close
22、();System.out.println(count);,FileInputStream fis=new FileInputStream(“test.txt”);int count=0;final int BUFSIZE=1024;byte buf=new byteBUFSIZE;int len;while(len=fis.read(buf)!=-1)for(int i=0;i len;i+)if(bufi=A)count+;fis.close();,28,String filename=“test.txt”;FileReader fr=new FileReader(filename);Bu
23、fferedReader br=new BufferedReader(fr);int count=0;while(br.readLine()!=null)count+;br.close();System.out.println(count);,如何提高文件读写效率-实例2,如何提高文件读写效率,29,概述,流的包装(wrap),FileReader fr=new FileReader(filename);BufferedReader br=new BufferedReader(fr);,BufferedReader br=new BufferedReader(new FileReader(fi
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 第08章 输入输出n课件 08 输入输出 课件
![提示](https://www.31ppt.com/images/bang_tan.gif)
链接地址:https://www.31ppt.com/p-2163183.html