Java语言程序设计-第10章课件

上传人:我*** 文档编号:140094685 上传时间:2020-07-26 格式:PPT 页数:23 大小:178.50KB
返回 下载 相关 举报
Java语言程序设计-第10章课件_第1页
第1页 / 共23页
Java语言程序设计-第10章课件_第2页
第2页 / 共23页
Java语言程序设计-第10章课件_第3页
第3页 / 共23页
Java语言程序设计-第10章课件_第4页
第4页 / 共23页
Java语言程序设计-第10章课件_第5页
第5页 / 共23页
点击查看更多>>
资源描述

《Java语言程序设计-第10章课件》由会员分享,可在线阅读,更多相关《Java语言程序设计-第10章课件(23页珍藏版)》请在金锄头文库上搜索。

1、第十章,输入与输出,一、输入输出类库,Java的输入输出功能必须借助于输入输出类库Java.io包来实现,这个包中的类大部分是用来完成流式输入输出的流类。,流,流是指在计算机的输入与输出之间的数据的序列,而Java中的数据流有位流(字节流)和字符流之分; 就流的运动方向而言,流可分为输入流(input stream)和输出流(output stream),输入流代表从外设流入计算机的数据序列;输出流代表从计算机流向外设的数据序列。,输入输出流类,在Java的流类中,最基本的类有两个: 输入流类InputStream和输出流类OutputStream。这两个是具有最基本的输入输出功能的抽象类,其

2、他流类都是为了方便处理各种特定流而设置的,属于InputStream或OutputStream的子类。,InputStream类,int read( ) long skip( ) void mark( ) void close( ) void reset( ),InputStream FileInputStream FilterInputStream DataInputStream BufferedInputStream,InputStream类的主要方法,InputStream子类的继承关系,OutputStream类,void write( ) void flush( ) void clo

3、se( ),OutputStream FileOutputStream FilterOutputStream PrintStream DataOutputStream BufferedOutputStream,OutputStream类的主要方法,OutputStream子类的继承关系,Reader类,int read( ) long skip( ) void mark( ) void close( ) void reset( ),Reader BufferedReader: InputStreamReader FileReader,Reader类的主要方法,Reader子类的继承关系,Wri

4、ter类,void write( ) void flash( ) void close( ),Writer PrintWriter BufferedWriter OutputStreamWriter FileWriter,Writer类的主要方法,Writer子类的继承关系,二、标准输入输出,Java系统预先定义好3个流对象分别表示标准输出设备、标准输入设备和标准错误设备,它们分别是System.out,System.in和System.err。 System.out是它的一个静态属性,属于PrintStream类对象,用于输出字节数据流,对应标准输出设备:屏幕。 System.in 也是Sy

5、stem的一个静态属性,属于InputStream类对象,用于输入字节数据流,对应标准输入设备:键盘。 System.err 也是System的一个静态属性,属于PrintStream类对象,用于系统错误信息的输出,对应屏幕。,Java实例标准输入,import java.io.*; public class StandardIn1 public static void main(String args) throws IOException char c; System.out.println( 输入一个字符); c=(char)System.in.read(); System.out.pr

6、int( 输入的字符是:+c); ,标准输出,Java的标准输入设备:显示器用System.out表示,System.out属于PrintStream类对象。 利用PrintStream类的print()或println()方法可以非常方便地输出各类数据,这两个方法的唯一区别是print()输出后不换行,而println()方法输出完毕后要换行 。,三、文件操作,在程序中要对磁盘文件或目录进行操作,首先要对文件或目录建立连接,为此Java提供了File类。File类也位于java.io包中,但不是流类,而是专门用来管理磁盘文件和目录。 一个File类对象表示一个磁盘文件或目录,其对象属性中包含

7、了文件或目录的相关信息,如名称、长度、所含文件个数等,其方法可以完成对文件或目录的常用管理操作,如创建、删除等。,File类,File类提供了3个不同的构造方法 : File(String path) String类参数path指定所建对象对应的磁盘文件名或目录名及其路径名。 File(String path, String name) 此构造方法中的参数path表示文件或目录的路径,参数name表示文件或目录名。 File(File dir, String name) 此构造方法中的参数dir表示一个磁盘目录对应的File对象,参数name表示文件名或目录名。,File类主要方法,publi

