您的位置:首页 > 编程语言 > Java开发

Java中的输入输出IO流

2015-11-30 16:37 471 查看
上一篇里简单介绍了一下File类的一些常见方法与使用方法,可知File是用于描述一个文件或者文件夹的。

通过File对象我们可以读取文件或者文件夹的属性数据,如果我们需要读取文件的内容数据那么我们就需要使用IO流技术。

首先先看一下IO流的分类:

如果是按照数据的流向划分:输入流,输出流

如果是按照处理的单位划分:

字节流:字节流读取得都是文件中二进制数据,读取到二进制不会经过任何处理。

字符流:字符流读取的数据是以字符为单位的。字符流也是读取文件中的二进制数据,
不过会把这些二进制数据转换成我们能识别的字符。

字符流 = 字节流 + 解码


一.输入字节流:InputStream类

———| InputStream 所有输入字节流的基类 抽象类

————–| FileInputStream 读取文件数据的输入字节流

使用FileInputStream读取文件数据的步骤:

1.找到目标文件

2.建立数据的输入通道

3.读取文件中的数据

4.关闭资源。


如:

public static void  readTest3() throws IOException {

//找到目标文件
File file = new File("F:\\wangdan.txt");
//建立数据的输入通道
FileInputStream fileInputStream = new FileInputStream(file);
//建立缓冲数组配合循环读取文件的数据。
int length = 0;  //保存每次读取到的字节个数。
byte[] buf = new byte[1024];  //存储读取到的数据  缓冲数组的长度一般是1024的倍数,
理论上缓冲数组越大,效率越高

while((length = fileInputStream.read(buf))!= -1){
//read方法如果读取到了文件末尾,那么会返回-1表示。
System.out.println(new String(buf,0,length));
}

//关闭资源
fileInputStream.close();
}


二.输出字节流:OutputStream类

——| OutputStream 是所有输出字节流的父类。 抽象类

———-| FileOutputStream 向文件输出数据的输出字节流。

FileOutputStream如何使用呢?

1.找到目标文件

2.建立数据的输出通道。

3.把数据转换成字节数组写出。

4.关闭资源


FileOutputStream要注意的细节:

1.使用FileOutputStream 的时候,如果目标文件不存在,那么会自动创建目标文件对象。

2.使用FileOutputStream写数据的时候,如果目标文件已经存在,那么会先清空目标文件中的数据,
然后再写入数据。

3.使用FileOutputStream写数据的时候, 如果目标文件已经存在,需要在原来数据基础上
追加数据的时候应该使用new FileOutputStream(file,true)构造函数,第二参数为true。

4.使用FileOutputStream的write方法写数据的时候,虽然接收的是一个int类型的数据,
但是真正写出的只是一个字节的数据,只是把低八位的二进制数据写出,其他二十四位数据全部丢弃。


如:

//使用字节数组把数据写出。
public static void writeTest2() throws IOException{
//找到目标文件
File file = new File("F:\\b.txt");
//建立数据输出通道
FileOutputStream fileOutputStream = new FileOutputStream(file,true);
//把数据写出。
String data = "\r\nhello world";
fileOutputStream.write(data.getBytes());
//关闭资源
fileOutputStream.close();
}


三.缓冲输入字节流 :BufferedInputStream类

上面介绍了输入输出字节流的一些基础知识点,而且知道了读取文件数据使用缓冲数组读取效率更高,

所以sun公司给我们提供了一个缓冲输入输出字节流对象,让我们可以更加高效的读取文件。

BufferedInputStream 缓冲输入字节流

缓冲输入字节流的出现主要是为了提高读取文件数据的效率。

其实该类内部只不过是维护了一个8kb的字节数组而已。


注意:凡是缓冲流都不具备读写文件的能力。需要在构造方法中传入一个字节流对象。

使用BufferedInputStream的步骤:

1.找到目标文件

2.建立数据的输入通道

3.建立缓冲输入字节流

4.关闭资源


如:

public static void readTest2() throws IOException{
//找到目标文件
File file = new File("F:\\wangdan.txt");

FileInputStream  fileInputStream= new FileInputStream(file);
BufferedInputStream     bufferedInputStream= new BufferedInputStream(fileInputStream);
int content = 0 ;
while((content = bufferedInputStream.read())!=-1){
System.out.print((char)content);
}

//关闭资源
bufferedInputStream.close();//调用BufferedInputStream的close方法实际上关闭的是FileinputStream.
}


