博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
IO操作的工具类总结
阅读量:4220 次
发布时间:2019-05-26

本文共 6958 字,大约阅读时间需要 23 分钟。

 

/** *  ClassName: IOHelper.java *  Created on 2013 *  Copyrights 2013 hi.csdn.net/tjcyjd All rights reserved. *  site: http://hi.csdn.net/tjcyjd *  email: 908599713@qq.com */package com.kinder.common;import java.awt.image.BufferedImage;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.text.SimpleDateFormat;import java.util.Date;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import com.sun.image.codec.jpeg.ImageFormatException;import com.sun.image.codec.jpeg.JPEGCodec;import com.sun.image.codec.jpeg.JPEGImageEncoder;/** * IO操作的工具类 *  * @author yjd */public class IOHelper {	private static Logger logger = LoggerFactory.getLogger(IOHelper.class);	private IOHelper() {	}	/**	 * 根据指定的文件名从classpath下把该文件加载成InputStream实例	 * 	 * @param path	 * @return	 */	public static InputStream getInputStreamFromClassPath(String path) {		return Thread.currentThread().getContextClassLoader()				.getResourceAsStream(path);	}	/**	 * 创建目录	 * 	 * @param dirFile	 * @return	 * @throws RuntimeException	 */	public static boolean createDir(File dirFile) throws RuntimeException {		boolean flag = false;		if (dirFile.exists()) {			flag = true;		} else {			flag = dirFile.mkdirs();		}		return flag;	}	/**	 * 删除目录	 * 	 * @param dirFile	 * @return	 * @throws RuntimeException	 */	public static boolean deleteDir(File dirFile) throws RuntimeException {		boolean flag = false;		if (!dirFile.exists()) {			flag = true;		} else {			if (deleteSubFiles(dirFile)) {				flag = dirFile.delete();			}		}		return flag;	}	/**	 * 清空指定目录下的所有文件	 * 	 * @param dir	 */	public static boolean deleteSubFiles(File dir) {		logger.debug("开始清除{}下的所有文件", dir.getAbsolutePath());		boolean flag = false;		if (dir.exists() && dir.isDirectory()) {			File[] subs = dir.listFiles();			int succ = 0;			int fail = 0;			int length = subs == null ? 0 : subs.length;			File sub = null;			try {				for (int i = 0; i < length; i++) {					sub = subs[i];					if (sub.delete()) {						succ++;					} else {						fail++;					}				}				flag = true;			} catch (Exception e) {				logger.error("清除{}下的" + sub.getAbsolutePath() + "文件失败,原因是:{}",						dir.getAbsolutePath(), e);			}			logger.debug("清除{}下的所有文件, 成功{}个", dir.getAbsolutePath(), succ);			logger.debug("清除{}下的所有文件, 失败{}个", dir.getAbsolutePath(), fail);		}		return flag;	}	/**	 * 获取指定文件的扩展名	 * 	 * @param fileName	 * @return	 */	public static String getExtend(String fileName) {		return fileName.substring(fileName.lastIndexOf('.'));	}	/**	 * 使用当前日期时间来产生"yyyyMMddHHmmssSSS"的字符串,主要用来做文件名	 * 	 * @return	 */	public static String generateFileNameByDateTime() {		return new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());	}	/**	 * 把源文件的内容复制到目标文件	 * 	 * @param src	 *            源文件	 * @param dest	 *            目标文件	 */	public static void copy(File src, File dest) throws IOException {		BufferedInputStream bis = null;		BufferedOutputStream bos = null;		byte[] b = new byte[8192];		try {			bis = new BufferedInputStream(new FileInputStream(src));			bos = new BufferedOutputStream(new FileOutputStream(dest));			for (int count = -1; (count = bis.read(b)) != -1;) {				bos.write(b, 0, count);			}			bos.flush();		} catch (IOException e) {			throw e;		} finally {			close(bis);			close(bos);		}	}	/**	 * 把源图片srcImg缩小至指定的宽度和高度并存放至dest目标文件	 * 	 * @param srcImg	 * @param dest	 * @param outWidth	 * @param outHeight	 */	public static void resizeToFile(BufferedImage srcImg, File dest,			int outWidth, int outHeight) throws RuntimeException {		BufferedImage pbFinalOut = null;		if (srcImg.getWidth() == outWidth && srcImg.getHeight() == outHeight) {			pbFinalOut = srcImg;		} else {			pbFinalOut = IOHelper.scaleImage(srcImg, outWidth, outHeight);		}		FileOutputStream out = null;		try {			out = new FileOutputStream(dest);			JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);			encoder.encode(pbFinalOut);		} catch (ImageFormatException e) {			throw new RuntimeException(e);		} catch (IOException e) {			throw new RuntimeException(e);		} finally {			close(out);		}	}	/**	 * 把源图像缩小成指定宽高	 * 	 * @param srcImg	 *            源图片	 * @param outWidth	 *            宽	 * @param outHeight	 *            高	 * @return 压缩后的图像对象	 */	public static BufferedImage scaleImage(BufferedImage srcImg, int outWidth,			int outHeight) {		int width = srcImg.getWidth();		int height = srcImg.getHeight();		int[] rgbArray = srcImg.getRGB(0, 0, width, height, null, 0, width);		BufferedImage pbFinalOut = new BufferedImage(outWidth, outHeight,				BufferedImage.TYPE_INT_RGB);		double hScale = ((double) width) / ((double) outWidth);// 宽缩小的倍数		double vScale = ((double) height) / ((double) outHeight);// 高缩小的倍数		int winX0, winY0, winX1, winY1;		int valueRGB = 0;		long R, G, B;		int x, y, i, j;		int n;		for (y = 0; y < outHeight; y++) {			winY0 = (int) (y * vScale + 0.5);// 得到原图高的Y坐标			if (winY0 < 0) {				winY0 = 0;			}			winY1 = (int) (winY0 + vScale + 0.5);			if (winY1 > height) {				winY1 = height;			}			for (x = 0; x < outWidth; x++) {				winX0 = (int) (x * hScale + 0.5);				if (winX0 < 0) {					winX0 = 0;				}				winX1 = (int) (winX0 + hScale + 0.5);				if (winX1 > width) {					winX1 = width;				}				R = 0;				G = 0;				B = 0;				for (i = winX0; i < winX1; i++) {					for (j = winY0; j < winY1; j++) {						valueRGB = rgbArray[width * j + i];						R += getRedValue(valueRGB);						G += getGreenValue(valueRGB);						B += getBlueValue(valueRGB);					}				}				n = (winX1 - winX0) * (winY1 - winY0);				R = (int) (((double) R) / n + 0.5);				G = (int) (((double) G) / n + 0.5);				B = (int) (((double) B) / n + 0.5);				valueRGB = comRGB(clip((int) R), clip((int) G), clip((int) B));				pbFinalOut.setRGB(x, y, valueRGB);			}		}		return pbFinalOut;	}	private static int clip(int x) {		if (x < 0)			return 0;		if (x > 255)			return 255;		return x;	}	private static int getRedValue(int rgbValue) {		int temp = rgbValue & 0x00ff0000;		return temp >> 16;	}	private static int getGreenValue(int rgbValue) {		int temp = rgbValue & 0x0000ff00;		return temp >> 8;	}	private static int getBlueValue(int rgbValue) {		return rgbValue & 0x000000ff;	}	private static int comRGB(int redValue, int greenValue, int blueValue) {		return (redValue << 16) + (greenValue << 8) + blueValue;	}	/**	 * 同时关闭输入流和输出流,并把可能抛出的异常转换成RuntimeException	 * 	 * @param is	 * @param os	 */	public static void close(InputStream is, OutputStream os) {		close(is);		close(os);	}	/**	 * 关闭输入流的工具方法,并把可能抛出的异常转换成RuntimeException	 * 	 * @param is	 */	public static void close(InputStream is) {		if (is != null) {			try {				is.close();			} catch (IOException e) {				e.printStackTrace();			}		}	}	/**	 * 关闭输出流的工具方法,并把可能抛出的异常转换成RuntimeException	 * 	 * @param os	 */	public static void close(OutputStream os) {		if (os != null) {			try {				os.close();			} catch (IOException e) {				e.printStackTrace();			}		}	}}

 

转载地址:http://xoemi.baihongyu.com/

你可能感兴趣的文章
C与C++中string的区别与联系
查看>>
OpenGL学习网站及资料
查看>>
数字图像处理基本知识--笔记一
查看>>
傅里叶变换的通俗理解
查看>>
傅里叶变换理解之二
查看>>
最大公共字串问题
查看>>
面试经
查看>>
男女比例问题
查看>>
大范围内素数的求法
查看>>
各大公司经典算法面试题
查看>>
各种进制转换的原理
查看>>
谈数学学习
查看>>
重要的算法
查看>>
面试常见的算法
查看>>
GPU的硬件结构
查看>>
AES加密算法在GPU上的实现
查看>>
Visual C++下的一些程序调试和开发技巧
查看>>
图像处理和计算机视觉中的经典论文
查看>>
时间与空间复杂度
查看>>
递归算法与非递归算法的转化
查看>>