javaIO流 IO流常用方法 简单易懂(2)

	//用文件字节输入流将d:\\a.txt的内容读取到显示在控制台
	
				FileInputStream fis=new FileInputStream("d:\\a.txt");
				//一次读取数组长度个字节 read(byte[] by)
				//定义空数组
				byte[] by=new byte[10];
				//定义一个变量,保存读取的长度
				int len=0;
				int count=0;
				while((len=fis.read(by))!=-1){
					count++;
					//所有的内容,都到数组里面,输出数组
					//new String(by) 将数组变成字符串
					//new String(by,0,len)将数组by中,从下标0开始,len个字节变成字符串
					 //System.out.println(new String(by));
					System.out.println(new String(by,0,len));
				}
				fis.close();
				System.out.println("次数:"+count);
				
				
	//往一个文件中写数据,文件可以不存在		
				FileOutputStream fos=new FileOutputStream("d:\\e.txt",true);		
				//往流中写数据
				String str="hello world";
				byte[] by = str.getBytes();
				fos.write("how are you".getBytes());
				fos.flush();
				fos.close();
				
				
				
	//将d盘的lbt2.jpg图片复制到e盘名字为e.png
				//复制分为两步:1、将图片的信息读取到内存里面  2、将内存中图片的信息写到另外一个盘中
				//创建文件字节输入流对象
				FileInputStream fis=new FileInputStream("d:\\lbt2.jpg");
				//创建文件输出流对象
				FileOutputStream  fos=new FileOutputStream("e:\\e.png",true);
				//创建一个数组,可以一次读取数组长度个字节
				byte[] by=new byte[1024];
				//定义一个变量,保存实际读取的字节个数
				int len=0;
				while((len=fis.read(by))!=-1){
					//将数组的内容写到输出流里面
					fos.write(by);
				}
				//关闭流
				fis.close();
				fos.close();
				
				
				
	//将d:\\a.txt文件的内容读取到控制台
				FileReader fr=new FileReader("d:\\a.txt");
				char[] ch=new char[8];
				int len=0;
				 while((len=fr.read(ch))!=-1){
					System.out.println(len);
					//将数组ch中从下标0开始,len个字符变成字符串
					System.out.println(new String(ch,0,len));
					}
				
				
	//将"好好学习,天天向上"写到E:\\at.txt中
	//创建文件字符输出流对象
				FileWriter fw=new FileWriter("e:\\at.txt", true);
				fw.write("好好学习,天天向上");
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值