黑马程序员——字节流

------- android培训java培训、期待与您交流! ---------- 

 

IO流用来处理设备之间的数据传输

Java对数据的操作是通过流的方式

Java用于操作流的类都在IO包中

流按流向分为两种:输入流,输出流。

流按操作类型分为两种:字节流与字符流。 

字节流可以操作任何数据,字符流只能操作纯字符数据,比较方便。

 

字节流的抽象父类

InputStream ,OutputStream

 

 

读取文件

创建FileInputStream对象,指定一个文件。文件必须存在,不存在则会抛出FileNotFoundException

使用read()方法可以从文件中读取一个字节,如果读取到文件末尾会读到-1

available()          获取可读的字节数

读取结束后需要释放资源,调用close()方法关闭输入流

 

写出文件

创建FileOutputStream对象,指定一个文件,文件不存在会创建新文件,存在则清空原内容。如果需要追加,在构造函数中传入true

使用write()方法可以向文件写出一个字节

写出结束后同样需要调用close()

 

拷贝文件

class  CopyPic{
            public static void main(String[] args){
		FileOutputStream fos = null;
		FileInputStream fis = null;

		try{
			fos = new FileOutputStream("e:\\2.jpg");
			fis = new FileInputStream("e:\\1.jpg");

			byte[] buf = new byte[1024];

			int len = 0;
			while ((len=fis.read(buf))!=-1){
				fos.write(buf,0,len);
			}
		}
		catch (IOException e){
			throw new RuntimeException("失败");
		}

		finally{
			try{
				if(fis!=null)
					fis.close();
			}
			catch (IOException e){
				throw new RuntimeException("失败");
			}

			try{
				if(fos!=null)
					fos.close();
			}
			catch (IOException e){
				throw new RuntimeException("失败");
			}
		}
	}
}


 

 BufferedInputStream
  BufferedInputStream内置了一个缓冲区(数组)
  从BufferedInputStream中读取一个字节时
  BufferedInputStream会一次性从文件中读取8192个,存在缓冲区中,返回给程序一个
  程序再次读取时,就不用找文件了,直接从缓冲区中获取
  直到缓冲区中所有的都被使用过,才重新从文件中读取8192个

BufferedOutputStream
  BufferedOutputStream也内置了一个缓冲区(数组)
  程序向流中写出字节时,不会直接写到文件,先写到缓冲区中
  直到缓冲区写满,BufferedOutputStream才会把缓冲区中的数据一次性写到文件里

 

public static void copy()throws IOException{
		BufferedInputStream bufis = new BufferedInputStream(new FileInputStream("e:\\1.mp3"));
		BufferedOutputStream bufos = new BufferedOutputStream(new FileOutputStream("e:\\2.mp3"));


		int by =0;
		while ((by=bufis.read())!=-1){
			bufos.write(by);
		}
		bufis.close();
		bufos.close();
	}


键盘录入

class  ReadIn{
	public static void main(String[] args)throws IOException{
		InputStream in = System.in;
		StringBuilder sb = new StringBuilder();

		while (true){
			int ch = in.read();
			if(ch=='\r')
				continue;
			if(ch=='\n'){
				String s = sb.toString();
				if("over".equals(s))
					break;
				System.out.println(s.toUpperCase());
				sb.delete(0,sb.length());
			}
			else
				sb.append((char)ch);
		}
		
	}
}


 

 

转换流

InputStreamReader,OutputStreamWriter

转换流的由来

字符流与字节流之间的桥梁

方便了字符流与字节流之间的操作

转换流的应用

字节流中的数据都是字符时,转成字符流操作更高效。

标准输入输出流

BfferedReader bufr =  new BufferedReader(new InputStreamReader(System.in));

BufferedWriter bufw =  new BufferedWriter(new OutputStreamWriter(System.out));

1,通过键盘录入数据,将数据转成大写打印在控制台

2,将键盘录入的数据,转成大写,保存到文件中

3,将一个已有的文本文件,转成大写,显示在控制台

4,将一个已有的文件,转成大写,保存到另一个文件中

 

class  TransStreamDemo{
	public static void main(String[] args)throws IOException{
		//获取键盘录入对象
		InputStream in = System.in;

		//将字节流对象转成字符流对象,
		InputStreamReader isr = new InputStreamReader(in);

		BufferedReader bufr = new BufferedReader(isr);
		
		
		
		OutputStream out = System.out;
                   //将字符流对象转成字节流对象,
  		OutputStreamWriter osw = new OutputStreamWriter(out);

		BufferedWriter bufw = new BufferedWriter(osw);
				
		
		String line = null;

		while ((line=bufr.readLine())!=null){
			if("over".equals(line))
				break;
			bufw.write(line.toUpperCase());
			bufw.newLine();
			bufw.flush();
			
		}

		bufr.close();
	}
}


 

 

流操作地基本规律:

1,明确源和目的

源:输入流          InputStream     Reader

目的:输出流      OutputStream    Writer

2,操作的数据是否是纯文本

是:字符流

不是:字节流

3,当体系明确后,再明确要使用哪个具体的对象

 通过设备进行区分:

源设备:内存,硬盘,键盘

目的设备:内存,硬盘,控制台

 

 

如果需要指定编码表,只有转换流可以指定。file默认码表:GBK 

OutputStreamWrite ows = new OutputStreamWrite(new FileOutputStream("d.txt","utf-8"));

 

 

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值