Java IO学习笔记(一)

news/2024/7/16 9:06:34

文章目录

    • 标准步骤
    • 文件拷贝
    • 字节数组流

标准步骤

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


http://www.niftyadmin.cn/n/3741986.html

相关文章

记住这曾经平静美好的小城-汶川(组图)

震前的汶川汶川县位于四川省西北部、阿坝州境东南部的岷江两岸。是阿坝州的南大门,有“川西锁钥”和“西羌门户”之称。是大禹的故乡。县城-威州镇,居县北部杂谷 脑河与岷江交汇地,海拔1326米,距省会成都159公里,距州府…

PHP学习笔记(三)

文章目录文件系统处理文件类型文件属性目录的基本操作文件的基本操作文件的一些基本操作函数网络留言板文件的上传PHP动态图像处理PHP中的数据库操作数据库概述数值类的数据列类型字符串类数据列类型日期和时间型数据列类型在PHP脚本中连接MySQL会话控制Cookie概述向客户端计算…

地震防护常识(图片版)

说明:1、看了几个门户做的地震防护常识,觉得有些乱,就到网上查了查,最后综合北京地震局上的 科普知识以及“日本华人网”上一篇叫 《如果地震来了》的帖子,整理了这篇文章。防患于未然,希望对大家有所帮助。…

致敬科比:科比投篮数据可视化

文章目录前言数据介绍shot_type可视化action_type可视化shot_zone_area可视化完整代码后记前言 当地时间1月26日上午10点左右,美国加州发生一起直升机坠机事故。NBA球星科比布莱恩特在这起坠机事故中身亡,年仅41岁。 当听到这个消息时,我相当…

Web2.0:重归内容

Web2.0的革命性在于UGC(User Generate Content,用户贡献内容)。从最早的Blog,到今天炙手可热的twitter。对用户来说,正因为UGC的存在,互联网从“只读”变得“可写”了。 但无论如何,互联网上的“读者”依然比“作者”…

王者荣耀英雄皮肤图片爬取

文章目录思路分析完整代码效果展示思路分析 首先,进入王者荣耀官网,再进入英雄资料页面。 随便找个英雄打开,进入详情页面。 分析图片的URL,我们可以发现链接的前半部分都一样,后半部分加上了英雄的序号&#xff0c…

fastjson android 版本,Fastjson Android版本

Fastjson Android版本Fastjson提供Android版本,和标准版本相比,Android版本去掉一些Android虚拟机dalvik不支持的功能,使得jar更小,同时针对dalvik做了很多性能优化,包括减少方法调用等。parse为JSONObject/JSONArray时…

迷失在专注中的Web2.0

很多互联网创业者都声称自己在专注于某项事业中,对此,我一点也不想表示怀疑。但是,在专注之前,总得先看看自己究竟专注在什么事业中,才有意义。 以UGC(用户贡献内容)为核心的Web2.0大潮席卷了当今的整个互联网络。Web…