Java IO学习笔记(一)

标准步骤

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();
			}
		}
	}
}

发布了127 篇原创文章 · 获赞 200 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/Deep___Learning/article/details/104088101
今日推荐