前言:对于java的输入输出流以前总是只知道用,对于具体细节一点也不清楚,今天整理了下这部分的总体框架,以后不断填补细节。
所有输入流类都是抽象类InputStream(字节输入流)或抽象类Reader(字符输入流)的子类;所有输出流都是抽象类OutputStream(字节输出流)或抽象类Writer(字符输出流)的子类。
输入流:
InputStream 类是字节输入流的抽象类,是所有字节输入流的父类。该抽象类中有如下一些方法:
方法 | 功能 |
---|---|
read() | 从输入流中读取下一个字节,如果读到末尾则返回-1 |
read(byte[]) | 从输入流中读取一定长度的字节,并返回读入的字节数 |
mark(int readlimit) | 在输入流的当前位置放置一个标记,参数表示此输入流在标记位置失效之前允许读取的字节数 |
reset() | 将输入指针返回到当前所做的标记处 |
skip(long n) | 跳过输入流上的n个字节,并返回实际跳过的字节数 |
markSupported() | 若流支持mark()和reset()则返回true |
close() | 关闭此输入流 |
java中的字符是unicode编码,双字节,然而InputStream是用来处理字节的,并不适合处理字符。所以java还提供了字符输入流Reader,Reader类是所有字符输入流类的父类。Reader中的方法和InputSteam中的方法类似。
输出流:
OutputStream类是字节输出流的抽象类,是所有字节输出流的父类。该抽象类中有如下一些方法:
方法 | 功能 |
---|---|
write(int b) | 将指定的字节写入输出流 |
write(byte[] b | 将字节数组写入输出流 |
write(byte[] b,int off,int len | 将字节数组中从off位置开始的len个字节写入输出流 |
flush() | 彻底完成输出流并清空缓存区 |
close() | 关闭输出流 |
Writer是字符输出流的抽象类,和Reader一样都是为了方便处理字符而设置。
File类:
File类是java.io包中代表磁盘文件本身的对象,可以通过调用File类中的方法,实现创建、删除、重命名、获取文件信息等操作。数据流可以将数据写入到文件。
使用File类创建一个文件对象由三种方法,这里介绍一种最常用的方法
File file = new File(String pathname)//其中pathname包含文件名
File类提供了一些操作文件的方法,其主要方法如下:
方法 | 功能 |
---|---|
getName() | |
exits() | |
length() | |
getAbsolutePath() | |
isDirectory() | |
createNewFIle() | |
delete() | |
… |
通过例子了解File对象的具体操作流程:
package io;
import java.io.*;
public class IO {
/**
* author: jack zhang
* @param args
*/
public static void main(String[] args) {
File file = new File("D://word.txt");
if (file.exists()){
String filename = file.getName();
System.out.println("文件名:"+filename);
file.delete();
System.out.println("文件已删除");
}
else{
try{
file.createNewFile();
System.out.println("文件已创建");
}
catch(Exception e){
e.printStackTrace();
}
}
}
}
------------------------------------------------------------
result:
文件名:word.txt
文件已删除
文件输入/输出流:
File类只是能够对文件本身进行一些操作,但是却不能对文件进行读取和写入操作,所以就需要和输入流和输出流类关联。于是就有了FileInputStream&&FileOutputStream以及FileReader&&FileWriter类。
FileInputStream&&FileOutputStream只提供了对字节或字节数组的读取方法,FileReader&&FileWriter则可以对字符数组进行操作。
对这两类方法的操作示例如下:
package io;
import java.io.*;
public class FileIO {
/**
* author: jack zhang
* @param args
*/
static File file = new File("D://word.txt");
public FileIO() {
if(!this.file.exists()){
try{
this.file.createNewFile();
System.out.println("文件创建成功");
}
catch(Exception e){
e.printStackTrace();
}
}
}
public static void example1(){
//FileInputStream&&FileOutputStream example
try{//向文件写数据
FileOutputStream out = new FileOutputStream(file);
byte[] b = "我爱祖国,贡献青春".getBytes();
out.write(b);//向文件输出流写入字节数组
out.close();
}
catch(Exception e){
e.printStackTrace();
}
try{//从文件读数据
FileInputStream in = new FileInputStream(file);
byte[] b = new byte[1024];
int len = in.read(b);//从文件输入流中读出数据到字节数组
System.out.println("文件中的信息为:"+new String(b,0,len));
in.close();
}
catch(Exception e){
e.printStackTrace();
}
}
public static void example2(){
//FileReader&&FileWriter example
try{
FileWriter fw = new FileWriter(file);
String info = "我爱祖国,做一个追梦人";
fw.write(info);
fw.close();
}
catch(Exception e){
e.printStackTrace();
}
try{
FileReader fr = new FileReader(file);
char[] ch = new char[1024];
int len = fr.read(ch);
fr.close();
System.out.println("文件中的信息为:"+new String(ch,0,len));
}
catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) {
System.out.println("使用FileInputStream&&FileOutputStream");
example1();
System.out.println("使用FileReader&&FileWriter");
example2();
}
}
--------------------------------------------------------------------
result:
使用FileInputStream&&FileOutputStream
文件中的信息为:我爱祖国,贡献青春
使用FileReader&&FileWriter
文件中的信息为:我爱祖国,做一个追梦人
带缓存的输入/输出流:
在前面的基础上,在内存中增加缓存区,使得在流上可以执行更多的操作。其主要的实现类有BufferedInputStream&&BufferedOutputStream以及BufferedReader&&BufferedWriter。
他们都有一个比较特殊的方法就是flush(),即在缓存区没有满的情况下,强制将缓存区的内容写入到外设,而不像close()方法,只有最后在关闭流的时候才将缓存区的内容写入到外设。
如下是带缓存的输入/输出的例子:
package io;
import java.io.*;
public class Buffered {
/**
* author: jack zhang
* @param args
*/
public static void main(String[] args) {
String[] content = {"我爱祖国","贡献青春","做一个追梦人"};
File file = new File("D://word.txt");
if(!file.exists()){
try{
file.createNewFile();
}
catch(Exception e){
e.printStackTrace();
}
}
try {
FileWriter fw = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fw);
for(int i=0;i<content.length;i++){
bw.write(content[i]);
bw.newLine();
}
bw.close();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
int i = 0;
String s = null;
try {
while((s = br.readLine())!=null){
i++;
System.out.println("第"+i+"行:"+s);
}
br.close();
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
-------------------------------------------------------------
result:
第1行:我爱祖国
第2行:贡献青春
第3行:做一个追梦人