四.缓冲输出字节流 :BufferedOutputStream类

Bufferedoutputstream  缓冲输出字节流

BufferedOutputStream出现的目的是为了提高写数据的效率。

内部也是维护了一个8kb的字节数组而已。


使用BufferedOutputStream的步骤:

1. 找到目标文件

2. 建立数据的输出通道

3. 建立缓冲输出字节流

4. 关闭资源


BufferedOutputStream 要注意的细节

1. 使用BufferedOutStream写数据的时候,它的write方法是是先把数据写到它内部维护的字节数组中。

2. 使用BufferedOutStream写数据的时候,它的write方法是是先把数据写到它内部维护的字节数组中, 如果
需要把数据真正的写到硬盘上面,需要调用flush方法或者是close方法、 或者是内部维护的字节数组已经填满数据的时候。


如:

public static void main(String[] args) throws IOException {
//找到目标文件
File file = new File("F:\\a.txt");
//建立数据的输出通道
FileOutputStream  fileOutputStream = new FileOutputStream(file);
//建立缓冲输出字节流对象
BufferedOutputStream bufferedOutputStream  = new BufferedOutputStream(fileOutputStream);
//把数据写出
bufferedOutputStream.write("hello world".getBytes());
//把缓冲数组中内部的数据写到硬盘上面。
bufferedOutputStream.flush();
bufferedOutputStream.close();
}

}


五.输入字符流 :Reader类

———-| Reader 输入字符流的基类 抽象类

—————-| FileReader 读取文件的输入字符流。

Reader的常用方法:

1,int read():
读取一个字符。返回的是读到的那个字符。如果读到流的末尾,返回-1.

2,int read(char[]):
将读到的字符存入指定的数组中, 返回的是读到的字符个数, 也就是往数组里装的元素的个数。
如果读到流的末尾,返回-1.

3,close()
希望使用完毕后,进行资源的释放


FileReader的用法:

1. 找到目标文件

2. 建立数据的输入通道

3. 读取数据

4. 关闭资源


如:

public static void readTest2() throws IOException{
//找到目标文件
File file = new File("F:\\1208project\\day21\\src\\day21\\Demo1.java");
// 建立数据的输入通道
FileReader fileReader = new FileReader(file);
//建立缓冲字符数组读取文件数据
char[] buf = new char[1024];
int length = 0 ;
while((length = fileReader.read(buf))!=-1){
System.out.print(new String(buf,0,length));
}
}


六.输出字符流 :Writer类

——| Writer 输出字符流的基类。 抽象类

———–| FileWriter 向文件数据数据的输出字符流

FileWriter的使用步骤:

1. 找到目标文件。

2. 建立数据输出通道

3. 写出数据。

4. 关闭资源


FileWriter要注意的事项:

1. 使用FileWriter写数据的时候,FileWriter内部是维护了一个1024个字符数组的,写数据的时候会先写入到它
内部维护的字符数组中,如果需要把数据真正写到硬盘上,需要调用flush或者是close方法或者是填满了内部的字符数组。

2. 使用FileWriter的时候,如果目标文件不存在,那么会自动创建目标文件。

3.使用FileWriter的时候, 如果目标文件已经存在了,那么默认情况会先清空文件中的数据,然后再写入数据 ,
如果需要在原来的基础上追加数据,需要使用“new FileWriter(File , boolean)”的构造方法,第二参数为true。


如:

public static void  writeTest1() throws IOException{
//找到目标文件
File file = new File("F:\\a.txt");
//建立数据输出通道
FileWriter fileWriter = new FileWriter(file,true);
//准备数据,把数据写出
String data = "今天天气非常好!!";
fileWriter.write(data);  //字符流具备解码的功能。
//刷新字符流
fileWriter.flush();
//关闭资源
fileWriter.close();

}


七.缓冲输入字符流 :BufferedReader类

BufferedReader 缓冲输入字符流 。

缓冲输入字符流出现的目的是为了提高读取文件的效率和拓展了FileReader的功能。

其实该类内部也是维护了一个字符数组.


注意:缓冲流都不具备读写文件的能力。

