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.ArrayList;
import java.util.List; import org.apache.log4j.Logger; public class FileUtils { /**
* 读取一个文件夹下所有文件的绝对路径。忽略此文件夹下的问价夹
*
* @param absFolderPath
* 文件夹绝对路径
* @return List<String>
*/
public static List<String> readFilePath(String absFolderPath) {
List<String> paths = new ArrayList<String>();
File file = new File(absFolderPath);
// 文件夹
if (file.isDirectory()) {
for (File childFile : file.listFiles()) {
// 不是文件夹并且没有被操作
if (!childFile.isDirectory()) {
// 路径通过"/"手动创建,避免因window下出现反斜杠\ 路径的情况
paths.add(absFolderPath + "/" + childFile.getName());
}
}
}
return paths;
} /**
* 通过文件的绝对路径删除文件,如果传入是文件夹路径,忽略并返回false
*
* @param absFilePath
* 文件绝对路径
* @return boolean
*/
public static boolean deleteFile(String absFilePath) {
boolean result = false;
File file = new File(absFilePath);
if (!file.isDirectory()) {
file.delete();
result = true;
}
return result;
} /**
* 删除问价夹下所有文件
*
* @param absFolderPath
* @return boolean
*/
public static boolean deleteAllFileInTheFolder(String absFolderPath) {
boolean result = true;
for (String filePath : FileDealUtil.readFilePath(absFolderPath)) {
result = result && FileDealUtil.deleteFile(filePath);
}
return result;
} /**
* 将文件移动到指定问价夹下
*
* @param filePath
* 文件路径
* @param folderPath
* 文件夹路径
* @return String 移动后文件路径,filePath为文件夹或folderPath为文件或文件正在被其他进程打开无法移动返回null */
public static String moveFile(String filePath, String folderPath) {
String path = null; File file = new File(filePath);
if (file.isDirectory())
return path;
File folder = new File(folderPath);
if (!folder.exists()) {
folder.mkdirs();
}
if (!folder.isDirectory())
return path; File nowFile = new File(folderPath + "/" + file.getName());
if (nowFile.exists())
nowFile.delete();
if (file.renameTo(new File(folder, file.getName())))
path = folderPath + "/" + file.getName();
return path;
} /**
* 得到文件输入流
*
* @param path
* 绝对路径
* @return InputStream */
public static InputStream getFile(String path) {
// First try to read from the file system ...
File file = new File(path);
if (file.exists() && file.canRead()) {
try {
return new FileInputStream(file);
} catch (FileNotFoundException e) {
// continue
return null;
}
}
return null;
} /**
* 判断文件夹是否存在 void
*
*/
public static boolean existFolder(String folderPath) {
File file = new File(folderPath);
return file.exists();
} /**
* 创建文件夹
*
* @param folderPath
* void
*/
public static void creatFolder(String folderPath) {
if (!existFolder(folderPath)) {
File file = new File(folderPath);
file.mkdirs();
}
} /**
* 获取文件大小 如果文件存在并且不是目录,返回文件大小,如果文件不存在或者是目录,返回0
*
* @return Long 单位bytes
*/
public static Long getFileSize(String filePath) {
File file = new File(filePath);
if (file.exists() && !file.isDirectory()) {
return file.length();
} else {
return 0L;
}
} /**
* 从文件路径中分离出文件名
*
* @param filePath
* @return String
*/
public static String splitFileName(String filePath) {
return filePath.substring(filePath.lastIndexOf("/") + 1);
} /**
*
* @param filePath
* @param inputStream
* @return boolean
*/
public static boolean writeFile(String filePath, InputStream inputStream) {
OutputStream outputStream = null;
try {
outputStream = new FileOutputStream(filePath); int bytesRead = 0;
byte[] buffer = new byte[2048];
while ((bytesRead = inputStream.read(buffer, 0, 2048)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
return true;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
} finally {
try {
outputStream.close();
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

FileUtils的更多相关文章

  1. Java程序员的日常—— FileUtils工具类的使用

    package cn.xingoo.learn.commons; import org.apache.commons.io.FileUtils; import org.apache.commons.i ...

  2. cocos2dx的android版FileUtils的坑

    cocos2dx3.13,FileUtils-android.cpp中可以看到: FileUtils::Status FileUtilsAndroid::getContents(const std:: ...

  3. Java File类总结和FileUtils类

    Java File类总结和FileUtils类 文件存在和类型判断 创建出File类的对象并不代表该路径下有此文件或目录. 用public boolean exists()可以判断文件是否存在. Fi ...

  4. Can't exec "aclocal": No such file or directory at /usr/share/autoconf/Autom4te/FileUtils.pm line 326.

    今天执行:autoreconf -fvi的时候出现如下错误: autoreconf: Entering directory `.' autoreconf: configure.in: not usin ...

  5. 文件相关操作工具类——FileUtils.java

    文件相关操作的工具类,创建文件.删除文件.删除目录.复制.移动文件.获取文件路径.获取目录下文件个数等,满足大多数系统需求. 源码如下:(点击下载 FileUtils.java) import jav ...

  6. 应用apache FileUtils把网页另存为文件

    public static void foo() { try { URL url = new URL("http://www.webservicex.net/globalweather.as ...

  7. Caused by: java.lang.ClassNotFoundException: org.apache.commons.io.FileUtils

    1.错误叙述性说明 警告: Could not create JarEntryRevision for [jar:file:/D:/MyEclipse/apache-tomcat-7.0.53/web ...

  8. apache的FileUtils方法大全

    FileUtils 获取系统的临时目录路径:getTempDirectoryPath() [java] view plaincopyprint? public static String getTem ...

  9. FileUtils类介绍

    Java的文件操作太基础,缺乏很多实用工具,比如对目录的操作,支持就非常的差了.如果你经常用Java操作文件或文件夹,你会觉得反复编写这些代码是令人沮丧的问题,而且要大量用到递归. 下面是的一个解决方 ...

随机推荐

  1. [React] React Fundamentals: with-addons - ReactLink

    It can be tedious to type out all the boilerplate needed to get the DOM and states in React to synch ...

  2. agentzh --春哥--调试专家

    https://github.com/agentzh/perl-systemtap-toolkit https://github.com/openresty http://openresty.org/ ...

  3. eslint error

    暂时不想解决 报错: 3:16 error Component should be written as a pure function react/prefer-stateless-function ...

  4. IE6与W3C标准的盒模型差异

    盒子模型(Box Model)是 CSS 的核心,现代 Web 布局设计简单说就是一堆盒子的排列与嵌套,掌握了盒子模型与它们的摆放控制,会发现再复杂的页面也不过如此,然而,任何美好的事物都有缺憾,盒子 ...

  5. C# 面向对象编程的继承性-多继承

    多继承 如果要使用多继承,需要使用接口,因为C#中的类只支持单继承,而接口支持多继承,实现多继承时,继承的多个接口中间用逗号(,)隔开. 说明: 实现多继承时,继承的可以是多个接口,也可以是一个类及多 ...

  6. MySQL性能测试工具之mysqlslap使用详解

    mysqlslap是mysql自带的基准测试工具,优点:查询数据,语法简单,灵活容易使用.该工具可以模拟多个客户端同时并发的向服务器发出查询更新,给出了性能测试数据而且提供了多种引擎的性能比较.msq ...

  7. 【原创】Git版本控制器的基本使用

    关于git Git,是一个分布式版本控制软件.最初本是为了更好的管理Linux内核开发而被林纳斯·托瓦兹开发,后来因为项目开发中版本控制的强烈需求,而git也日趋成熟,最终成为了一个独立的版本控制软件 ...

  8. 微信小程序开发之 下拉刷新,上拉加载更多

    本文记载了如何在微信小程序里面实现下拉刷新,上拉加载更多 先开看一下界面 大致如此的界面吧. 这个Demo使用了微信的几个Api和事件,我先列出来. 1.wx.request (获取远程服务器的数据, ...

  9. OC文件操作(2)

    NSFileManager 文件管理器完成文件的创建.移动.拷贝等管理操作 1.查询文件和目录  OC中查询路径下的目录主要分为浅度遍历和深度遍历.  浅度遍历  NSFileManager * ma ...

  10. 【转】 C语言自增自减运算符深入剖析

    转自:http://bbs.csdn.net/topics/330189207 C语言的自增++,自减--运算符对于初学者来说一直都是个难题,甚至很多老手也会产生困惑,最近我在网上看到一个问题:#in ...