- 浏览: 89543 次
- 性别:
- 来自: 北京
最新评论
-
JaHunter:
bootstrap示例:http://www.bootcss. ...
实用网址 -
JaHunter:
http://www.entrepreneur.com/art ...
read -
zdsong2009:
很受用,现在公司正(ˇˍˇ) 想做一个B2C,
B2C -
marshan:
停掉tomcat,然后删除temp目录下的东东。
java.io.NotSerializableException错误解决方法 -
JaHunter:
http://www.filamentgroup.com/la ...
实用网址
import java.io.*;
public class FileOperate {
public FileOperate() {
}
/**
* 新建目录
* @param folderPath String 如 c:/fqf
* @return boolean
*/
public void newFolder(String folderPath) {
try {
String filePath = folderPath;
filePath = filePath.toString();
java.io.File myFilePath = new java.io.File(filePath);
if (!myFilePath.exists()) {
myFilePath.mkdir();
}
}
catch (Exception e) {
System.out.println("新建目录操作出错");
e.printStackTrace();
}
}
/**
* 新建文件
* @param filePathAndName String 文件路径及名称 如c:/fqf.txt
* @param fileContent String 文件内容
* @return boolean
*/
public void newFile(String filePathAndName, String fileContent) {
try {
String filePath = filePathAndName;
filePath = filePath.toString();
File myFilePath = new File(filePath);
if (!myFilePath.exists()) {
myFilePath.createNewFile();
}
FileWriter resultFile = new FileWriter(myFilePath);
PrintWriter myFile = new PrintWriter(resultFile);
String strContent = fileContent;
myFile.println(strContent);
resultFile.close();
}
catch (Exception e) {
System.out.println("新建目录操作出错");
e.printStackTrace();
}
}
/**
* 删除文件
* @param filePathAndName String 文件路径及名称 如c:/fqf.txt
* @param fileContent String
* @return boolean
*/
public void delFile(String filePathAndName) {
try {
String filePath = filePathAndName;
filePath = filePath.toString();
java.io.File myDelFile = new java.io.File(filePath);
myDelFile.delete();
}
catch (Exception e) {
System.out.println("删除文件操作出错");
e.printStackTrace();
}
}
/**
* 删除文件夹
* @param filePathAndName String 文件夹路径及名称 如c:/fqf
* @param fileContent String
* @return boolean
*/
public 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 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 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);
}
inStream.close();
}
}
catch (Exception e) {
System.out.println("复制单个文件操作出错");
e.printStackTrace();
}
}
/**
* 复制整个文件夹内容
* @param oldPath String 原文件路径 如:c:/fqf
* @param newPath String 复制后路径 如:f:/fqf/ff
* @return boolean
*/
public 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 void moveFile(String oldPath, String newPath) {
copyFile(oldPath, newPath);
delFile(oldPath);
}
/**
* 移动文件到指定目录
* @param oldPath String 如:c:/fqf.txt
* @param newPath String 如:d:/fqf.txt
*/
public void moveFolder(String oldPath, String newPath) {
copyFolder(oldPath, newPath);
delFolder(oldPath);
}
}
发表评论
-
统计代码行数
2013-11-20 10:47 471public class ReadFile { ... -
常用验证方法
2013-11-20 10:44 4711.正则表达式 /** ... -
DateUtil
2013-11-20 10:42 625DateUtil 1.取得当前日期“年”,并判断此年 ... -
set相关
2013-11-20 10:41 484ArrayList 允许快速随机存取,相比于Link ... -
集合排序
2013-11-20 10:38 917集合排序(BigDecimal、根据对象某个属性排序) ... -
编程良好习惯
2013-03-22 21:38 614转: 对于面向对象编程语言,有5个基本原则能帮助你写出整洁 ... -
关于dll文件的生成以及找不到jni.h的解决办法
2010-08-02 18:48 1645来源:http://jzgl-javaeye.iteye.co ... -
java 产生随机数的几种方式
2010-07-12 12:57 1274一.在j2se里我们可以使用Math.random()方法来产 ... -
关于session的详细解释
2010-07-04 15:24 810一、术语session 在我的经验里,session这个 ... -
mysql 删除多余重复记录
2010-03-25 00:30 1620(stu表中的名字(name)是重复的) 问题: 在Mys ... -
java基本数据类型
2010-03-10 18:38 1753(写了很多 Java程序 ... -
proxool连接池
2009-12-18 10:50 674proxool 在hibernate.cfg.xml中加入 & ... -
tomcat虚拟主机配置
2009-11-13 15:41 584tomcat:虚拟主机配置 在server.xml中 &l ... -
struts国际化手动切换
2009-11-11 16:19 1255当页面请求时,把本地信息(主要为Accept-Language ... -
JAVA中如何用接口实现多继承和多态
2009-11-07 17:57 884JAVA里没有多继承,一 ... -
servlet,jsp乱码及解决方案
2009-11-07 11:40 2307MIME多用途Internet邮件扩 ... -
jstl数字格式
2009-07-22 11:45 1011<fmt:formatNumber value=&quo ... -
jstl日期
2009-07-22 11:43 1532<%@ page language="java ... -
Hibernate代码小集
2009-07-17 21:09 9751.hibernate:查找总共多少条记录 L ... -
mysql
2009-07-13 08:48 1052备份数据库:在mysql的bin目录下, C:\Progra ...
相关推荐
java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java...
io java 文件操作 命令 全部 io java 文件操作 命令 全部 io java 文件操作 命令 全部 io java 文件操作 命令 全部
Java文件操作大全
java文件操作总结 详细介绍 java对文件的操作 每个都有对应的例子
Java 文件操作大全···················································
Java文件操作封装类
非常全的java文件操作说明、示例,欢迎下载共同学习
java文件操作相关的类和基本用法,都是一些基本的用法,但很重要
java文件操作工具类是java中针对文件操作的一个工具类,可以方便的读取,下载,上传文件等操作,希望可以帮到大家。
Java文件操作大全(绝对的经典,值得收藏!) Java文件操作大全(绝对的经典,值得收藏!) Java文件操作大全(绝对的经典,值得收藏!)
java视频教程 Java文件操作 JavaFile
一些较为全面的java的文件操作的代码,在学习中有极大地帮助!!
java 文件操作类 20多个实用方法,操作文件的好帮手。 JAVA自定义Util大放送-集成j2ee精英团队十年编程之精华
Java文件操作Java文件操作