• FileInputStream、FileOutputStream的应用


    1. /*
    2. 复制一个图片:
    3. 思路:
    4. 1、用字节流读取对象和图片进行关联
    5. 2、用字节写入流创建一个文件图片,用于存储获取到的图片数据。
    6. 3、通过循环读取,完成数据的存储。
    7. 4、关闭资源
    8. */
    9. 1、二者操作的基本单位是字节byte。
      2、常用于读写图片、声音、影像文件。
      基本操作步骤:
      1、创建流对象
      2、调用流对象的功能read、write等
      3、关闭流对象

    10. 方法一:
      import java.io.*;
      class  CopyMp3
      {
      	public static void main(String[] args) 
      	{
      		CopyPic();
      	}
      
      	public static void CopyPic()
      	{
      		FileInputStream fileInput = null;
      		FileOutputStream fileOutput = null;
      		try
      		{
      			fileInput = new FileInputStream("C:\1.jpg");
      			fileOutput = new FileOutputStream("C:\2.Jpg");
      			byte [] bt = new byte[1024];
      			int len = 0;
      			while((len = fileInput.read()) != -1)
      			{
      				fileOutput.write(len);
      			}
      		}
      		catch (IOException ex)
      		{
      			ex.printStackTrace();
      		}
      		finally
      		{
      			try
      			{
      				if(fileInput != null)
      				{
      					fileInput.close();
      				}
      				if(fileOutput != null)
      				{
      					fileOutput.close();
      				}
      			}
      			catch (IOException ex)
      			{
      				ex.printStackTrace();
      			}
      		}
      	}
      }
      
      此时图片2和图片1的属性完全一样。

      方法二:速度比较快,但是图片多了1KB,可能就是由于数组的使用。
    11. import java.io.*;
      class  CopyMp3
      {
      	public static void main(String[] args) 
      	{
      		CopyPic();
      	}
      
      	public static void CopyPic()
      	{
      		FileInputStream fileInput = null;
      		FileOutputStream fileOutput = null;
      		try
      		{
      			fileInput = new FileInputStream("C:\1.jpg");
      			fileOutput = new FileOutputStream("C:\2.Jpg");
      			byte [] bt = new byte[1024];
      			int len = 0;
      			while((len = fileInput.read(bt)) != -1)
      			{
      				fileOutput.write(bt);
      			}
      		}
      		catch (IOException ex)
      		{
      			ex.printStackTrace();
      		}
      		finally
      		{
      			try
      			{
      				if(fileInput != null)
      				{
      					fileInput.close();
      				}
      				if(fileOutput != null)
      				{
      					fileOutput.close();
      				}
      			}
      			catch (IOException ex)
      			{
      				ex.printStackTrace();
      			}
      		}
      	}
      }
      


  • 相关阅读:
    19.音乐查询l练习
    python用requests爬取新浪财经首页要闻
    关于Pyhton正则报错: sre_constants.error: nothing to repeat at position
    python中的字典
    Python flask jQuery ajax 上传文件
    python中与时间有关的对象-datetime、time、date
    python os模块之实现多层目录文件查找
    python 字符串格式化输出 %d,%s及 format函数, 数字百分化输出
    linux unbuntu 虚拟环境 安装沙盒virtualenv 、virtualenvwrapper
    python实现二分查找
  • 原文地址:https://www.cnblogs.com/dengshiwei/p/4258467.html
Copyright © 2020-2023  润新知