java IO流(字节流)

/*一,操作文件
 * 1.操作字符,也可以操作其他媒体文件
 * 字节流可以复制图片等文件,但是字符流不可
 * 2.InputStream
 *   OutputStream
 * 3.直接将数据写入到目的地中,有flush函数只是单单继承了父类函数的原因
 * 4.代码

public class 字节流 {
	public static void main(String[]args) throws IOException {
	read_();
	write_();
	}
	public static void read_() throws IOException {
    FileInputStream readStream=new FileInputStream("in.txt");
   //    byte[]arr=new byte[readStream.available()];//此方法在文件较大时不要使用
   //    readStream.read(arr);
   //    System.out.println(new String(arr));
     
   //    byte[]arr=new byte[3];
   //    int len=0;
   //    while((len=readStream.read(arr))!=-1) {
   //    	System.out.println(new String(arr,0,len));
  //    }
      int ch=0;
      while((ch=readStream.read())!=-1)
    	  System.out.println((char)ch);
	}
	public static void write_() throws IOException {
		FileOutputStream writerStream=new FileOutputStream("out.txt");
		writerStream.write("abcdefg".getBytes());
	}
  }

*二,mp3复制

*代码

public class 字节流
{
	public static void main(String []args) throws IOException {
		Copy2();
	}
	//复制方式一,自己定义字节数组做缓冲区
	public static void Copy1() throws IOException {
		FileInputStream fielStream=new FileInputStream("D:\\CloudMusic\\butterfly.mp3");
		FileOutputStream fileOutputStream=new FileOutputStream("D:\\CloudMusic\\butterfly1.mp3");
		int len=0;
		byte[]arr=new byte[1024];
		while((len=fielStream.read(arr))!=-1) {
		fileOutputStream.write(arr,0,len);	
		}
		fileOutputStream.close();
		fielStream.close();
	}
	//复制方式二,使用缓冲区
	public static void Copy2() throws IOException {
		FileInputStream fielStream=new FileInputStream("D:\\CloudMusic\\butterfly.mp3");
		BufferedInputStream bufferedInputStream=new BufferedInputStream(fielStream);
		FileOutputStream fileOutputStream=new FileOutputStream("D:\\CloudMusic\\butterfly1.mp3");
		BufferedOutputStream fileBufferedOutputStream=new BufferedOutputStream(fileOutputStream);
		int ch=0;
		while((ch=bufferedInputStream.read())!=-1) {
	    fileBufferedOutputStream.write(ch);
		fileBufferedOutputStream.flush();
		}
		fileOutputStream.close();
		fielStream.close();
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值