- 浏览: 157649 次
- 性别:
- 来自: 厦门
文章分类
最新评论
-
jxpath:
局部变量的生命周期-----引用传递作为函数值返回的呢?
php变量的生命周期 -
mdusa_java:
文件上传,使用Apache组件上传文件 -
qzlijian:
spring MVC 转向(重定向) -
青春的、脚步:
解决办法:<s:form action="&q ...
Struts2中action的方法被执行两次 -
zcz123:
这不正是你的真实写照吗
程序员装B指南
public class FileUtil {
/**
* 功能:创建文件目录
* @param fileName
* @return
*/
public static boolean creatFold(String fileName){
File f = new File(fileName);
if(!f.exists()){
return f.mkdirs();
}
return true;
}
/**
* 功能:数据寫入硬盤
* @param fold
* 地址
* @param fileName
* 文件名
* @param date
* 写入的数据
* @throws Exception
*/
public static void saveFile(String fold,String fileName,byte[] date) throws Exception{
FileOutputStream out =null;
BufferedOutputStream bStream = null;
ByteArrayInputStream in = new ByteArrayInputStream(date);
File f = new File(fold);
if(!f.mkdir()){
if(!f.mkdirs()){
throw new Exception("创建文件路径失败");
}
}
out = new FileOutputStream(new File(f,fileName));
bStream = new BufferedOutputStream(out);
byte[] bs = new byte[1024*10];
while(( in.read(bs)) != -1){
out.write(bs);
}
out.flush();
out.close();
in.close();
bStream.close();
}
/**
* 功能:文件写入硬盘
* @param fold
* @param fileName
* @param file
* @throws Exception
*/
public static void saveFile(String fold,String fileName,File file) throws Exception{
FileOutputStream out = null;
FileInputStream in = new FileInputStream(file);
File f = new File(fold);
if(!f.mkdir()){
if(!f.mkdirs()){
throw new Exception("创建文件路径失败");
}
}
out = new FileOutputStream(fold+fileName);
byte[] buffer = new byte[1024];
int len =0;
while((len = in.read(buffer))!= -1){
out.write(buffer, 0, len);
}
out.flush();
out.close();
in.close();
}
/**
* 功能:根据路径获取文件
* @param fold
* @return
*/
public static File getFile(String fold){
if(fold != null && !fold.equals("")){
File f = new File(fold);
if(f.isFile()){
return f;
}else{
return null;
}
}else{
return null;
}
}
/**
* 功能:取得文件夹
* @param fold
* @return
*/
public static File getFold(String fold){
if(fold !=null && !fold.equals("")){
File f = new File(fold);
if(f.isDirectory()){
return f;
}else{
return null;
}
}else{
return null;
}
}
/**
* 功能:删除文件
* @param filePath
*/
public static void delFile(String filePath){
if(filePath != null && !filePath.equals("")){
File f = new File(filePath);
if(f.isFile()){
f.delete();
}
}
}
/**
* 功能:删除文件夹
* @param filePathAndName String 文件夹路径及名称 如c:/fqf
* @param fileContent String
* @return boolean
*/
public static void delFolder(String folderPath) {
try {
delAllFile(folderPath); //删除完里面所有内容
String filePath = folderPath;
filePath = filePath.toString();
java.io.File myFilePath = new java.io.File(filePath);
myFilePath.delete(); //删除空文件夹
}
catch (Exception e) {
System.out.println("删除文件夹操作出错");
e.printStackTrace();
}
}
/**
* 功能:删除文件夹里面的所有文件
* @param path String 文件夹路径 如 c:/fqf
*/
public static void delAllFile(String path) {
File file = new File(path);
if (!file.exists()) {
return;
}
if (!file.isDirectory()) {
return;
}
String[] tempList = file.list();
File temp = null;
for (int i = 0; i < tempList.length; i++) {
if (path.endsWith(File.separator)) {
temp = new File(path + tempList[i]);
}
else {
temp = new File(path + File.separator + tempList[i]);
}
if (temp.isFile()) {
temp.delete();
}
if (temp.isDirectory()) {
delAllFile(path+"/"+ tempList[i]);//先删除文件夹里面的文件
delFolder(path+"/"+ tempList[i]);//再删除空文件夹
}
}
}
/**
* 功能:复制单个文件
* @param oldPath String 原文件路径 如:c:/fqf.txt
* @param newPath String 复制后路径 如:f:/fqf.txt
* @return boolean
*/
public static void copyFile(String oldPath, String newPath) {
try {
int bytesum = 0;
int byteread = 0;
File oldfile = new File(oldPath);
if (oldfile.exists()) { //文件存在时
InputStream inStream = new FileInputStream(oldPath); //读入原文件
FileOutputStream fs = new FileOutputStream(newPath);
byte[] buffer = new byte[1444];
int length;
while ( (byteread = inStream.read(buffer)) != -1) {
bytesum += byteread; //字节数 文件大小
System.out.println(bytesum);
fs.write(buffer, 0, byteread);
}
fs.flush();
fs.close();
inStream.close();
}
}
catch (Exception e) {
System.out.println("复制单个文件操作出错");
e.printStackTrace();
}
}
/**
* 复制整个文件夹内容
* @param oldPath String 原文件路径 如:c:/fqf
* @param newPath String 复制后路径 如:f:/fqf/ff
* @return boolean
*/
public static void copyFolder(String oldPath, String newPath) {
try {
(new File(newPath)).mkdirs(); //如果文件夹不存在 则建立新文件夹
File a=new File(oldPath);
String[] file=a.list();
File temp=null;
for (int i = 0; i < file.length; i++) {
if(oldPath.endsWith(File.separator)){
temp=new File(oldPath+file[i]);
}
else{
temp=new File(oldPath+File.separator+file[i]);
}
if(temp.isFile()){
FileInputStream input = new FileInputStream(temp);
FileOutputStream output = new FileOutputStream(newPath + "/" +
(temp.getName()).toString());
byte[] b = new byte[1024 * 5];
int len;
while ( (len = input.read(b)) != -1) {
output.write(b, 0, len);
}
output.flush();
output.close();
input.close();
}
if(temp.isDirectory()){//如果是子文件夹
copyFolder(oldPath+"/"+file[i],newPath+"/"+file[i]);
}
}
}
catch (Exception e) {
System.out.println("复制整个文件夹内容操作出错");
e.printStackTrace();
}
}
/**
* 移动文件到指定目录
* @param oldPath String 如:c:/fqf.txt
* @param newPath String 如:d:/fqf.txt
*/
public static void moveFile(String oldPath, String newPath) {
copyFile(oldPath, newPath);
delFile(oldPath);
}
/**
* 移动文件到指定目录
* @param oldPath String 如:c:/fqf.txt
* @param newPath String 如:d:/fqf.txt
*/
public static void moveFolder(String oldPath, String newPath) {
copyFolder(oldPath, newPath);
delFolder(oldPath);
}
}
/**
* 功能:创建文件目录
* @param fileName
* @return
*/
public static boolean creatFold(String fileName){
File f = new File(fileName);
if(!f.exists()){
return f.mkdirs();
}
return true;
}
/**
* 功能:数据寫入硬盤
* @param fold
* 地址
* @param fileName
* 文件名
* @param date
* 写入的数据
* @throws Exception
*/
public static void saveFile(String fold,String fileName,byte[] date) throws Exception{
FileOutputStream out =null;
BufferedOutputStream bStream = null;
ByteArrayInputStream in = new ByteArrayInputStream(date);
File f = new File(fold);
if(!f.mkdir()){
if(!f.mkdirs()){
throw new Exception("创建文件路径失败");
}
}
out = new FileOutputStream(new File(f,fileName));
bStream = new BufferedOutputStream(out);
byte[] bs = new byte[1024*10];
while(( in.read(bs)) != -1){
out.write(bs);
}
out.flush();
out.close();
in.close();
bStream.close();
}
/**
* 功能:文件写入硬盘
* @param fold
* @param fileName
* @param file
* @throws Exception
*/
public static void saveFile(String fold,String fileName,File file) throws Exception{
FileOutputStream out = null;
FileInputStream in = new FileInputStream(file);
File f = new File(fold);
if(!f.mkdir()){
if(!f.mkdirs()){
throw new Exception("创建文件路径失败");
}
}
out = new FileOutputStream(fold+fileName);
byte[] buffer = new byte[1024];
int len =0;
while((len = in.read(buffer))!= -1){
out.write(buffer, 0, len);
}
out.flush();
out.close();
in.close();
}
/**
* 功能:根据路径获取文件
* @param fold
* @return
*/
public static File getFile(String fold){
if(fold != null && !fold.equals("")){
File f = new File(fold);
if(f.isFile()){
return f;
}else{
return null;
}
}else{
return null;
}
}
/**
* 功能:取得文件夹
* @param fold
* @return
*/
public static File getFold(String fold){
if(fold !=null && !fold.equals("")){
File f = new File(fold);
if(f.isDirectory()){
return f;
}else{
return null;
}
}else{
return null;
}
}
/**
* 功能:删除文件
* @param filePath
*/
public static void delFile(String filePath){
if(filePath != null && !filePath.equals("")){
File f = new File(filePath);
if(f.isFile()){
f.delete();
}
}
}
/**
* 功能:删除文件夹
* @param filePathAndName String 文件夹路径及名称 如c:/fqf
* @param fileContent String
* @return boolean
*/
public static void delFolder(String folderPath) {
try {
delAllFile(folderPath); //删除完里面所有内容
String filePath = folderPath;
filePath = filePath.toString();
java.io.File myFilePath = new java.io.File(filePath);
myFilePath.delete(); //删除空文件夹
}
catch (Exception e) {
System.out.println("删除文件夹操作出错");
e.printStackTrace();
}
}
/**
* 功能:删除文件夹里面的所有文件
* @param path String 文件夹路径 如 c:/fqf
*/
public static void delAllFile(String path) {
File file = new File(path);
if (!file.exists()) {
return;
}
if (!file.isDirectory()) {
return;
}
String[] tempList = file.list();
File temp = null;
for (int i = 0; i < tempList.length; i++) {
if (path.endsWith(File.separator)) {
temp = new File(path + tempList[i]);
}
else {
temp = new File(path + File.separator + tempList[i]);
}
if (temp.isFile()) {
temp.delete();
}
if (temp.isDirectory()) {
delAllFile(path+"/"+ tempList[i]);//先删除文件夹里面的文件
delFolder(path+"/"+ tempList[i]);//再删除空文件夹
}
}
}
/**
* 功能:复制单个文件
* @param oldPath String 原文件路径 如:c:/fqf.txt
* @param newPath String 复制后路径 如:f:/fqf.txt
* @return boolean
*/
public static void copyFile(String oldPath, String newPath) {
try {
int bytesum = 0;
int byteread = 0;
File oldfile = new File(oldPath);
if (oldfile.exists()) { //文件存在时
InputStream inStream = new FileInputStream(oldPath); //读入原文件
FileOutputStream fs = new FileOutputStream(newPath);
byte[] buffer = new byte[1444];
int length;
while ( (byteread = inStream.read(buffer)) != -1) {
bytesum += byteread; //字节数 文件大小
System.out.println(bytesum);
fs.write(buffer, 0, byteread);
}
fs.flush();
fs.close();
inStream.close();
}
}
catch (Exception e) {
System.out.println("复制单个文件操作出错");
e.printStackTrace();
}
}
/**
* 复制整个文件夹内容
* @param oldPath String 原文件路径 如:c:/fqf
* @param newPath String 复制后路径 如:f:/fqf/ff
* @return boolean
*/
public static void copyFolder(String oldPath, String newPath) {
try {
(new File(newPath)).mkdirs(); //如果文件夹不存在 则建立新文件夹
File a=new File(oldPath);
String[] file=a.list();
File temp=null;
for (int i = 0; i < file.length; i++) {
if(oldPath.endsWith(File.separator)){
temp=new File(oldPath+file[i]);
}
else{
temp=new File(oldPath+File.separator+file[i]);
}
if(temp.isFile()){
FileInputStream input = new FileInputStream(temp);
FileOutputStream output = new FileOutputStream(newPath + "/" +
(temp.getName()).toString());
byte[] b = new byte[1024 * 5];
int len;
while ( (len = input.read(b)) != -1) {
output.write(b, 0, len);
}
output.flush();
output.close();
input.close();
}
if(temp.isDirectory()){//如果是子文件夹
copyFolder(oldPath+"/"+file[i],newPath+"/"+file[i]);
}
}
}
catch (Exception e) {
System.out.println("复制整个文件夹内容操作出错");
e.printStackTrace();
}
}
/**
* 移动文件到指定目录
* @param oldPath String 如:c:/fqf.txt
* @param newPath String 如:d:/fqf.txt
*/
public static void moveFile(String oldPath, String newPath) {
copyFile(oldPath, newPath);
delFile(oldPath);
}
/**
* 移动文件到指定目录
* @param oldPath String 如:c:/fqf.txt
* @param newPath String 如:d:/fqf.txt
*/
public static void moveFolder(String oldPath, String newPath) {
copyFolder(oldPath, newPath);
delFolder(oldPath);
}
}
发表评论
-
连接池技术
2011-06-24 17:55 8031.为什么使用连接池技术:数据库的建立和连接都是极其消耗系统资 ... -
私有的构造方法不能被实例化
2011-04-26 20:58 886v -
JDBC,JDBC连接数据库
2011-03-30 20:55 888package servlet_jdbc; import j ... -
接口和抽象类
2011-03-29 19:00 837java只能单继承,但接口可以实现多个接口。一个类可以继承一个 ... -
instance of用于判断对象是否相等或满足某种类型
2011-03-29 17:52 679静态方法可以被继承。但是,静态方法不能被覆盖,如果父类种的静态 ... -
复制对象用clone
2011-03-29 17:51 737飞 -
基本数据类型的包装类,数据对象
2011-03-29 14:35 832java为每一种数据类型提供一个包装类,这些类是:Charac ... -
== ,equals与matches,contains的区别
2011-03-29 13:43 3660public static void main(String ... -
二分查找法,数组的查找方法,二分法
2011-03-28 17:16 1164public static int binarySearch( ... -
获取数组中的最大值,求最大值
2011-03-28 15:16 1003public static void main(String ... -
随机字母,生成随机字符串,如何生成随机字母
2011-03-28 12:39 1176public static void main(String ... -
验证类型,如何验证一个数据类型, 类型验证转换
2011-03-24 17:32 894public static void main(String ... -
数组转化为字符串,用逗号分开。并去掉最后一个逗号
2011-03-24 11:37 10846public static void main(String[ ... -
保留两位小数点
2011-03-24 10:55 871public static void main(String[ ... -
upload文件上传,文件名全部变小写
2011-01-17 14:53 967我用MultipartFile上传文件的时候,文件名本身是大小 ... -
文件上传,使用Apache组件上传文件
2011-01-14 14:55 4865使用Apache文件上传组件来接受浏览器上传的文件,该组件由多 ... -
IO 流的分类
2010-12-14 16:39 1032IO的分类: 1.按流的方向分:按流的方向分可以分为输入流跟 ... -
Java集合框架
2010-12-14 10:32 626在JAVA 集合中又主要分 ... -
java基础 interface,接口与抽象类
2010-12-10 17:43 9081.JAVA 接口 在JAVA中,接口有两种 ... -
JAVA IO
2010-12-10 17:37 8121.JAVA IO系统简介 JAVA IO (i ...
相关推荐
* FileUtil. Simple file operation class. * * @author BeanSoft * */ public class FileUtil { /** * The buffer. */ protected static byte buf[] = new byte[1024]; /** * Read content from ...
FileUtil(文件操作工具类)
fileutil工具类 处理文件流工具 private static File file; /** * 判断文件是否存在 * * @param path * 文件路径 * @return boolean */ public static boolean fileIsExists(String path) { if (path ==...
FileUtil类文件整理
通用文件方法,方发都很通用,在java中这些都是常用的
java中用FileUtil实现文件读写
public class FileUtil { /** * 新建目录 * @param folderPath String 如 c:/fqf * @return boolean */ public static void newFolder(String folderPath) { try { String filePath = folderPath; ...
java文件工具类FileUtil 递归获取一个文件夹(及其子文件夹)下所有文件 获取扩展名 (doc/docx/jpg等) 判断是否是图片 判断是否是压缩包 是否是word文档 是否是excel
创建文件工具类 包含创建文件夹 文件 临时文件等
主要为大家详细介绍了超全的文件工具类FileUtil,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
文件上传的工具类。里面包括一些文件的下载以及上传。都是封装好的一些方法,很好用的。
使用Qt框架编写的各种文件操作库,支持:csv、dbf、excel、ini、json、xml文件。
dateUtil fileUtil propertyUtil 反射 json 分页 jdbc struts2 string 一些java开发 常用的工具类的总结 封装
[工具类] 文件FileUtil.java [工具类] 通信客户端simpleClient.java [工具类] 通信服务端simpleServer.java [工具类] 框架StringUtil.java [工具类] 时间Time.java [工具类] 时间工具TimeUtil.java [工具类] 连...
Base64 工具类:Base64Util 文件读取工具类:FileUtil Json工具类:GsonUtils http 工具类:HttpUtil
[工具类] 文件FileUtil.java [工具类] 通信客户端simpleClient.java [工具类] 通信服务端simpleServer.java [工具类] 框架StringUtil.java [工具类] 时间Time.java [工具类] 时间工具TimeUtil.java [工具类] 连...
主要介绍了Android开发中的文件操作工具类FileUtil,结合完整实例形式分析了Android文件操作的常用技巧,包括文件的获取、遍历、搜索、复制、删除、判断等功能,需要的朋友可以参考下
从输入流中读取string,新建一个文件并写入内容,复制一个目录及其子目录、文件到另外一个目录 ,递归删除目录下的所有文件及子目录下所有文件,读取文本文件内容,以行的形式读取....
D:\002 我的工具类\001 流\文件操作整体\FileUtil.java D:\002 我的工具类\001 流\文件操作整体\valid.java D:\002 我的工具类\001 流\文件操作整体2 D:\002 我的工具类\001 流\文件操作整体2\Charsets.java D:\002 ...
FileUtil