BufferedReader的使用步骤:

1. 找到目标文件

2. 建立数据的输入通道。

3. 读取数据

4.关闭资源


如:

public static void main(String[] args) throws IOException {

//找到目标文件
File file = new File("F:\\wangdan.txt");
//建立数据的输入通道。
FileReader fileReader = new FileReader(file);
//建立缓冲输入字符流
BufferedReader bufferedReader = new BufferedReader(fileReader);
//读取数据
//      int content = bufferedReader.read(); //读到了一个字符。 读取到的字符肯定也是从Bufferedreader 内部的字符数组中获取的到。所以效率高。
//      System.out.println((char)content);

//使用BufferedReader拓展的功能,readLine()  一次读取一行文本数据,如果读到了文件末尾返回null表示。
String line = null;
while((line = bufferedReader.readLine())!= null){
//虽然readLine每次读取一行数据,但是读取到的line是不包含\r\n的。
System.out.println(line);
}

}


八.缓冲输出字符流 :BufferedWrite类

BufferedWriter 缓冲输出字符流

缓冲输出字符流作用: 提高FileWriter的写数据效率与拓展FileWriter的功能。

BufferedWriter内部只不过是提供了一个8192长度的字符数组作为缓冲区而已,拓展了FileWriter的功能。


BufferedWriter如何使用?

1. 找到目标文件

2. 建立数据的输出通道

3.写出数据

4.关闭资源


如:

public static void main(String[] args) throws IOException {
//找到目标文件
File file = new File("F:\\a.txt");
//建立数据的输出通道
FileWriter fileWriter = new FileWriter(file,true);
//建立缓冲输出流对象
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
//写出数据
bufferedWriter.newLine(); //newLine() 换行。 实际上就是向文件输出\r\n.
bufferedWriter.write("哈哈,天天开心!!");
//关闭资源
bufferedWriter.flush();
bufferedWriter.close();

}


九.序列流 :SequenceInputStream类

SequenceInputStream 表示其他输入流的逻辑串联。

它从输入流的有序集合开始,并从第一个输入流开始读取,直到到达文件末尾,接着从第二个输入流读取,
依次类推,直到到达包含的最后一个输入流的文件末尾为止。


构造函数:

SequenceInputStream(Enumeration<? extends InputStream> e)

SequenceInputStream(InputStream s1, InputStream s2)


1.合并两个流

使用构造函数: SequenceInputStream(InputStream s1, InputStream s2)


如:

public static void merge2() throws IOException{
//找到目标文件
File inFile1 = new File("F:\\a.txt");
File inFile2 = new File("F:\\b.txt");
File outFile = new File("F:\\c.txt");
//建立数据的输入输出通道
FileOutputStream fileOutputStream = new FileOutputStream(outFile);

FileInputStream fileInputStream1 = new FileInputStream(inFile1);
FileInputStream fileInputStream2 = new FileInputStream(inFile2);
//建立序列流对象
SequenceInputStream inputStream = new SequenceInputStream(fileInputStream1,fileInputStream2);
byte[] buf = new byte[1024];
int length = 0 ;

while((length = inputStream.read(buf))!=-1){
fileOutputStream.write(buf,0,length);
}
//关闭资源
inputStream.close();
fileOutputStream.close();

}


2.合并多个流:

使用构造函数:SequenceInputStream(InputStream s1, InputStream s2)


如:

public static void merge3() throws IOException{
//找到目标文件
File file1 = new File("F:\\a.txt");
File file2 = new File("F:\\b.txt");
File file3 = new File("F:\\c.txt");
File file4 = new File("F:\\d.txt");

//建立对应 的输入输出流对象
FileOutputStream fileOutputStream = new FileOutputStream(file4);
FileInputStream fileInputStream1 = new FileInputStream(file1);
FileInputStream fileInputStream2 = new FileInputStream(file2);
FileInputStream fileInputStream3 = new FileInputStream(file3);

//创建序列流对象
Vector<FileInputStream> vector = new Vector<FileInputStream>();
vector.add(fileInputStream1);
vector.add(fileInputStream2);
vector.add(fileInputStream3);
Enumeration<FileInputStream> e = vector.elements();

SequenceInputStream sequenceInputStream = new SequenceInputStream(e);

//读取文件数据
byte[] buf = new byte[1024];
int length = 0;

while((length = sequenceInputStream.read(buf))!=-1){
fileOutputStream.write(buf,0,length);
}

//关闭资源
sequenceInputStream.close();
fileOutputStream.close();

}


