使用apache的commons-io
包下的FileUtils,下载地址点击这里。官方API文档点击这里,我筛选了一些常用方法:
创建
作用 | 方法名 |
创建文件(如果文件存在,将更新文件的创建时间) | touch |
创建目录(支持多级创建) | forceMkdir |
删除
作用 | 方法名 |
删除或(多级)目录 | deleteQuietly |
移动
作用 | 方法名 |
移动文件到目录中 | moveFileToDirectory |
移动目录到目录中 | moveDirectoryToDirectory |
复制
作用 | 方法名 |
复制文件到目录中 | copyFileToDirectory |
复制目录到目录中 | copyDirectoryToDirectory |
保存URL中信息到文件中 | copyURLToFile |
IO操作
作用 | 方法名 |
获取文件输入流 | openInputStream |
获取文件输出流 | openOutputStream |
读取内容到串 | readFileToString |
按行读取内容到串集合 | readLines |
按行写入文件 | writeLines |
获取文件输出流 | openOutputStream |
文件大小
作用 | 方法名 |
获取文件大小 | sizeOf |
字节大小可视化(转化为带单位的形式) | byteCountToDisplaySize |
文件比较
作用 | 方法名 |
文件内容是否相同 | contentEquals |
是否比指定文件最后修改时间新 | isFileNewer |
是否比指定文件最后修改时间旧 | isFileOlder |
判断文件是否是符号链接 | isSymlink |
查找文件
下面是自己补充了一些文件操作函数:
(1)文件重命名
/** * 重命名文件 * @author jitwxs * @version 创建时间:2018年2月27日 上午10:59:33 * @param path 目录路径 * @param oldName 源文件名 * @param newName 目标文件名 * @return */
public static boolean renameFile(String path, String oldName, String newName) {
//新的文件名和以前文件名不同时,才有必要进行重命名
if (!oldName.equals(newName)) {
File oldfile = new File(path + "/" + oldName);
File newfile = new File(path + "/" + newName);
if (!oldfile.exists()) {
System.out.println("重命名文件失败," + oldName +"不存在!");
return false;
}
//若在该目录下已经有一个文件和新文件名相同,则不允许重命名
if (newfile.exists()) {
System.out.println("重命名文件失败," + newName + "已经存在!");
return false;
} else {
oldfile.renameTo(newfile);
}
}
return true;
}
(2)裁剪图片为指定大小
/** * 调整图片大小 * @param srcImgPath 原图片路径 * @param distImgPath 转换大小后图片路径 * @param width 转换后图片宽度 * @param height 转换后图片高度 * @author jitwxs * @since 2018/6/17 21:54 */
public static void resizeImage(String srcImgPath, String distImgPath, int width, int height) throws IOException {
File srcFile = new File(srcImgPath);
Image srcImg = ImageIO.read(srcFile);
BufferedImage buffImg = null;
buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
buffImg.getGraphics().drawImage(
srcImg.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0,
0, null);
ImageIO.write(buffImg, "JPEG", new File(distImgPath));
}
(3)获取视频缩略图(指定帧)
依赖:
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv</artifactId>
<version>0.8</version>
</dependency>
/** * 获取指定视频的帧并保存为图片至指定目录 * * @param videofile 源视频文件路径 * @param framefile 截取帧的图片存放路径 * @throws Exception */
public static void fetchFrame(String videofile, String framefile) throws Exception {
long start = System.currentTimeMillis();
File targetFile = new File(framefile);
FFmpegFrameGrabber ff = new FFmpegFrameGrabber(videofile);
ff.start();
int lenght = ff.getLengthInFrames();
int i = 0;
Frame f = null;
while (i < lenght) {
// 过滤前5帧,避免出现全黑的图片,依自己情况而定
f = ff.grabFrame();
if ((i > 5) && (f.image != null)) {
break;
}
i++;
}
IplImage img = f.image;
int owidth = img.width();
int oheight = img.height();
// 对截取的帧进行等比例缩放
int width = 800;
int height = (int) (((double) width / owidth) * oheight);
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
bi.getGraphics().drawImage(f.image.getBufferedImage().getScaledInstance(width, height, Image.SCALE_SMOOTH),
0, 0, null);
ImageIO.write(bi, "jpg", targetFile);
//ff.flush();
ff.stop();
}
还没有评论,来说两句吧...