zoukankan      html  css  js  c++  java
  • 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();
      			}
      		}
      	}
      }
      


  • 相关阅读:
    Qt 交换Layout中的QWidget控件位置
    霍夫变换(Hough)
    图像傅里叶变换
    通俗讲解:图像傅里叶变换
    傅里叶分析之掐死教程(完整版)
    一幅图弄清DFT与DTFT,DFS的关系
    Qt 实现简单的TCP通信
    Qt 基于TCP的Socket编程
    Socket原理讲解
    科研相机选择:sCMOS还是CCD?
  • 原文地址:https://www.cnblogs.com/dengshiwei/p/4258467.html
Copyright © 2011-2022 走看看