十.对象的输入输出流 :ObjectOutput和 ObjectInput

由于上述 ObjectOutput 和 ObjectInput 是接口,所以需要使用具体实现类。

ObjectOutput

ObjectOutputStream 被写入的对象必须实现一个接口:Serializable
否则会抛出:NotSerializableException


ObjectInput

ObjectInputStream 该方法抛出异常:ClassNotFountException


ObjectOutputStream 和 ObjectInputStream 对象分别需要字节输出流和字节输入流对象来构建对象。

也就是这两个流对象需要操作已有对象将对象进行本地持久化存储。

对象输入输出流要注意的细节:

1. 如果对象需要被写出到文件上,那么对象所属的类必须要实现Serializable接口。 Serializable接口
没有任何的方法,是一个标识接口而已。

2. 对象的反序列化创建对象的时候并不会调用到对象的构造方法。

3. serialVersionUID 是用于记录class文件的版本信息的,serialVersionUID这个数字是通过
一个类的类名、成员、包名、工程名算出的一个数字。

4. 使用ObjectInputStream反序列化的时候,ObjeectInputStream会先读取文件中的serialVersionUID,
然后与本地的class文件的serialVersionUID进行对比,如果这两个id不一致,那么反序列化就失败了。

5. 如果序列化与反序列化的时候可能会修改类的成员,那么最好一开始就给这个类指定一个serialVersionUID,
如果一类已经指定的serialVersionUID,然后在序列化与反序列化的时候,jvm都不会再自己算这个 class的serialVersionUID了。

6. 如果一个对象某个数据不想被序列化到硬盘上,可以使用关键字transient修饰。

7. 如果一个类维护了另外一个类的引用,那么另外一个类也需要实现Serializable接口。


如:

//定义方法把对象的信息写到硬盘上------>对象的序列化。
public static void writeObj() throws IOException{
//把user对象的信息持久化存储。
Address address = new Address("中国","成都");
User user = new User("admin","123",15,address);
//找到目标文件
File file = new File("F:\\obj.txt");
//建立数据输出流对象
FileOutputStream fileOutputStream = new FileOutputStream(file);
//建立对象的输出流对象
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
//把对象写出
objectOutputStream.writeObject(user);
//关闭资源
objectOutputStream.close();
}


//把文件中的对象信息读取出来-------->对象的反序列化
public static void readObj() throws  IOException, ClassNotFoundException{
//找到目标文件
File file = new File("F:\\obj.txt");
//建立数据的输入通道
FileInputStream fileInputStream = new FileInputStream(file);
//建立对象的输入流对象
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
//读取对象信息
User user = (User) objectInputStream.readObject();  //创建对象肯定要依赖对象所属的class文件。
System.out.println("对象的信息:"+ user);
}


十一.转换流:InputStreamReader和 OutputStreamWriter

转换流:可以包装我们的字节流,自动的完成节流编码和解码的工作。该流是一个 Reader/Writer 的子类,

是字符流的体系。所以将转换流称之为字节流和字符流之间的桥梁。

InputStreamReader  输入字节流的转换流是字节流通向字符流的桥.

OutputStreamWriter  输出字节流的转换流可以把输出字节流转换成输出字符流 。


转换流的作用:

1. 如果目前所 获取到的是一个字节流需要转换字符流使用,这时候就可以使用转换流。  字节流----> 字符流

2. 使用转换流可以指定编码表进行读写文件。


转换流的使用如下:

public static void readTest() throws IOException{

InputStream in = System.in; //获取了标准的输入流。

//把输入字节流转换成输入字符流。
InputStreamReader inputStreamReader = new InputStreamReader(in);

//使用字符流的缓冲类
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

String line = null;
while((line = bufferedReader.readLine())!=null){
System.out.println("内容:"+ line);
}
}


public static void writeTest() throws IOException{

File file = new File("F:\\a.txt");

FileOutputStream fileOutputStream = new FileOutputStream(file);

//把输出字节流转换成输出字符流。
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream);

outputStreamWriter.write("大家好");
outputStreamWriter.close();

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息