FIle类常用工具方法整理(持续更新)
1.递归遍历一个目录,获取所有文件名(也可以取到绝对路径)
public static void traverse(String filePath, List<String> files) {
if (StringUtils.isBlank(filePath)){
return ;
}
try{
File superFile = new File(filePath);
if (superFile.exists()) {
File[] fileList = superFile.listFiles();
if (null != files && fileList.length > 0) {
for (File file : fileList) {
// 还是文件夹
if (file.isDirectory()) {
traverse(file.getAbsolutePath(),files);
} else {
files.add(file.getName()); //文件名
//files.add(file.getAbsolutePath()); //文件绝对路径
}
}
}
}
}catch (Exception e){
//log
}
return ;
}
2.获取文件大小,自动用K、M、G表示。
public static String parseSize(long length){
StringBuilder size = new StringBuilder();
DecimalFormat format = new DecimalFormat("###.0");
if (length < 1024) {
size.append((int) length).append(" B");
}else if (length >= 1024 && length < 1024 * 1024) {
double i = (length / (1024.0));
size.append(format.format(i)).append(" K");
}else if (length >= 1024 * 1024 && length < 1024 * 1024 * 1024) {
double i = (length / (1024.0 * 1024.0));
size.append(format.format(i)).append(" M");
}else if (length >= 1024 * 1024 * 1024) {
double i = (length / (1024.0 * 1024.0 * 1024.0));
size.append(format.format(i)).append(" G");
}
return size.toString();
}
3.Multipart文件转存为本地的File。
public static void multipartToFile(MultipartFile file, String fileFolder){
FileOutputStream outputStream = null;
try {
File newFileFolder = new File(fileFolder);
if (!newFileFolder.exists()) {
newFileFolder.mkdirs();
}
fileFolder = newFileFolder.getAbsolutePath() + File.separator + file.getOriginalFilename();
outputStream = new FileOutputStream(new File(fileFolder));
IOUtils.copy(file.getInputStream(), outputStream);
} catch (Exception e) {
// log
} finally {
IOUtils.closeQuietly(outputStream);
}
}
4.清理指定目录下一天前(时间可以指定)的文件。
public static void cleanDirectory(String dir, long ttl) {
File file = new File(dir);
String[] subDirNames = file.list(new FilenameFilter() {
@Override
public boolean accept(File current, String name) {
return new File(current, name).isDirectory();
}
});
if (subDirNames != null) {
for (String name : subDirNames) {
File subDir = new File(dir + File.separator + name);
if (System.currentTimeMillis() - subDir.lastModified() > ttl) {
try {
FileUtils.deleteDirectory(subDir); //import org.apache.commons.io.FileUtils;
} catch (Exception e) {
// log
}
}
}
}
}
5.把字符串存入指定文件。
public static void strToFile(String content, File outFile) {
OutputStream os = null;
try {
File parent = outFile.getParentFile();
if (!parent.exists()) {
parent.mkdirs();
}
if (!outFile.exists()) {
outFile.createNewFile();
}
os = new FileOutputStream(outFile);
IOUtils.write(content, os);
} catch (Exception e) {
// log
} finally {
IOUtils.closeQuietly(os);
}
}
FIle类常用工具方法整理(持续更新)的更多相关文章
- File类常用的方法与字节流类方法简介
File类常用的方法 获取功能的方法 public String getAbsolutePath() :返回此File的绝对路径名字符串. public String getPath() :将此Fil ...
- JavaScript 深入学习及常用工具方法整理 ---- 01.浮点数
在JavaScript中是不区分整数值和浮点数值的,其中所有的数字均用浮点数值表示.JavaScript采用IEEE 754标准(有兴趣可以浏览网络规范分类下的IEEE 754标准,需要原文件请在留言 ...
- BAT 前端开发面经 —— 吐血总结 前端相关片段整理——持续更新 前端基础精简总结 Web Storage You don't know js
BAT 前端开发面经 —— 吐血总结 目录 1. Tencent 2. 阿里 3. 百度 更好阅读,请移步这里 聊之前 最近暑期实习招聘已经开始,个人目前参加了阿里的内推及腾讯和百度的实习生招聘, ...
- iOS:开发常用GitHub开源项目(持续更新)
IOS开发常用GitHub开源项目(持续更新) 数据类 开源库 作者 简介 AFNetworking Mattt 网络请求库 ASIHTTPRequest pokeb 网络请求库 Alamofire ...
- C#File类常用的文件操作方法(创建、移动、删除、复制等)
File类,是一个静态类,主要是来提供一些函数库用的.静态实用类,提供了很多静态的方法,支持对文件的基本操作,包括创建,拷贝,移动,删除和 打开一个文件. File类方法的参量很多时候都是路径path ...
- 常用js方法整理common.js
项目中常用js方法整理成了common.js var h = {}; h.get = function (url, data, ok, error) { $.ajax({ url: url, data ...
- java开发中遇到的问题及解决方法(持续更新)
摘自 http://blog.csdn.net/pony12/article/details/38456261 java开发中遇到的问题及解决方法(持续更新) 工作中,以C/C++开发为主,难免与其他 ...
- 项目中常用js方法整理common.js
抽空把项目中常用js方法整理成了common.js,都是网上搜集而来的,大家一起分享吧. var h = {}; h.get = function (url, data, ok, error) { $ ...
- jQuery常用工具方法
前面的话 jQuery提供一些与元素无关的工具方法,不必选中元素,就可以直接使用这些方法.如果理解原生javascript的继承原理,那么就能理解工具方法的实质.它是定义在jQuery构造函数上的方法 ...
随机推荐
- Android如何获取屏幕的分辨
在实际的项目中,我们经常要得到当前屏幕的分辨率,进行机型适配,得到分辨率其实很简单,主要有两种方法. 方法一: Display mDisplay = getWindowManager().getDef ...
- html基础知识介绍
1 前端概要 前端三大利器 1.html 赤裸裸的人 2.css 穿上华丽的衣服 3.js 让人生动起来 2 HTML本质及在web程序中的作用 2.1 介绍 HTML 1.一套规则,浏览 ...
- javaWeb中的JDBC学习入门
学习引荐地址:https://www.cnblogs.com/xdp-gacl/p/3946207.html 一.JDBC的相关概念介绍 1.1 数据库驱动 其实就好比我们平时使用到的独立声卡.网卡之 ...
- [转]SSH包全解
Struts2 Core Libraries 必须引入的包 : struts2-core.jar——Struts2的核心包 xwork-core.jar——Command模式框架,WebWork和St ...
- Mysql双主实战
参考:http://dev.mysql.com/doc/refman/5.1/en/replication-options-slave.htmlhttp://blog.chinaunix.net/ui ...
- POJ 3608 Bridge Across Islands(旋转卡壳,两凸包最短距离)
Bridge Across Islands Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7202 Accepted: ...
- 前端工业化工具Gulp初体验
1. 全局安装 gulp: npm install --global gulp 2.在项目目录下,用以下命令创建一个基本的package.json文件 npm init 3.安装Gulp npm in ...
- 流畅的python第五章一等函数学习记录
在python中,函数是一等对象,一等对象是满足以下条件的程序实体 1在运行时创建 2能复制给变量或数据结构的元素 3能作为参数传给函数 4能作为函数的返回结果 高阶函数(接受函数作为参数或者把函数作 ...
- 更改"xxxx" 的权限: 不允许的操作
[摘要:做为root用户,用chmod为何改没有了文件权限 以ROOT用户上岸,当用chmod改文件权限时,体系表现无权变动,为何 文件名是:aa chmod 777 aa chmod: changi ...
- jQuery选择器的灵活用法
// 摘自: http://hi.baidu.com/274084093/item/47a4ce696e89e534ad3e836b jQuery中选择器很强大,可以根据元素名称.ID.class等多 ...