探索InputStream 和 OutputStream 家族成员的心得<一>

探索InputStream 和 OutputStream 家族成员的心得
java程序中的流操作主要解决的是,程序与外界的数据交互问题,也就是说这个知识也是必须要掌握的。

问题:什么是 输入?什么是输出?
这个也是一个重要知识,因为在懂得这个概念才能明白数据的传输方向,才能很肯定地使用相关的类进行操作。
输入:从外界环境,将数据传送给运行程序的内存,在内存看来,是读       取数据。
输出:从内存方向,将数据传送给外界环境(比如一个文件),在内存       看来,是向外写数据。
一、文件的字节流类
FileOutputStream 、FileInputStream
import java.io.FileInputStream;

import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 操作:文件的字节流相关操作
 * 
 * @author Character_Painter
 *
 */
public class StreamDemo {
	public static void main(String[] args) throws IOException {
		/*
		 * 向文件中写数据
		 */
		FileOutputStream fos = new FileOutputStream("demo.txt",true);
		//创建文件字节输出流-----目的地demo.txt,参数为true是代表追加模式
		byte [] b ="java".getBytes();//将字符串的字节长度存在字节数据中
		
		fos.write(11);//将11通过文件字节输出流写到文件中
		
		fos.write(b, 0, b.length);
		/*
		 * 由于在这种情况下,写数据成功后,--打开demo.txt会是乱码-----原因:编码集不一样
		 */
		
		FileInputStream fis  = new FileInputStream("demo.txt");
		System.out.println(fis.read());//read的返回值是int型
		
		System.out.println(fis.read(b));//将字节数据的长度读出来---显示出来
		/*
		 * 从文件中读取数据
		 * 
		 * 最终在Console中显示为11,4
		 */
		fos.close();
                fis.close();
	}

}
总结:文件的字节流主要针对的是对文件的操作,读写文件
拓展:利用文件的字节流拷贝文件
/**
 * 操作:利用文件的字节流--进行文件的拷贝操作
 * @author Character_Painter
 *
 */
public class Copydemo {

	public static void main(String[] args) throws IOException {
		
		/*
		 * 拷贝文件操作
		 */
		FileInputStream fis = new FileInputStream("demo.txt");//文件字节输入流
		FileOutputStream fos = new FileOutputStream("demo_copy.txt");//文件字节输出流
		int b =-1;
		while((b=fis.read())!=-1){
			/*
			 * 读取源文件的字节数据,如不为-1,就把当前读到的字节数据写到拷贝文件中
			 */
			fos.write(b);
		}

		fis.close();
                fos.close();
	}

}

 
二、缓冲流
BufferedOutputStream 、BufferedInputStream

缓冲流的作用:一次性写出若干字节数据,减少写出的次数,来提高读写效率。
利用缓冲流拷贝文件
/**
 * 操作:利用缓冲字节流--进行文件的拷贝操作
 * @author Character_Painter
 *
 */
public class Copydemo {

	public static void main(String[] args) throws IOException {
		
		/*
		 * 拷贝文件操作
		 */
		FileInputStream fis = new FileInputStream("demo.txt");//文件字节输入流
		BufferedInputStream bis = new BufferedInputStream(fis);//缓冲字节输入流
		FileOutputStream fos = new FileOutputStream("Buffer_demo_copy.txt");//文件字节输出流
		BufferedOutputStream bos = new BufferedOutputStream(fos);//缓冲字节输出流
		int b =-1;
		while((b=bis.read())!=-1){
			/*
			 * 读取源文件的字节数据,如不为-1,就把当前读到的字节数据写到拷贝文件中
			 */
			bos.write(b);
		}
		bos.close();
		bis.close();
		
	}

}
三、字符转换流
将字节流转换成字节流,InputStreamReader,OutputStreamWriter
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

/**
 * 操作:使用字符转换流--设定特定的字符集,读写到文件中
 * 
 */
public class chars {
	public static void main(String[] args) throws IOException {
		/*
		 * 利用转换流在文件中写出字符串
		 */
		FileOutputStream fos = new FileOutputStream("change.txt");
		OutputStreamWriter osw = new OutputStreamWriter(fos,"GBK");
		String str ="学习字符转换流";
		osw.write(str);
		osw.close();
		
		/*
		 * 利用转换流读出文件中字符串
		 */
		FileInputStream fis = new FileInputStream("change.txt");
		InputStreamReader isr = new InputStreamReader(fis,"GBK");
		int c =-1;
		while((c=isr.read())!=-1){
			System.out.println((char)c);
		}
	        isr.close();
         }

}


















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值