Java IO学习笔记(一)

本文详细介绍了一种使用Java进行文件拷贝的标准步骤,并提供了将图片转换为字节数组以及将字节数组还原为图片的具体代码实现。通过实例演示了如何利用字节数组流进行高效的数据读写。

标准步骤

1、创建源
2、选择流
3、操作
4、释放

文件拷贝

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
代码如下:

package com.liuyuhe;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Scanner;

public class FileCopy {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入文件的源头:");
		String from = scanner.nextLine();
		System.out.println("请输入文件的目的地:");
		String to = scanner.nextLine();
		scanner.close();
		File src = new File(from);
		File dest = new File(to);
		if(!src.isFile()) {
			System.out.println("文件路径出错了,请仔细检查一下!");
			System.exit(0);
		}
		InputStream input = null;
		OutputStream output = null;
		try {
			input = new FileInputStream(src);
			output = new FileOutputStream(dest,true);
			//缓冲容器
			byte[] flush = new byte[1024];
			int length = -1;
			while((length=input.read(flush))!=-1) {
				output.write(flush,0,length);
			}
			output.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch(IOException e) {
			e.printStackTrace();
		}finally {
			//释放资源,分别关闭,先打开的后关闭
			try {
				if(output!=null) {
					output.close();
				}
			}catch(IOException e) {
				e.printStackTrace();
			}
			try {
				if(input!=null) {
					input.close();
				}
			}catch(IOException e) {
				e.printStackTrace();
			}
		}
		System.out.println("文件拷贝成功!");
	}

}

字节数组流

在这里插入图片描述
例题:用程序实现将图片转换成字节数组,再将字节数组还原为图片。

代码如下:

package com.liuyuhe;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Scanner;

/**
 * 步骤:
 * 1、图片读取到字节数组
 * 2、字节数组还原成图片
 * @author Martin
 *
 */
public class ImgTransformation {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入文件的源头:");
		String from = scanner.nextLine();
		System.out.println("请输入文件的目的地:");
		String to = scanner.nextLine();
		scanner.close();
		byte[] datas = fileToByteArray(from);
		System.out.println("文件大小为:"+datas.length);
		byteArrayToFile(datas,to);
	}
	public static byte[] fileToByteArray(String filePath) {
		File src = new File(filePath);
		InputStream in = null;
		ByteArrayOutputStream baos = null;
		try {
			in = new FileInputStream(src);
			baos = new ByteArrayOutputStream();
			byte[] flush = new byte[1024];
			int length = -1;
			while((length=in.read(flush))!=-1) {
				baos.write(flush,0,length);
			}
			baos.flush();
			return baos.toByteArray();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				if(baos!=null) {
					baos.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				if(in!=null) {
					in.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	public static void byteArrayToFile(byte[] src,String filePath) {
		File dest = new File(filePath);
		InputStream in = null;
		OutputStream out = null;
		try {
			in = new ByteArrayInputStream(src);
			out = new FileOutputStream(dest,true);
			byte[] flush = new byte[5];
			int length = -1;
			while((length=in.read(flush))!=-1) {
				out.write(flush,0,length);
			}
			out.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}catch (IOException e) {
			e.printStackTrace();
		}finally {
			try {
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值