Io流总结

io流用于输入和输出,就是将数据从程序输出到本地硬盘,从本地硬盘读取数据到程序。废话不多说,直接上关系图

一 字节流的输入 使用 FileInputSteam 

public static void main(String[] args) throws IOException {
		//创建一个文件源,将此文件中的数据读取出来并打印
		File file=new File("e:/src/ByteOut.txt");
		//new一个 FileInputStream类,参数为file,表示读取此文件中的 数据
		InputStream in=new FileInputStream(file);
		//创建长度
		int len=0;
		//创建中间容器
		byte[] by=new byte[1024];
		//下面调用read()方法开始读取数据,如果已经读完数据,没有数据可读取了,会返回-1
		//也就是len=-1那么跳出循环
		while(-1!=(len=in.read(by)))
		{
			System.out.println(new String(by));		
		}
		//关闭连接
		in.close();
		
	}

字节流的输出  使用FileOutputStream

public static void main(String[] args) throws IOException {				
				//创建一个文件源,将数据写入到这个文件中
				File file=new File("e:/src/ByteOut.txt");
				//new一个 FileInputStream类,参数为file,true表示追加
				OutputStream out=new FileOutputStream(file,true);
				String str="this is test";
				//将str转为字节数组
				byte[] b=str.getBytes();
				//调用write方法将数据输出.
				out.write(b);				
				//关闭连接
				out.close();
				
			}

二 字符流的输入 (字符流只能处理纯文本,而字节流可以处理一切数据) 

使用FileReader

public static void main(String[] args) throws IOException {
		//创建一个文件源,将数据写入到这个文件中
		File file=new File("e:/src/ByteOut.txt");
		//new一个 FileInputStream类,参数为file,
		Reader in=new FileReader(file);
		//创建长度
		int len=0;
		//创建中间容器 因为是字符,所有简历一个char类型的数组用作中间容器
		char[] by=new char[1024];
		//下面调用read()方法将数据读入by数组 ,如果已经读完数据,没有数据可读取了,会返回-1
		//也就是len=-1那么跳出循环
		while(-1!=(len=in.read(by)))
		{
			System.out.println(new String(by));		
		}
		//关闭连接
		in.close();
		
	}

字符流的输出

public static void main(String[] args) throws IOException {				
				//创建一个文件源,将数据写入到这个文件中
				File file=new File("e:/src/ByteOut.txt");
				//new一个 FileInputStream类,参数为file,true表示追加
				Writer out=new FileWriter(file,true);
				String str="this is test";						
				//调用write方法将数据写入.
				out.write(str);				
				//关闭连接
				out.close();				
			}

三 BufferedInputStream 和 BufferedOutputStream类 是字符缓冲流,为了提高性能和效率,

1.  java.io.BufferedInputStream与java.io.BufferedOutputStream可以为InputStream,OutputStream类增加缓冲区功能。构建BufferedInputStream实例时,需要给定一个InputStream类型的实例,实现BufferedInputStream时,实际上最后是实现InputStream实例。同样,构建BufferedOutputStream时,也需要给定一个OutputStream实例,实现BufferedOutputStream时,实际上最后是实现OutputStream实例。

2. BufferedInputStream的数据成员buf是一个位数组,默认为2048字节。当读取数据来源时,例如文件,BufferedInputStream会尽量将buf填满。当使用read()方法时,实际上是先读取buf中的数据,而不是直接对数据来源作读取。当buf中的数据不足时,BufferedInputStream才会再实现给定的InputStream对象的read()方法,从指定的装置中提取数据。

3. BufferedOutputStream的数据成员buf也是一个位数组,默认为512字节。当使用write()方法写入数据时实际上会先将数据写到buf中,当buf已满时才会实现给定的OutputStream对象的write()方法,将buf数据写到目的地,而不是每次都对目的地作写入的动作。

写法如下 用法和之前的字节流和字符流的输入输出一样.

BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(new File("e:/src/a.txt")));

BufferedInputStream  bis=new BufferedInputStream(new FileInputStream(new File("e:/src/a.txt")));

四 字节流转化为字符流  使用的类 BufferedReader和InputStreamReader这两个都可以 不过是有区别滴,就是InputStreamReader类是将字节流转为了字符数组

BufferedReader 的readLine()方法可以将字节流转为字符串 而InputStreamReader只有read()方法 返回的是char[];

public static void main(String[] args) throws IOException {	
				//创建文件
				File file=new File("e:/src/ByteOut.txt");
				//new一个BufferedReader类 类中包含 InputStreamReader的实例  可以指定编码
				BufferedReader br=new BufferedReader
						(new InputStreamReader(new FileInputStream(file), "UTF-8"));
				String str=br.readLine();
				System.out.println(str);
				br.close();
			}

 InputStreamReader用法

public static void main(String[] args) throws IOException {	
				//创建文件
				File file=new File("e:/src/ByteOut.txt");
				//new一个InputStreamReader类  可以指定编码
				InputStreamReader isr=new InputStreamReader(new FileInputStream(file),"UTF-8");
				//创建中间容器
				char[] c=new char[1024];
				while(-1!=(isr.read(c)))
				{
					System.out.println(new String(c));
				}
				isr.close();
			}

五 字节数组流 ByteArrayInputStream ByteArrayOutputStream

ByteArrayOutputStream类是在创建它的实例时,程序内部创建一个byte型别数组的缓冲区,然后利用ByteArrayOutputStream和ByteArrayInputStream的实例向数组中写入或读出byte型数据。在网络传输中我们往往要传输很多变量,我们可以利用ByteArrayOutputStream把所有的变量收集到一起,然后一次性把数据发送出去。具体用法如下:

ByteArrayOutputStream:    可以捕获内存缓冲区的数据,转换成字节数组 ,ByteArrayInputStream: 可以将字节数组转化为输入流

public class OutIn {
			public static void main(String[] args) throws IOException {				
			
				outbyte("E:/src/mhy.txt",inbyte("E:/src/my.txt"));
			}
			//测试ByteArrayInputStream 并输出到E:/src/mhy.txt
			public static void outbyte(String src,byte[] b) throws IOException{
				File file=new File(src);
				byte[] best=new byte[1024];		
				OutputStream out=new BufferedOutputStream(new FileOutputStream(file));
				//new 一个 BufferedInputStream  这个可以使用多态  
				InputStream in=new BufferedInputStream(new ByteArrayInputStream(b));			
				int len=0;
				//将数据读取到 best 然后输出
				while(-1!=(len=in.read(best)))
				{				
					out.write(best);
					System.out.println(new String(best));
				}				
				out.flush();
				in.close();
				out.close();						
			}
		//测试ByteArrayOutputStream并返回一个  byte[]数组
		public static byte[] inbyte(String src) throws IOException{
			File file=new File(src);
			byte[] best=new byte[1024];
			InputStream inp=new BufferedInputStream(new FileInputStream(file));
			//new 一个 ByteArrayOutputStream  这个不可使用多态
			ByteArrayOutputStream out=new ByteArrayOutputStream();
			int len=0;
			while(-1!=(len=inp.read(best)))
			{
				//将字节数组写入到 out
				out.write(best, 0, len);
			}
			//将out中的有效信息赋值给best
			best=out.toByteArray();
			out.flush();
			inp.close();
			out.close();		
			return best;
		}
}

总结: 

 

   

 

posted @ 2016-11-08 17:44  臭屁猪  阅读(252)  评论(0编辑  收藏  举报