




已阅读5页,还剩27页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
文件组件 I/O 流 标准 输入输出 q文件 输入输出 行 言 输入输出 I/O软件层 设备驱动程序 设备驱动程序 设备驱动程序 设备驱动程序 设备驱动程序OS 独 立 于 设 备 的 I/O 层 java.io 提供 I/O层 (类层 ) 应用 程序 应用 程序 应用 程序 java.io的核心 n 文件( File类) = 各种文件系统提供的基本服务一样,但实现细节互不 兼容。 = 解决 java程序与文件系统的沟通。 n 流 线性的顺序的输入输出数据流 读输入流 写 输出流 I/O 流类 标准文件类 : File n File类的对象是文件系统中的一个目录或文件的 抽象表示。 n File类对象描述文件路径、名字、长度、可否读 写等属性,可用来命名文件、查询文件属性、对 目录进行操作,但不读写文件。 n 上述操作都是以独立于系统的方式进行。 n 通过 File类对象可以对操作系统的文件进行管理 ,体现了跨平台不同文件的统一管理 File类构造函数 n File(String path) = 封装文件例(使用相对路径, 移植性较好) File f1 = new File(“mydirmyfile.txt”); = 封装目录例(使用绝对路径) File f2 = new File(“d:mydirdir1”); n File(String parent, String child ) File f3 = new File(“d:d1” , “a.java”) n File(File dir, String name) File f4 = new File(f2 , ”myfile.txt”); win unix / File类方法 获取文件属性 n 判断目标是否存在 public boolean exists() n 判断是文件还是目录 public boolean isFile(); public boolean isDirectory(); n 获取文件名称或整个路径 public String getName() 仅返回文件名 public String getPath() 返回整个路径字符串 n 获取文件长度 public long length() n 获取文件读写属性 public boolean canRead() public boolean canWrite() n 列出目录中的文件 public String list() public File listFiles() File类方法 文件 /目录操作 n 重命名 public boolean renameTo(File new) n 删除文件或目录 public boolean delete(); n 创建目录 public boolean mkdir() n 创建目录及父目录 (如果需要的话) public boolean mkdirs() n 创建临时文件 public static File createTempFile(文件名 ,后缀 ) throws IOException deleteOnExit() 1、 2 文件 I/O类: RandomAccessFile n 视文件如同一个字节类型数组,数组下标即文件 指针。读写操作都会移动指针。 n 构造函数 RandomAccessFile(File file, String mode) RandomAccessFile(String name, String mode) 文件 I/O类: RandomAccessFile n 指针操作 long getFilePointer() 返回相对于文件头的位移量 单位:字节 该位置即下一个读写操作的位置。 void seek(long pos) 移动指针 (位移单位:字节 ) int skipBytes(int n) 移动指针 n 关闭文件 close() n 取文件长度 length() 文件 I/O类: RandomAccessFile n 写文件 writeInt(int v) 写整型数 4个字节,高字节在前 writeBoolean(boolean v) 一个字节, 0或 1 writeUTF(String str) 写一字符串,前两个字节标明字符串字节长度 文件 I/O类: RandomAccessFile n 读文件 byte readByte() 读一字节 char readChar() 读一 字符( Unicode码 2个字节) double readDouble() 读双精度数( 8个字节) String readUTF() 读一个字符串。 readLine() 读一行文本(对每一个字节,配上一个 置 0高字节,构成一个双字节字符。不支持 Unicode码 3 方法测试 4 学生信息 RandomAccessFile n 构造函数的例外: = IllegalArgumentException ( not “r“ or “rw“) = FileNotFoundException = SecurityException n 文件操作例外: = EOFException (IOException) 读到文件尾 = IOException 虽没有到文件尾,但读不到字符 = IOException 文件已关闭 InputStream输入流 n 管理字节(适于读取面向字节的数据) n 是所有表示输入字节流类的父类(抽象类 ) n 三个基本方法: = abstract int read() 从输入流中读下一个字节。 = int read(byte b) 从输入流中读若干个字节到数组中。 = int read(byte b , int off , int len ) 从输入流中读 len个字节到数组中。 off是写入数 组的位置(位移)。 InputStream输入流 n skip(long n) 跳过 n个字节。 n boolean markSupported() 流是否支持 mark功能 n mark(int readlimit) 在当前位置做标记 . n reset() 回到最近一次做的标记处。 n close() 关闭输入流,释放与此输入流相连 的系统资源。 nByteArrayInputStream( byte数组 ) nFileInputStream( 文件路径名 或 File对象) nObjectInputStream ( Inputstream in) nPipedInputStream (PipedOutputStream pipe) nSequenceInputStream 表示其他输入串的逻辑连接 nFilterInputStream( 其他输入流)定义了子类对流的进 一步处理功能。 nBufferedInputStream (InputStream in) nDataInputStream(InputStream in) 各种数据读入 InputStream输入流的子类 n 这些子类的构造函数都可以用某种方式指定其数据源。 n 加强输入流,对 InputStream类 进行功能扩充 OutputStream输出流 n 抽象类:是所有表示输出字节流类的父类。 n 功能:接受要输出的字节并将它送往目的地。 n 方法: write(int b) 往输出流写一个字节 write(byte b) 将字节数组数据写入输出流。 flush() 刷新输出流,并使缓冲区中的数据写出。 close() 关闭输出流,释放与之相连的系统资源。 OutputStream输出流子类 n FileOutputStream( File类对象或文件名) , n ByteArrayOutputStream() 数据被写到无名字节数组,该字节数组内容可利用 toByteArray() 和 toString()分别取到指定字节数组 和字符串中 。 n PipedOutputStream (PipedInputStream pipe) n ObjectOutputStream n FilterOutputStream = DataOutputStream(OutputStream out) 包含输出各种数据类型数据的方法,如 writeFloat() = PrintStream(OutputStream out) 包含输出各种数据类型数据的方法,如 print(),println。 但没有对应输入流 两种流类 n node stream 对指定的地方(磁盘文件、内存 等 ) 读 /写 n filter stream 一个 filter 流使用 node流作为输入或输出。 目 的 地 数 据 源 基本流类 node流 n FileInputStream FileOutputStream FileInputStream infile = new FileInputStream(“old.dat”) ; FileOutputStream outfile = new FileOutputStream(“new.dat”); 基本流类 filter流 n BufferedInputStream BufferedOutputStream 增加 I/O操作的有效性 n DataInputStream DataOutputStream 可以读写 Java基本类型的数据 byte readByte() void writeByte(byte) long readLong() void writeLong(long) double readDouble() void writeDouble(double) 使用 I/O流读写文件 n 输出 DataOutputStream out = new DataOutputStream(new FileOutputStream(“test3“) 写 文件: out.writeUTF(“wang hong“); out.writeFloat(485.2F); n 输入 DataInputStream in = new DataInputStream(new FileInputStream(“test3“); 读 文件: in.readUTF() in.readFloat() 字符流 - Reader类 n 是所有读取字符流类的父类抽象类 (面向 Unicode 字符操作 ) Java使用 Unicode码表示字符和字符 串。 方法: n boolean ready() 输入字符流是否可读 n int read() 读取一个字符 n int read(char cbuf) 读取一串字符 (到字符数组 ) n long skip(long n) 跳过 n个字符 n mark(int readAheadLimit) 在 当前位置做一标记 n reset() 将读取位置恢复到标记处 n close() 关闭字符流 Reader类子类 n CharArrayReader(char buf) n PipedReader(PipedWriter) n StringReader(String s) n BufferedReader(Reader in) = 提供有效读的方法,如: readLine 字节流字符流 读 字符 InputStreamReader(InputStream in) FileReader(File对象 或 文件名) Writer类 n 是所有表示输出字符流类的父类(抽象类)。 n 功能:接受要输出的字符并将它送往目的地。 n 方法: n write(String str) n write(char cbuf) n flush() n close() Writer类的 子类 n CharArrayWriter() toCharArray() and toString(). n StringWriter () 方法 toString n PipedWriter(PipedReader) n BufferedWriter(Write out) 提高 I/O效率 = 方法: write() 写字符或字符串 n PrintWriter( OutputStream类或 Writer类对象 ) = 方法: print println 实现各种类型数据的输出 字节流字符流 写字符 OutputStreamWriter(OutputStream out) FileWriter(File对象或文件名 ) 文件读写(文本) n 输入 BufferedReader fin = new BufferedReader( new FileReader(“test.txt“) ); 读 文件 fin.readln() n 输出 PrintWriter fout = new PrintWriter( new FileWriter(“test.txt“) , true) ); 或 : PrintWriter fout = new PrintWriter( new BufferedWriter(new FileWriter(“test.txt“); 写 文件 fout.println() flush() close() System类及标准输入输出 n static PrintStream err 标准错误输出流 n static InputStream in 标准输入流 n static PrintStream out 标准输出流 n OutputStream = FilterOutputStream PringStream 提供输出各种类型数据的方法( print ,println) 不抛出 I/O例外 (可用 checkError 方法检测) 可设置自动刷新 System.out.println() System类及标准输入输出 BufferedReader stdin = new BufferedReader( new InputStreamReader(System.in) ); n stdin.read() 读一个字符 n String readLine() 读 一行 n read(char cbuf, int off, int len) 将 指定数量 (len) 字符读到字符数组中的指定位置( off) n InputStreamReader 没有 readLine() 方法 n BufferedReader(Reader) 构造函数 5 输入输出流例 -对象流 FileOutputStream ostream = new FileOutputStream(“t.tmp“); ObjectOutputStream p = new ObjectOutputStream(ostream); p.writeInt(12345); p.writeObject(“Today“); p.write
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 招标采购从业人员考试(招标采购专业实务初、中级)试题库及答案(上海市2025年)
- 《祝福》课件四课时
- 我能打败怪兽绘本解读
- 广东省湛江市麻章区2023-2024学年高一上学期第一次月考数学考点及答案
- 公司新厂房布局规划课件
- 早期胃癌名词解释护理
- 2025绿洲集团商品房代理销售合同
- 2025年电子设备全国分销代理合同范本
- 骨科个案护理比赛案例汇报大纲
- 2025工程担保的合同范本
- 人工智能算力中心项目环境影响报告书
- 无人机飞防应急处置预案
- 四川蜀道养护集团有限公司招聘笔试题库2025
- 高一历史第一次月考卷02(考试版)(新高考适用)
- 报废产品处置合同范本
- 水平定向钻施工专项方案施工技术方案
- 2025年《临床执业医师》考试试卷及参考答案
- 儿科泌尿道感染护理查房
- 2025年国防知识竞赛题库及答案(共300题)
- 胃肠胰神经内分泌肿瘤诊疗指南2025年版
- 护理人力资源培训
评论
0/150
提交评论