java实用教程第16讲io(一)

上传人:tian****1990 文档编号:81527123 上传时间:2019-02-21 格式:PPT 页数:15 大小:344.31KB
返回 下载 相关 举报
java实用教程第16讲io(一)_第1页
第1页 / 共15页
java实用教程第16讲io(一)_第2页
第2页 / 共15页
java实用教程第16讲io(一)_第3页
第3页 / 共15页
java实用教程第16讲io(一)_第4页
第4页 / 共15页
java实用教程第16讲io(一)_第5页
第5页 / 共15页
点击查看更多>>
资源描述

《java实用教程第16讲io(一)》由会员分享,可在线阅读,更多相关《java实用教程第16讲io(一)(15页珍藏版)》请在金锄头文库上搜索。

1、第16讲 I/O(一),16.1 File类 16.2 文件过滤器 16.3 流 16.4 字节流和缓冲字节流 16.5 字符流和缓冲字节流 16.6 本讲小结,Java提供了丰富的类以实现与文件、控制台、网络等进行通信,而通信的方式可以按字节或字符的方式进行,可以是顺序读取数据,也可以是随机读取数据。,16.1 File类,File类的对象既可以表示文件,也表示目录。我们可以在相对路径下创建文件或目录,也可以在绝度路径下创建文件或路径。在创建文件或路径时,要特别注意路径中文件的分隔符,不同的操作系统中,文件分隔符是不同。“/”是UNIX下的分隔符,而Windows下为“”,为了使程序具有良好

2、的移植性,最好使用File.separator。,16.2 文件过滤器,我们还可以做一个文件过滤器,只列出符合条件的文件。下面代码列出当前目录下所有的.txt文件: public class FileFilter String suffix; public void showFiles(String path,String suffix) this.suffix = suffix; File file = new File(path);,String name = file.list(new MyFileFilter(); for(int i = 0;i name.length;i+) Sys

3、tem.out.println(namei); class MyFileFilter implements FilenameFilter public boolean accept(File dir,String name) return name.endsWith(suffix); ,16.3 流,Java使用流的方式读写硬盘、内存、键盘等设备上的数据。按照流的方向,可以分为输入流和输出流。按照要处理的数据类型,可以分为字节流和字符流。 java.io包中。所有输入流类是InputStream和Reader的子类,所有输出流类都是OutputStream和Writer的子类。其中InputS

4、tream和OutputStream表示字节流,Reader和Writer表示字符流。,16.4 字节流和缓冲字节流,字节流可以处理所有类型的数据,视频、音频、图片、文本等,读取时按照字节读取,读到一个字节就返回一个字节。,/字节流处理读写文本文件的示例 import java.io.*; public class RWByStreamOne public static void main(String args) try FileInputStream fis = new FileInputStream(“test.txt“); FileOutputStream fos = new File

5、OutputStream(“test_new.txt“); byte b = new byte10;int a = 0; while(a = fis.read(b)!=-1) fos.write(b, 0, a); fos.flush();fos.close();fis.close(); catch (IOException e) e.printStackTrace(); ,/缓冲字节流读取视频文件的示例 import java.io.*; public class RWByBufferedStream public static void main(String args) try File

6、InputStream fis = new FileInputStream(“a.mp4“); /内存的大小是经验值,可以通过多次运行程序而定 BufferedInputStream bis = new BufferedInputStream(fis,1024*100); FileOutputStream fos = new FileOutputStream(“a_new.mp4“); BufferedOutputStream bos = new BufferedOutputStream(fos,1024*100); byte b = new byte100; int a = 0;long s

7、tart = System.currentTimeMillis(); while(a = bis.read(b)!=-1) bos.write(b, 0, a); bos.flush();bos.close(); fos.close();bis.close();fis.close(); long stop = System.currentTimeMillis(); System.out.println(“所用时间:“+(stop-start)+“ms“); catch (IOException e) e.printStackTrace(); ,16.5 字符流和缓冲字符流,字符流只能处理纯文本

8、数据,读取时,读取一个或多个字符,然后查找指定的编码表,将查到的字符返回。,public class RWByReaderAndWriter public static void main(String args) try FileReader fis = new FileReader(“test.txt“); FileWriter fos = new FileWriter(“test_new.txt“); char b = new char10; int a = 0; while(a = fis.read(b)!=-1) fos.write(b, 0, a); fos.flush(); fo

9、s.close(); fis.close(); catch (IOException e) e.printStackTrace(); ,缓冲字符流可以提高字符流的读取效率,示例代码如下: public class RWByBufferedChar public static void main(String args) try FileReader fis = new FileReader(“test.txt“); BufferedReader br = new BufferedReader(fis); FileWriter fos = new FileWriter(“test_new.txt

10、“); BufferedWriter bw = new BufferedWriter(fos); String s; while(s = br.readLine()!=null) bw.write(s+“n“); bw.flush();bw.close();fos.close(); br.close();fis.close(); catch (IOException e) e.printStackTrace(); ,Java还提供了一个高效输出字符串的类:PrintWriter。示例代码如下:import java.io.*; public class RByPrintWriter publi

11、c static void main(String args) try FileReader fis = new FileReader(“test.txt“); BufferedReader br = new BufferedReader(fis); PrintWriter pw = new PrintWriter(“test_new.txt“); String s; while(s = br.readLine()!=null) pw.println(s); pw.flush();pw.close();br.close();fis.close(); catch (IOException e) e.printStackTrace(); ,16.6 本讲小结,本讲首先讲述了File类的用法,它即表示文件,也可以表示路径,使用File类中的方法还可以完成文件的过滤。其次讲解了字节流和字符流,并给出了相应的示例。,讲后练习,1、编写程序,列出给定路径下的文件名和目录文件名。 2、编写程序,列出当前目录下的所有.class文件。 3、编写程序,实现从文件中读出文件内容,并将其打印在屏幕当中,并标注上行号。,

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 高等教育 > 大学课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号