码迷,mamicode.com
首页 > 其他好文 > 详细

io流整理和归纳

时间:2019-05-10 11:14:44      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:为我   内存区域   exception   字符   dex   数据   port   mic   文件信息   

io流:

技术图片

java有两种类型的流:字节流和字符流和两种类型的流:输入流和输出流(输入和输出是相对java程序而言,磁盘和键盘等设备向程序读入文件信息就为输入,反之为输出)

InputStream 和OutputStream 是字节流的输入和输出的父类,是一个抽象类,FileInputStream、FilterInputStream 等都是对它们的继承。

相同Reader和Writer是字符流的输入和输出的父类,也是抽象类,图片后面其他类也是对它们的继承。

 

字节流和字符流的区别:

1.字节流默认不使用缓冲区;字符流使用缓冲区。 (缓冲区是内存区域,字节流能够在程序完成后立即完成输入输出,字符流需要flush()完成)

2.字节流操作的基本单元为字节;字符流操作的基本单元为Unicode码元。字节流是最基本的,所有的InputStream和OutputStream的子类都是,主要用在处理二进制数据,它是按字节来处理的,但实际中很多的数据是文本,又提出了字符流的概念,它是按虚拟机的Encode来处理,也就是要进行字符集的转化。这两个之间通过 InputStreamReader,OutputStreamWriter来关联,实际上是通过byte[]和String来关联。

3.底层设备永远只接受字节数据,有时候要写字符串到底层设备,需要将字符串转成字节再进行写入。字符流是字节流的包装,字符流则是直接接受字符串,它内部将串转成字节,再写入底层设备,这为我们向IO设别写入或读取字符串提供了一点点方便。

 

输入流的简单应用

package IO;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import java.io.InputStream;

public class IoTest {
    public static void main(String[] args) throws Exception {
        /* 读取io.txt文本的内,在程序中逐字逐句的输出 */
        String filename = "E:" + File.separator + "Io.txt";
        File f = new File(filename);
        /* 读取文件分为两步 */
        /* 1.系统自动将文件的数据转化为流 */
        /* 2.通过read读取流的内部数据 */
        System.out.println(f.length());
        InputStream in = new FileInputStream(f);
        byte[] b = new byte[(int) f.length()];
        int i = in.read(b);
        in.close();
        System.out.println(new String(b));
    }
}

体验下同情景下(假若此时不知道文件的大小)以及不同read的参数情况的感受

package IO;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import java.io.InputStream;

public class IoTest {
    public static void main(String[] args) throws Exception {
        /* 读取io.txt文本的内,在程序中逐字逐句的输出 */
        String filename = "E:" + File.separator + "Io.txt";
        File f = new File(filename);
        /* 读取文件分为两步 */
        /* 1.系统自动将文件的数据转化为流 */
        /* 2.通过read读取流的内部数据 */
        InputStream in = new FileInputStream(f);
        byte[] b = new byte[1024];
        int temp = 0;
        int count  = 0 ;
        while((temp = in.read())!=-1) {/*read将内部数据一个个byte的读取给temp,读取为空时返回-1*/
            b[count]= (byte) temp;
            count++;
        }
        in.close();
        System.out.println(new String(b));
    }
}

 

/*如果把InputStream当做吸管,DataInputStream就是导水管,将吸管进行包装成导水管*/
DataInputStream dataIn = new DataInputStream(in);

 

输出流和输入流同时应用(文件的复制)

package IO;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

public class IoTest {
	public static void main(String[] args) throws Exception {

		String filename1 = "E:" + File.separator + "Io.txt";
		String filename2 = "E:" + File.separator + "Io2.txt";
		File f1 = new File(filename1);
		File f2 = new File(filename2);
		InputStream in = new FileInputStream(f1);
		OutputStream out = new FileOutputStream(f2);
		int temp = 0;
		if(in != null&&out != null) {
			while((temp=in.read())!=-1) {
				out.write(temp);
			}
		}
		in.close();
		out.close();
	}
}

  

 

io流整理和归纳

标签:为我   内存区域   exception   字符   dex   数据   port   mic   文件信息   

原文地址:https://www.cnblogs.com/JTrun/p/10843173.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!