(13.1-13.3)输入输出流
- 格式:ppt
- 大小:778.00 KB
- 文档页数:68


实验五 Java 输入输出流1.实验目的(1) 掌握输入输出流的总体结构;(2) 掌握流的概念;(3) 了解各种流(包括文件流、管道流、连接文件、过滤流、对象的序列化、随机访问)的使用。
2.实验内容实验题1 编写一个Java Application程序,打印命令行输入的所有参数。
实验题2 阅读下面程序,叙述其功能package .nwsuaf.jp;import java.io.FileReader;import java.io.IOException;public class FileViewer {/** Defines the entry point of the program. */public static void main(String[] args) {System.out.println("Please enter the file path:");try {String fileName = "";while(true) {int readByte = System.in.read();if(readByte == -1 || readByte == '\r')break;fileName += (char) readByte;}// Reads the file and prints it to the System.out stream.char[] buffer = new char[20];FileReader reader = new FileReader(fileName);while(true) {int length = reader.read(buffer);if(length < 0) // Reads a long as there is more data.break;String text = new String(buffer, 0, length);System.out.print(text);}} catch (IOException e) {e.printStackTrace();}}}本程序实现的功能是:把指定文件中的数据读到控制台窗口中。
输入、输出流总结一、理解数据流流一般分为输入流(Input Stream)和输出流(Output Stream)两类。
二、Java的标准数据流标准输入输出指在字符方式下(如DOS),程序与系统进行交互的方式,分为三种:标准输入studin,对象是键盘。
标准输出stdout,对象是屏幕。
标准错误输出stderr,对象也是屏幕。
三、字节流方法字节流:从InputStream和OutputStream派生出来的一系列类。
这类流以字节(byte)为基本处理单位。
InputStream、OutputStreamFileInputStream、FileOutputStreamPipedInputStream、PipedOutputStreamByteArrayInputStream、ByteArrayOutputStreamFilterInputStream、FilterOutputStreamDataInputStream、DataOutputStreamBufferedInputStream、BufferedOutputStream1、InputStream 和OutputStreamread():从流中读入数据skip():跳过流中若干字节数available():返回流中可用字节数mark():在流中标记一个位置reset():返回标记过得位置markSupport():是否支持标记和复位操作close():关闭流int read() :从输入流中读一个字节,形成一个0~255之间的整数返回(是一个抽象方法)。
int read(byte b[]) :读多个字节到数组中。
int read(byte b[], int off, int len):从输入流中读取长度为len的数据,写入数组b中从索引off开始的位置,并返回读取得字节数。
write(int b) :将一个整数输出到流中(只输出低位字节,抽象)write(byte b[]) :将字节数组中的数据输出到流中write(byte b[], int off, int len) :将数组b中从off指定的位置开始,长度为len的数据输出到流中flush():刷空输出流,并将缓冲区中的数据强制送出close():关闭流例:打开文件。