FileInputStream与FileOutputStream练习题 -------------------图片拷贝
2015-08-04 22:04
288 查看
package com.outputstream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; /** * 需求: 拷贝一张图片 * @author Administrator * */ class Picture{ public static void readWrite() throws IOException{ //找到原文件 File file = new File("D://abc.jpg"); //找到存放文件位置 File file2 = new File("E://abc.jpg"); //建立读取,写入数据通道 InputStream inputStream = new FileInputStream(file); FileOutputStream fileOutputStream = new FileOutputStream(file2); //建立缓冲数据,边读边写 //每新创建一个FileOutputStream的时候,默认情况下FileOutputStream 的指针是指向了文件的开始的位置。 每写出一次,指向都会出现相应移动。 int length = 0; byte[] bs = new byte[1024]; while((length = inputStream.read(bs)) != -1){ fileOutputStream.write(bs, 0, length);//写出很多次数据,所以就必须要追加。 } //关闭资源 原则: 先开后关,后开先关。 fileOutputStream.close(); inputStream.close(); } } public class Test { public static void main(String[] args) throws IOException { // TODO Auto-generated method stub Picture picture = new Picture(); picture.readWrite(); } }
相关文章推荐
- php的unset函数何时释放内存
- 4.PHP 教程_PHP 变量
- 3.PHP 教程_PHP 语法
- php中的关于xml的使用(SEO)
- PHP ${}
- 批处理 ftp 上传文件
- Ubuntu15.04中FTP-vsftp配置
- Ubuntu14.04安装phpmyadmin 出现的问题及解决办法
- HDU 1096 A+B for Input-Output Practice (VIII)
- thinkphp3.1多表关联查询
- php mygetype typeof 变量的数据类型
- php -l filename.php 来检查语法
- 浏览器上传文件到PHP的几种方法
- php--字符转义
- php--字符转义
- php--字符转义
- js与php传递参数
- iis8 php 去除index.php配置web.config报500错误
- php header函数详解
- php 计算两个文件之间的相对路径方法