- 浏览: 322352 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (178)
- 程序人生 (9)
- 工作总结 (16)
- 我的功能实现 (8)
- J2SE总结 (8)
- 面试中用到的 (10)
- struts2 (15)
- jsp (4)
- js (21)
- 数据库 (7)
- 数据结构和算法 (3)
- jQuery (18)
- hibernate (3)
- web service (2)
- spring (1)
- mySQL (7)
- 服务器 (1)
- 缓存 (2)
- nginx (1)
- jquery.ui (1)
- 各类标签(Date转 string) (1)
- Raphael (3)
- maven (2)
- 大型分布式网站架构设计 (2)
- linux (1)
- java易犯错误 (1)
- 优化 (4)
最新评论
-
15381049808:
楼主写的很赞 我对中间的批量发送拿过来现在还好用 受益匪浅
java实现邮件抄送,密送,多个附件发送 -
猜猜猜:
你这不是选择了语言之后总是跳到同一个页面吗?比如登录页面,选择 ...
struts2国际化——实例 -
itshu:
struts2国际化——实例 -
itshu:
[flash=200,200][url][img][list] ...
struts2国际化——实例 -
ZP0605:
com.asia.home.cn这个包名好熟悉
struts2拦截器管理日志
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);
}
}
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);
}
}
发表评论
-
持有对象
2011-12-14 14:34 982Collection: List: Ar ... -
泛型1————弄懂了它就基本上会用泛型了
2011-07-26 14:11 849[size=medium][/size] 引用. ... -
追MM的23种设计模式
2011-07-26 11:47 784引用1、FACTORY?追MM少不了请吃饭了,麦当劳的鸡翅 ... -
Java 5泛型深入研究 泛型——精解
2011-07-26 11:43 777[size=medium][/size] 引用Jav ... -
IO流——精解
2011-07-26 11:00 632[size=medium][/size] 引用一 ... -
java多线程
2011-07-13 18:08 1036这几天不断添加新内容,给个大概的提纲吧,方面朋友们阅读,各部分 ... -
容器总结
2010-11-26 16:02 8031: /** * * @author zhuty ...
相关推荐
文件文件夹操作。解决SVN空文件夹提交失败。运行软件输入:命令-空文件夹插入文件。填充文件即可提交。
常见的MFC中的字符串文件文件夹操作,整理了一下,添加到工程中可以直接使用。
04_财务数据审计分析系统(文件文件夹操作公用组件).part2.rar
04_财务数据审计分析系统(文件文件夹操作公用组件).part1.rar
MFC文件夹及文件操作
Win7 文件、文件夹操作Win7 文件、文件夹操作Win7 文件、文件夹操作Win7 文件、文件夹操作Win7 文件、文件夹操作Win7 文件、文件夹操作Win7 文件、文件夹操作Win7 文件、文件夹操作
MFC文件及文件夹操作封装.rar MFC文件及文件夹操作封装.rar MFC文件及文件夹操作封装.rar MFC文件及文件夹操作封装.rar MFC文件及文件夹操作封装.rar
6.文件对话框文件夹操作演示(Visual C++编程 源代码)6.文件对话框文件夹操作演示(Visual C++编程 源代码)6.文件对话框文件夹操作演示(Visual C++编程 源代码)6.文件对话框文件夹操作演示(Visual C++编程 源...
Asp.Net文件和文件夹操作大全
操作文件及文件夹的源代码,是涉及文件及文件夹操作比较全面的源代码
c#实现对文件,文件夹新建、修改、移动、删除、重命名、遍历等基本操作。有例子,大家交流使用。
集中常用的文件和文件夹操作方法。如复制、剪切、查找、删除等。对文件夹的建立、打开等。用起来很简便
文件 文件夹操作, 增加 删除 修改, 复制 txt 文件数据换行,数据写入, 清空, 数据附加。
文件与文件夹操作课件.ppt
针对文件操作:读写,文件夹操作:批量对应获取文件信息,批量删除
Excel VBA_文本文件 文件夹操作实例
C++Builder 6的文件和文件夹操作
源代码,介绍最基本的VC中文件与文件夹操作,选择保存文件的路径,打开一个文件夹,选择一个文件等等
JAVA文件操作 获取文件文件夹大小、文件夹下子文件个数
此文件支持对文件及文件夹结构的多种操作,其中包括: 读取文本文件内容 遍历指定路径下指定后缀的所有文件 新建目录 多级目录创建 新建文件 有编码方式的文件创建 删除文件 删除文件夹 删除指定文件夹下所有文件 ...