Java中的流,可以从不同的角度进行分类。
按照数据流的方向不同可以分为:输入流和输出流。
按照处理数据单位不同可以分为:字节流和字符流。
按照实现功能不同可以分为:节点流和处理流。
输出流:
输入流:
因此输入和输出都是从程序的角度来说的。
字节流:一次读入或读出是8位二进制。
字符流:一次读入或读出是16位二进制。
字节流和字符流的原理是相同的,只不过处理的单位不同而已。后缀是Stream是字节流,而后缀是Reader,Writer是字符流。
节点流:直接与数据源相连,读入或读出。
直接使用节点流,读写不方便,为了更快的读写文件,才有了处理流。
处理流:与节点流一块使用,在节点流的基础上,再套接一层,套接在节点流上的就是处理流。
Jdk提供的流继承了四大类:InputStream(字节输入流),OutputStream(字节输出流),Reader(字符输入流),Writer(字符输出流)。
以下是java中io中常用的流。
字节输入流:
字节输出流:
字符输入流:
字符输出流:
简单介绍其上图:
对文件进行操作:FileInputStream(字节输入流),FileOutputStream(字节输出流),FileReader(字符输入流),FileWriter(字符输出流)
FileReader、FileWriter 可以实现文本文件的复制, 对于非文本文件(视频文件、音频文件、图片),只能使用字节流!
例子:
// 字节流实现文件复制的方法
public void copyFile(String src, String dest) {
// 1.提供读入、写出的文件
File file1 = new File(src);
File file2 = new File(dest);
// 2.提供相应的流
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(file1);
fos = new FileOutputStream(file2);
// 3.每次读取的字节数
byte[] b = new byte[20];
int len;
while ((len = fis.read(b)) != -1) {
// fos.write(b);//错误的写法两种: fos.write(b,0,b.length);
fos.write(b, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
// 字符流实现文件复制的方法
public void copyFile(String src, String dest) {
//1.输入流对应的文件src一定要存在,否则抛异常。输出流对应的文件dest可以不存在,执行过程中会自动创建
FileReader fr = null;
FileWriter fw = null;
// 1.提供读入、写出的文件
File file1 = new File(src);
File file2 = new File(dest);
try {
fr= new FileReader(file1);
fw = new FileWriter(file2);
// 3.每次读取的字符数
char[] c = new char[24];
int len;
while ((len = fr.read(c)) != -1) {
// fos.write(c);//错误的写法两种: fos.write(c,0,c.length);
fos.write(c, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally{
if(fw != null){
try {
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(fr != null){
try {
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public void testCopyFile(){
long start = System.currentTimeMillis();
String src = "C:\\Users\\shkstart\\Desktop\\dbcp.txt";
String dest = "C:\\Users\\shkstart\\Desktop\\dbcp1.txt";
copyFile(src,dest);
long end = System.currentTimeMillis();
System.out.println("花费的时间为:" + (end - start));//3198
}
对管道进行操作:PipedInputStream(字节输入流),PipedOutStream(字节输出流),PipedReader(字符输入流),PipedWriter(字符输出流)
PipedInputStream的一个实例要和PipedOutputStream的一个实例共同使用,共同完成管道的读取写入操作。主要用于线程操作。
字节/字符数组:ByteArrayInputStream,ByteArrayOutputStream,CharArrayReader,
CharArrayWriter是在内存中开辟了一个字节或字符数组。
Buffered缓冲流:BufferedInputStream,BufferedOutputStream (flush()刷新缓冲区),BufferedReader (readLine()读取一行),BufferedWriter (flush()刷新缓冲区) ,是带缓冲区的处理流,缓冲区的作用的主要目的是:避免每次和硬盘打交道,提高数据访问的效率。
例子:
//使用缓冲流实现文件的复制的方法
public void copyFile(String src,String dest){
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
//1.提供读入、写出的文件
File file1 = new File(src);
File file2 = new File(dest);
//2.想创建相应的节点流:FileInputStream、FileOutputStream
FileInputStream fis = new FileInputStream(file1);
FileOutputStream fos = new FileOutputStream(file2);
//3.将创建的节点流的对象作为形参传递给缓冲流的构造器中
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
//4.具体的实现文件复制的操作
byte[] b = new byte[1024];
int len;
while((len = bis.read(b)) != -1){
bos.write(b, 0, len);
bos.flush();
}
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
//5.关闭相应的流
if(bos != null){
try {
bos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(bis != null){
try {
bis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
转化流:InputStreamReader/OutputStreamWriter,把字节转化成字符。
/*
* 如何实现字节流与字符流之间的转换:
* 转换流:InputStreamReader OutputStreamWriter
* 编码:字符串 --->字节数组
* 解码:字节数组--->字符串
*/
@Test
public void test1(){
BufferedReader br = null;
BufferedWriter bw = null;
try {
//解码
File file = new File("dbcp.txt");
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
br = new BufferedReader(isr);
//编码
File file1 = new File("dbcp4.txt");
FileOutputStream fos = new FileOutputStream(file1);
OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
bw = new BufferedWriter(osw);
String str;
while((str = br.readLine()) != null){
bw.write(str);
bw.newLine();
bw.flush();
}
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(bw != null){
try {
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(br != null){
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
数据流:DataInputStream,DataOutputStream。
因为平时若是我们输出一个8个字节的long类型或4个字节的float类型,那怎么办呢?可以一个字节一个字节输出,也可以把转换成字符串输出,但是这样转换费时间,若是直接输出该多好啊,因此这个数据流就解决了我们输出数据类型的困难。数据流可以直接输出float类型或long类型,提高了数据读写的效率。
打印流:printStream,printWriter,一般是打印到控制台,可以进行控制打印的地方。
对象流:ObjectInputStream,ObjectOutputStream,把封装的对象直接输出,而不是一个个在转换成字符串再输出。
序列化流:SequenceInputStream。
对象序列化:把对象直接转换成二进制,写入介质中。
使用对象流需要实现Serializable接口,否则会报错。而若用transient关键字修饰成员变量,不写入该成员变量,若是引用类型的成员变量为null,值类型的成员变量为0.
参考:http://blog.youkuaiyun.com/yuebinghaoyuan/article/details/7388059/
转载于:https://blog.51cto.com/ciyorecord/1936503