没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
流的概念和作用
学习 Java IO,不得不提到的就是 JavaIO 流。
流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象。
即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输特性将流抽
象为各种类,方便更直观的进行数据操作。
IO 流的分类
根据处理数据类型的不同分为:字符流和字节流
根据数据流向不同分为:输入流和输出流
字符流和字节流
字符流的由来: 因为数据编码的不同,而有了对字符进行高效操作的流对象。
本质其实就是基于字节流读取时,去查了指定的码表。字节流和字符流的区别:
(1)读写单位不同:字节流以字节(8bit)为单位,字符流以字符为单位,
根据码表映射字符,一次可能读多个字节。
(2)处理对象不同:字节流能处理所有类型的数据(如图片、avi 等),而字
符流只能处理字符类型的数据。
(3)字节流在操作的时候本身是不会用到缓冲区的,是文件本身的直接操作
的;而字符流在操作的时候下后是会用到缓冲区的,是通过缓冲区来操作文件,我
们将在下面验证这一点。
结论:优先选用字节流。首先因为硬盘上的所有文件都是以字节的形式进行传
输或者保存的,包括图片等内容。但是字符只是在内存中才会形成的,所以在开发
中,字节流使用广泛。
输入流和输出流
对输入流只能进行读操作,对输出流只能进行写操作,程序中需要根据待传输
数据的不同特性而使用不同的流。
Java 流操作有关的类或接口:
Java 流类图结构:
Java IO 流对象
1. 输入字节流 InputStream
定义和结构说明:
从输入字节流的继承图可以看出:
InputStream 是所有的输入字节流的父类,它是一个抽象类。
ByteArrayInputStream、StringBufferInputStream、FileInputStream 是三种
基本的介质流,它们分别从 Byte 数组、StringBuffer、和本地文件中读取数据。
PipedInputStream 是从与其它线程共用的管道中读取数据,与 Piped 相关的知识后
续单独介绍。
ObjectInputStream 和所有 FilterInputStream 的子类都是装饰流(装饰器模
式的主角)。意思是 FileInputStream 类可以通过一个 String 路径名创建一个对象,
FileInputStream(String name)。而 DataInputStream 必须装饰一个类才能返回一
个对象,DataInputStream(InputStream in)。如下图示:
实例操作演示:
【案例 】读取文件内容
?
1
/**
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
* 字节流
* 读文件内容
* */
import java.io.*;
class hello{
public static void main(String[] args) throws IOException {
String fileName="D:"+File.separator+"hello.txt";
File f=new File(fileName);
InputStream in=new FileInputStream(f);
byte[] b=new byte[1024];
in.read(b);
in.close();
System.out.println(new String(b));
}
}
注意:该示例中由于 b 字节数组长度为 1024,如果文件较小,则会有大量填充
空格。我们可以利用 in.read(b);的返回值来设计程序,如下案例:
【案例】读取文件内容
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
* 字节流
* 读文件内容
* */
import java.io.*;
class hello{
public static void main(String[] args) throws IOException {
String fileName="D:"+File.separator+"hello.txt";
File f=new File(fileName);
InputStream in=new FileInputStream(f);
byte[] b=new byte[1024];
int len=in.read(b);
in.close();
System.out.println("读入长度为:"+len);
System.out.println(new String(b,0,len));
}
}
注意:观察上面的例子可以看出,我们预先申请了一个指定大小的空间,但是
有时候这个空间可能太小,有时候可能太大,我们需要准确的大小,这样节省空间,
那么我们可以这样做:
【案例】读取文件内容
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
* 字节流
* 读文件内容,节省空间
* */
import java.io.*;
class hello{
public static void main(String[] args) throws IOException {
String fileName="D:"+File.separator+"hello.txt";
File f=new File(fileName);
InputStream in=new FileInputStream(f);
byte[] b=new byte[(int)f.length()];
in.read(b);
System.out.println("文件长度为:"+f.length());
in.close();
System.out.println(new String(b));
}
}
【案例】逐字节读
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/**
* 字节流
* 读文件内容,节省空间
* */
import java.io.*;
class hello{
public static void main(String[] args) throws IOException {
String fileName="D:"+File.separator+"hello.txt";
File f=new File(fileName);
InputStream in=new FileInputStream(f);
byte[] b=new byte[(int)f.length()];
for (int i = 0; i < b.length; i++) {
b[i]=(byte)in.read();
}
in.close();
System.out.println(new String(b));
}
}
注意:上面的几个例子都是在知道文件的内容多大,然后才展开的,有时候我
们不知道文件有多大,这种情况下,我们需要判断是否独到文件的末尾。
【案例】字节流读取文件
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
* 字节流
*读文件
* */
import java.io.*;
class hello{
public static void main(String[] args) throws IOException {
String fileName="D:"+File.separator+"hello.txt";
File f=new File(fileName);
InputStream in=new FileInputStream(f);
byte[] b=new byte[1024];
int count =0;
int temp=0;
while((temp=in.read())!=(-1)){
b[count++]=(byte)temp;
}
in.close();
System.out.println(new String(b));
}
}
注意:当读到文件末尾的时候会返回-1.正常情况下是不会返回-1 的。
【案例】DataInputStream 类
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class DataOutputStreamDemo{
public static void main(String[] args) throws IOException{
File file = new File("d:" + File.separator +"hello.txt");
DataInputStream input = new DataInputStream(new
FileInputStream(file));
char[] ch = new char[10];
int count = 0;
char temp;
while((temp = input.readChar()) != 'C'){
ch[count++] = temp;
}
System.out.println(ch);
}
}
剩余28页未读,继续阅读
资源评论
悠闲饭团
- 粉丝: 157
- 资源: 3307
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功