8、c String getName():得到文件的名字 public String getPath():得到文件的路径名 public boolean exists():判断文件或目录是否存在 public long length():返回文件的字节数 public boolean canRead():返回当前文件是否可写 public boolean canWrite():返回当前文件是否可读 public boolean equals(File file):比较文件或目录 public boolean isFile():检测是否是文件 public boolean isDirectiry()

9、:检测是否是目录 public boolean renameTo(File file):重命名文件 public void delete():删除文件,FileOutputStream类流,FileOutputStream流类的构造方法有两个: FileOutputStream(String fileName): 参数fileName表示带路径的磁盘文件名。 FileOutputStream(File file): 参数file表示为磁盘文件所建立的File对象名,Java实例FileOutputStream,import java.io.*; public class File2 publi

10、c static void main(String args) throws IOException char ch; File file1=new File(c:jdk1.3examplenewFile.txt); try FileOutputStream fout= new FileOutputStream(file1); System.out.println(输入任一字符串,以?结束); ch= (char) System.in.read(); while (ch !=?) fout.write(ch); ch=(char) System.in.read(); fout.close();

11、 catch (FileNotFoundException e) System.out.println(e); catch (IOException e) System.out.println(e); ,FileInputStream类流,FileInputStream流类的构造方法有两个: FileInputStream(String fileName): 参数fileName表示带路径的磁盘文件名。 FileInputStream(File file): 参数file表示为磁盘文件所建立的File对象名 。,Java实例FileInputStream,import java.io.*; p

12、ublic class File3 public static void main(String args) throws IOException int ch; File file1=new File(c:jdk1.3examplenewFile.txt); try FileInputStream fin= new FileInputStream(file1); System.out.println(文件中的信息为:); ch= fin.read(); while (ch !=-1) System.out.print(char)ch); ch =fin.read(); fin.close()

13、; catch (FileNotFoundException e) System.out.println(e); catch (IOException e) System.out.println(e); ,DataOutputStream类流,使用DataOutputStream类向文件中写入各种类型数据的操作步骤是: 为磁盘文件建立File类对象; 为该File对象建立FileOutputStream类流对象,建立其与磁盘文件的连接; 为该FileOutputStream类对象建立DataOutputStream类对象,利用DataOutputStream类的writeInt(),write

14、Float(),writeDouble(),writeBoolean()等方法分别向文件中写入整型、单精度型、双精度型、布尔型等数据; 写入操作完成后,利用close()方法将流关闭,断开与磁盘文件的联系。,Java实例DataOutputStream,import java.io.*; public class File4 public static void main(String args) int ch; InputStreamReader iin=new InputStreamReader(System.in); BufferedReader bin =new BufferedRea

15、der(iin); File file1=new File(c:jdk1.3exampledataFile.txt); try FileOutputStream fout= new FileOutputStream(file1); DataOutputStream dout =new DataOutputStream(fout); System.out.println( 输入整数); int i=Integer.parseInt(bin.readLine(); System.out.println( 输入浮点数);,Java实例(续),float f=Float.parseFloat(bin.

16、readLine(); System.out.println( 输入布尔量); boolean b=new Boolean(bin.readLine().booleanValue(); dout.writeInt(i); dout.writeFloat(f); dout.writeBoolean(b); dout.close(); catch (FileNotFoundException e) System.out.println(e); catch (IOException e) System.out.println(e); ,Writer和Reader,以字符流方式向文件写入或从文件中读取数据,可以使用Writer和Reader类及其子类。 Writer和Reader类都是抽象类,不能建立它们的对象,所以只能通过它们子类对象对文件进行操作。常用的Writer类的子类包括FileWriter类和BufferedFileWriter类。 Fi

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

当前位置:首页 > 办公文档 > PPT模板库 > PPT素材/模板

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