Java教程流式输入输出与文件处理.docx

上传人:自*** 文档编号:126205509 上传时间:2020-03-23 格式:DOCX 页数:14 大小:56.31KB
返回 下载 相关 举报
Java教程流式输入输出与文件处理.docx_第1页
第1页 / 共14页
Java教程流式输入输出与文件处理.docx_第2页
第2页 / 共14页
Java教程流式输入输出与文件处理.docx_第3页
第3页 / 共14页
Java教程流式输入输出与文件处理.docx_第4页
第4页 / 共14页
Java教程流式输入输出与文件处理.docx_第5页
第5页 / 共14页
点击查看更多>>
资源描述

《Java教程流式输入输出与文件处理.docx》由会员分享,可在线阅读,更多相关《Java教程流式输入输出与文件处理.docx(14页珍藏版)》请在金锄头文库上搜索。

1、一、输入/输出的基本概念(1)输入/输出设备与文件外部设备:存储设备:硬盘、光盘、U盘等。输入输出设备:鼠标、键盘、扫描仪、显示器、打印机、绘图仪等。文件:输入输出设备可以看成是一类特殊的文件;文件可以看成是字节的序列;文件分为文本文件和二进制文件,文本文件存放的是ASCII码(或其他编码)表示的字符,而二进制文件是具有特定结构的数据。(2)流的概念Java中的输入/输出是以流的方式来处理的。流是在计算机的输入/输出操作中流动的数据系列。流中有未经加工的二进制数据,也有特定格式的数据。 流的分类: 输入流和输出流; 字节流和字符流。 流的特点: 先进先出。先被写入的数据,在读取时先被读取; 顺

2、序存取。不能随机访问中间的数据; 只读或只写。要么是输出流,要么是输入流。 针对一些频繁的设备交互,Java系统预先定义了3个可以直接使用: 标准输入(System.in):InputStream类型,通常代表键盘的输入; 标准输出(System.out):PrintStream类型,通常写往显示器; 标准错误输出(System.err):PrintStream类型,通常写往显示器。 可以使用下面的方法对流对象进行重定向: static voidsetIn(InputStream in):重新定义标准输入流; static voidsetErr(PrintStream err):重新定义标准错

3、误输出; static void setout(PrintStreamout):重新定义标准输出。二、面向字节的输入/输出流(1)面向字节的输入流面向字节的输入流都是InputStream的子类,此类为抽象类,类中定义了多个方法:public int read():读取一个字节;public int read(byte b):读多个字节到字节数组; public int read(byte b,int off,int len):从输入流读指定长度的数据到字节数组,数组从字节数组的off处开始存放; public long skip(long n):指针跳过n个字节; public void m

4、ark():在当前位置指针处做一标记; public void reset():将位置指针返回标记处; public void close():关闭流。使用InputStream类的子类文件输入流(FileInputStream)例题:9.1在屏幕上显示文件内容importjava.io.*;public classDisplayFile public static void main(String args) try FileInputStream infile=new FileInputStream(args0); intbyteRead=infile.read(); while(byte

5、Read!=-1) System.out.print(char)byteRead); byteRead=infile.read(); catch(ArrayIndexOutOfBoundsExceptione) System.out.println(需要提供一个文件名作为命令行参数); catch(FileNotFoundException e) System.out.println(file not find!); catch(IOException e) 改进此程序:每次读入多个字节。importjava.io.*;public classDisplayFile2 public stati

6、c void main(String args) try FileInputStream infile=new FileInputStream(args0); byte b=new byte100; int byteRead=0; while(byteRead=infile.read(b)!=-1) String s=new String(b); System.out.print(s); catch(ArrayIndexOutOfBoundsExceptione) System.out.println(需要提供一个文件名作为命令行参数); catch(FileNotFoundException

7、 e) System.out.println(file not find!); catch(IOException e) 按字符流读取:importjava.io.*;public classDisplayFile3 public static void main(String args) try FileInputStream infile=new FileInputStream(args0); String s;BufferedReaderln=new BufferedReader(new InputStreamReader(infile); while(s=ln.readLine()!=

8、null) System.out.println(s); catch(ArrayIndexOutOfBoundsExceptione) System.out.println(需要提供一个文件名作为命令行参数); catch(FileNotFoundException e) System.out.println(file not find!); catch(IOException e) 数据输入流(DataInputStream)为了规范对各类基本数据类型数据的读取操作,Java定义了DataInput接口,该接口中规定了基本类型数据的输入方法:lreadByte()lreadBoolean()

9、lreadShort()lreadCahr()lreadInt()lreadLong()lreadFloat()lreadDouble()DataInputStream类实现了DataInput接口。 利用DataInputStream类可以从键盘上得到一个整数吗?例题:9.2 从键盘上输入一个整数,求该数各位数字之和,并输出。import java.io.*;public class BitSum publicstatic void main(String args)throws IOException DataInputStreamdin=new DataInputStream(Syste

10、m.in); System.out.print(inputa integer:); intx=din.readInt(); intsum=0; intn=x; while(n0) int lastbit=n%10; n=n/10; sum=sum+lastbit; System.out.println(x+的各位数字之和=+sum); (2)面向字节的输出流 面向字节的输出流都是OutputStream的子类,此类为抽象类,类中定义了多个方法: public void write(int b):将参数b的低字节写入输出流; public void write(byte b):将字节数组写入输出

11、流; public voidflush():强制将缓冲区数据写入输出流对应的外设; public voidclose():关闭输出流;例题:9.3 将一个大文件分拆成若干个小文件。import java.io.*;public class BigToSmall publicstatic void main(String args) int number=0; final intsize=Integer.parseInt(args1); byte b=newbytesize; try FileInputStream infile=newFileInputStream(args0); while(true) FileOutputStream outfile=newFileOutputStream(file+number); number+; int byteRead=infile.read(b); if(byteRead=-1)break; outfile.write(b,0,byteRead); outfi

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > IT计算机/网络 > 其它相关文档

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