第七章:输入输出
7.1流式输入输出
- 两种基本流:InputStream(输入流), OutputStream(输出流)
- 根据流相对于程序的另一个端点不同可分两类:
- NodeStream(节点流/基层流):从特定源如磁盘文件或内存某区域进行读或写入
- FilterStream(过滤流/中层流):使用其它的流作为输入源或输出目的地
- 流式I/O类根据操作的数据类型(16位字符或8位字节)分成两个层次体系:
- 字节流:流中的数据以8位的字节为单位进行读写,一般用于处理图片、声音之类的二进制数据,以InputStream、OutputStream为基础类
- 字符流:流中的数据以16位字符为单位进行读写,专用于字符数据,处理文本文件,以Reader、Writer为基础类
1.字节流
- InputStream方法
//基本read方法
int read() //读一个字节返回, 如果到达流的末尾,则返回 -1
int read(byte[ ] ) // 将数据读入byte[], 返回读的字节数
int read( byte[], int offset, int length )
void close( ) //关闭流。自顶向下关闭Filter stream
int available() //返回未读的字节数
long skip(long n) // 跳过n个字节
boolean markSupported( ) //测试打开的流是否支持书签
void mark(int buf) //标记当前流,并建立buf大小缓冲区
void reset( ) // 返回标签处
- OutputStream方法
//三个基本的write( )方法
void write(int c) // 写一个字节
void write(byte[ ]) // 写一个字节数组
void write(byte[ ], int offset, int length )
void close( )
void flush( ) // 强行将缓冲的输出数据写出
2.字符流
-
Reader和Writer是字符流的两个抽象父类
-
Reader的基本方法
int read();//读单个字符
int read(char cbuf[]);//读字符放入数组中
int read(char cbuf[], int offset, int length);//读字符放入数组的指定位置
void close( ) //关闭流
long skip(long n) // 跳过n个字符
boolean markSupported( ) //测试打开的流是否支持书签
void mark(int) //标记当前流,并建立int大小缓冲区
void reset( ) // 返回标签处
boolean ready() //测试当前流是否准备好进行读
- Writer的基本方法
int write(int c); // 写单个字符
int write(char cbuf[]); // 写字符数组
int write(char cbuf[], int offset, int length);
int write(String str);
int write(String str, int offset, int length);
void close( ) //关闭流
void flush( ) //强行写
- 字节流与字符流的比较:Reader 和 InputStream以及Writer 与 OutputStream定义的API类似,但操作的数据类型不同
输入输出类型 | 流类型 | 描述 |
---|---|---|
File | FileReader FileWriter FileInputStream FileOutputStream | 统称为文件流。对文件进行读、写操作 |
Buffering | BufferedReader BufferedWriter BufferedInputStream BufferedOutputStream | 在读入或写出时,对数据进行缓存,以减少I/O的次数 |
3.文件流
-
文件流类包括:FileReader,FileWriter,FileInputStream,FileOutputStream
-
创建文件流:常用文件名或File类的对象创建文件流
-
//通过文件字节流实现文件的复制 import java.io.*; public class FileIn{ public static void main(String args[]){ File inputFile = new File("in.txt"); File outputFile = new File("out.txt"); FileInputStream IN = new FileInputStream(inputFile); FileOutputStream OUT = new FileOutputStream(outputFile); int c; while((c = IN.read()) != -1){ OUT.write(c); } IN.close(); OUT.close(); } } //通过文件字符流实现文件的复制 import java.io.*; public class FileIn{ public static void main(String args[]){ FileReader IN = new FileReader("in.txt"); FileWriter OUT = new FileWriter("out.txt"); int c; while((c = IN.read()) != -1){ OUT.write(c); } IN.close(); OUT.close(); } }
4.缓存流
-
包括BufferedInputStream/BufferedOutputStream类和BufferedReader/BufferedWriter类
-
缓冲流就是为数据流设置一个专门用于传送数据的一块内存区域(缓冲区)
-
缓存流把数据从原始流成块读入或把数据积累到一个大数据块后再成批写出, 通过减少系统资源的读写次数来加快程序的执行(提高读写效率)
-
//基本方法 int read() int read( byte[], int offset, int length ) int write(int c) void write(byte[ ], int offset, int length ) //BufferedReader增加readLine( ) 方法 ,该方法读一行字符返回 //BufferedWriter增加newLine( ) 方法 ,该方法写一个行分隔符
-
常用输入/输出流类:标准的输入/输出
-
Java通过System.in、System.out和System.err来实现标准输入输出和标准错误输出
-
每当main方法被执行时,就自动生成System.in、System.out和System.err三个对象
-
Scanner sc = new Scanner (System.in); BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
-
7.2文件
-
File类用来操作文件和获得文件的信息,但不提供对文件数据读取的方法,这些方法由文件流提供
-
创建文件流:常用文件名或File类的对象创建文件流
-
提供对文件进行创建目录、创建临时文件、改变文件名、删除文件等操作
提供获取文件信息的方法,如文件名、文件路径和文件长度等 -
//构造方法 public File(String pathname) public File(String parent,String child) //访问文件对象 public String getName( ) public String getPath( ) public String getAbsolutePath( ) public String getParent( ) public File getParentFile( ) //文件操作 public boolean renameTo(File dest) public boolean delete() //获得文件的属性 public long length() public boolean exists() public long lastMoidfied() //目录操作 public boolean mkdir()//创建目录 public String[ ] list() public File[ ] listFiles()
7.3随机存储文件
-
在文件的任意位置读或写数据,而且可以同时进行读和写的操作。RandomAccessFile类提供的对文件随机访问方式
-
//构造方法 public RandomAccessFile(File file,String mode) throws FileNotfoundException public RandomAccessFile(String name,String mode) throws FileNotfoundException
7.4对象串行化
-
把对象保存到外存,称为永久化
-
实现java.io.Serializable接口类的对象可以被输入/输出
-
只有对象的数据被保存,方法与构造方法不被保存(Serializable接口是个空接口,它只是一个表示对象可以串化的特殊标识)
-
以transient关键字标记的数据不被保存,声明暂时性变量
-
public class MyClass implements Serializable { public transient Thread myThread ; private transient String customerID; private int total; … }