Java IO study notes (a)

Standard procedures

1, to create the source
2, select stream
3, operation
4, the release

File copy

Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
code show as below:

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("文件拷贝成功!");
	}

}

Byte array stream

Here Insert Picture Description
Example: use the program converts the image into a byte array, then the image is reduced to an array of bytes.

code show as below:

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

Published 127 original articles · won praise 200 · views 20000 +

Guess you like

Origin blog.csdn.net/Deep___Learning/article/details/104088101