《黑马程序员》java IO流 基本操作(2)

--------------------- <a href="http://edu.csdn.net"target="blank">ASP.Net+Android+IOS开发</a>、<a href="http://edu.csdn.net"target="blank">.Net培训</a>、期待与您交流! ----------------------

 ----------- android培训java培训、java学习型技术博客、期待与您交流! ------------

  字节流对象可以操作各类数据文件,如拍图片和mp3


 文件输入输出

     首先够着一个FielputStream,所以关联的文件必须存在而且可读

        FileInputStraenm fid=new FileInputStream("asassa.mp3")

    构造一个FileOutputStream ,如果文件存在,就覆盖它

        FileOutputStream fos=new FileOutputStream("ada.mp3")

    如果想要追加方式写文件,就需要额外加一个参数

      FileOutputStream fos=new FileOutputStream("ada.mp3",true)

    临时存放文件数据用字节数组

        byte[] buf=new byte[1024]

实例1

        复制文件

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

public class Copytu {

	/**
	 * 1.用字节读取流 和图片关联
	 * 2.用字节写入流对象创建一个图片温江,用于存储取到的图片数据
	 * 3.通过讯黄读写,完成数据的存储
	 * 关闭资源
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
   FileOutputStream fos=null;
   FileInputStream fis=null;
   try
   {
	   fos=new FileOutputStream("asa.bmp");
	   fis=new FileInputStream("ada.bmp");
	   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("写入关闭失败");
		   
	   }
   }
		
	}

}
实例2  

    将一个MP3文件(Beyond.mp3)以追加的方式续写到别一个文件(my.mp3),实例中使用了字节输入输出缓冲流,程序编译成功,我运行了三次,最后my.mp3的容量正好是beyond.mp3的三倍,播放MP3也反复播放了三遍


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Mp3Copy {

	/**我将beyond.mp3文件以追加的形式存入到12.mp3中
	 * @param args
	 * @throws FileNotFoundException 
	 */
	public  static void copy1() throws FileNotFoundException
	{   BufferedOutputStream bos=null;
	    BufferedInputStream bis=null;
	    try{
		   bos=new BufferedOutputStream(new FileOutputStream("my.mp3",true));
		 
		   bis=new BufferedInputStream(new FileInputStream("Beyond.mp3"));
	     int bt=0;
	     while((bt=bis.read())!=-1)
	     {
	    	 bos.write(bt);
	     }
	    }
	    catch(IOException e)
	    {
	    	throw new RuntimeException("复制失败");
	    }
	    finally
	    {
	    	try
	    	{
	    		if(bis!=null)
	    			bis.close();
	    	}
	    	catch(IOException e)
	    	{
	    		throw new RuntimeException("文件读取失败 ");
	    	}
	    	try
	    	{
	    		if(bos!=null)
	    			bos.close();
	    	}
	    	catch(IOException e)
	    	{
	    		throw new RuntimeException(" 复制 失败 ");
	    	}
	    }
	    System.out.print("复制成功");
	     
	     
	}
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
    //通过自己留的缓冲区完成复制
		copy1();
	}

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值