zoukankan      html  css  js  c++  java
  • FileInputStream与FileOutputStream练习题 -------------------图片拷贝

     1 package com.outputstream;
     2 
     3 import java.io.File;
     4 import java.io.FileInputStream;
     5 import java.io.FileNotFoundException;
     6 import java.io.FileOutputStream;
     7 import java.io.IOException;
     8 import java.io.InputStream;
     9 /**
    10  * 需求: 拷贝一张图片
    11  * @author Administrator
    12  *
    13  */
    14 class Picture{
    15     public static void readWrite() throws IOException{
    16         //找到原文件
    17         File file = new File("D://abc.jpg");
    18         //找到存放文件位置
    19         File file2 = new File("E://abc.jpg");
    20         //建立读取,写入数据通道
    21         InputStream inputStream = new FileInputStream(file);
    22         FileOutputStream fileOutputStream = new FileOutputStream(file2);
    23         //建立缓冲数据,边读边写
    24         //每新创建一个FileOutputStream的时候,默认情况下FileOutputStream 的指针是指向了文件的开始的位置。 每写出一次,指向都会出现相应移动。
    25         int length = 0;
    26         byte[] bs = new byte[1024];
    27         while((length = inputStream.read(bs)) != -1){
    28             fileOutputStream.write(bs, 0, length);//写出很多次数据,所以就必须要追加。
    29         }
    30         //关闭资源 原则: 先开后关,后开先关。
    31         fileOutputStream.close();
    32         inputStream.close();
    33     }
    34 }
    35 
    36 
    37 public class Test {
    38 
    39     public static void main(String[] args) throws IOException {
    40         // TODO Auto-generated method stub
    41 
    42         Picture picture = new Picture();
    43         picture.readWrite();
    44     }
    45 
    46 }
  • 相关阅读:
    jquery 绑定事件前先解除事件绑定
    jquer ajax的方法返回值
    jQuery动态生成不规则表格前后端
    常见的正则表达式
    锚点连接
    javascript动态添加删除表格
    java面试题之第二回
    [转]java抽象类和接口
    Java IO 和 NIO
    Java IO之序列化
  • 原文地址:https://www.cnblogs.com/fujilong/p/4703275.html
Copyright © 2011-2022 走看看