资源预览内容
第1页 / 共49页
第2页 / 共49页
第3页 / 共49页
第4页 / 共49页
第5页 / 共49页
第6页 / 共49页
第7页 / 共49页
第8页 / 共49页
第9页 / 共49页
第10页 / 共49页
亲,该文档总共49页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
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 /关闭流 写(Writing) 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操作 标准输入/输出 文件读写操作 目录管理 随机访问文件 文件属性,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) 到达流的终点,无数据读出则返回-1 java.io.OutputStream (抽象类) public abstract void write(int b) public void write(byte b) public void write(byte b, int 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) 到达流的终点,无数据读出则返回-1 java.io.Writer (抽象类) public void write(int c) public void write(char cbuf) public void write(char cbuf, int offset, int length) 所有的读/写函数都抛出java.io.IOException,9,Java I/O类,I/O流的层次关系 class java.io.InputStream的子类 class java.io.ByteArrayInputStream class java.io.FileInputStream class java.io.FilterInputStream class java.io.BufferedInputStream class java.io.DataInputStream class java.io.ObjectInputStream class java.io.PipedInputStream class java.io.SequenceInputStream ,10,Java I/O类,I/O流的层次关系 class java.io.OutputStream的子类 class java.io.ByteArrayOutputStream class java.io.FileOutputStream class java.io.FilterOutputStream class java.io.BufferedOutputStream class java.io.DataOutputStream class java.io.PrintStream class java.io.ObjectOutputStream class java.io.PipedOutputStream ,11,Java I/O类,I/O流的层次关系 class java.io.Reader的子类 class java.io.BufferedReader class java.io.CharArrayReader class java.io.FilterReader class java.io.InputStreamReader class java.io.FileReader class java.io.PipedReader class java.io.StringReader ,12,Java I/O类,I/O流的层次关系 class java.io.Writer的子类 class java.io.BufferedWriter class java.io.CharArrayWriter class java.io.FilterWriter class java.io.OutputStreamWriter class java.io.FileWriter class java.io.PipedWriter class java.io.PrintWriter class java.io.StringWriter ,13,Java I/O类,I/O流的分类 (12个功能类),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) System.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 Test A Received number=3 C:,20,文件读/写流程 打开文件流 条件判断 读出/写入 关闭文件流 两种类型文件 FileInputStream/FileOutputStream (字节流) FileReader/FileWriter (字符流),文件读写操作,21,字节流构造方法 public FileInputStream(File file) throws FileNotFoundException public FileInputStream(String name) throws FileNotFoundException public File(String pathname) public FileOutputStream(File file) throws FileNotFoundException public FileOutputStream(File file, boolean append) throws FileNotFoundException /是否向已存在的文件后添加 public FileOutputStream(String name) throws FileNotFoundException public FileOutputStream(String name, boolean append) throws FileNotFoundException,文件读写操作,22,文件读写-实例1,文件读写操作,import java.io.*; public class CopyBytes public static 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 read ()throws IOException public int read (byte b) throws IOException public 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 IOException public void write (byte b) throws IOException public void write (byte b, int off, int len) throws IOException,23,字符流构造方法 public FileReader(File file) throws FileNotFoundException public FileReader(String fileName) throws FileNotFoundException public File(String pathname) public FileWriter(File file) throws IOException public FileWriter(File file, boolean append) throws IOException public FileWriter(String fileName) throws IOException public FileWriter(St
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号