JAVA IO流(字节流读写File(文本、图片、音乐~))

//仅作为学习笔记


/*
字符流
FileReader
FileWriter

BufferedReader
BufferedWriter

字节流(操作图片要用到字节流)
InputStream (读)
OutputStream(写)

需求:想要操作图片数据,这时就需要用到字节流
*/

import java.io.*;
class FileStream
{
	public static void main(String []args) throws IOException
	{
		//writeFile();
		//readFile_1();
		readFile_3();
	}


	public static void readFile_3() throws IOException//字节流特有的 不过数据太大的话 容易发生内存溢出 推荐用readFile_2
	{
		FileInputStream fis = new FileInputStream("fos.txt");
	
		byte[] buf = new byte[fis.available()];//定义一个大小刚刚好的缓冲区

		fis.read(buf);
		System.out.println(new String(buf));

		fis.close();
	}


	public static void readFile_2() throws IOException
	{
		FileInputStream fis = new FileInputStream("fos.txt");
	
		byte[] buf = new byte[1024];
		int len = 0;
		while((len = fis.read(buf))!=-1)
		{
			System.out.println(new String(buf, 0 ,len));
		}
		fis.close();
	}

	public static void readFile_1() throws IOException
	{
		FileInputStream fis = new FileInputStream("fos.txt");
		int ch = 0;
		while((ch = fis.read())!= -1)
		{
			System.out.println((char)ch);
		}
		fis.close();
	}

	public static void writeFile()throws IOException
	{
			FileOutputStream fos = new FileOutputStream("fos.txt");

			fos.write("abcd".getBytes());//将字符串变成字节数组
			//不需要刷新 都是字节最小单位 每次都字节玩目的地写入

			fos.close();//关闭资源
	}
}


/*
	复制一个图片
	思路:
	1,用字节读取流对象和图片关联
	2,用字节写入流对象创建一个图片文件 用于存储获取到的图片数据
	3,通过循环读写  完成数据的存储
	4,关闭资源
*/

import  java.io.*;

class CopyPic
{
	public static void main(String []args)
	{
		FileOutputStream fos = null;
		FileInputStream fis = null;
		try
		{
			fos = new FileOutputStream("C:\\2.png");
			fis = new FileInputStream("C:\\1.png");

			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("写入关闭失败!");
			}
		}
	}
}


/*
演示mp3的复制 通过缓冲区
BufferedOutputStream
BufferedInputStream

*/

import java.io.*;

class CopyMp3
{
	public static void main(String []args)throws IOException
	{
		long start = System.currentTimeMillis();
		copy();
		long end = System.currentTimeMillis();
		System.out.println((end - start) + "毫秒");
	}
		
	public static void copy() throws IOException //不建议这样方式抛出异常 此处是为了简写
	{
		BufferedInputStream bufis = new BufferedInputStream(new FileInputStream("D:\\1.mp3"));
	
		BufferedOutputStream bufos = new BufferedOutputStream(new FileOutputStream("D:\\2.mp3"));	
	
		int by = 0;
		
		while( (by = bufis.read())!= -1)
		{
			bufos.write(by);
		}
		bufos.close();
		bufis.close();
	}
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值