Java 文件(目录)操作全
关键字: Java 文件 目录 操作


package File;
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];
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);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
FileOperate f=new FileOperate();
f.copyFile("g:\\电影\\crazy stone.wmv", "g:\\222.wmv");
}
}
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1255433
发表评论
- 浏览: 42942 次
- 性别:

- 来自: 上海

- 详细资料
搜索本博客
最近加入圈子
链接
- Hibernate Tag 参考手册(中文翻译版1.0)
- 闲聊日语张宏
- 《NeoSpeech语音合成英、中、韩、日朗读引擎》
- 《新版中日交流标准日本语广播讲座初级》同步更新,2006年6月5日,更新全部ISO文件,最后更新[MP3!]
- 《番茄花园 Windows All In One DVD 特别版》[ISO]
- 中 国 DOS 联 盟
- 新DOS时代
- 《虚拟工作站 6.0.1 Build 55017》(VMware Workstation 6.0.1 Build 55017)VMware Workstation 6.0.1 Build 55017
- 《经典2D即时战略游戏集》(Classic 2D-RTS Games Collection)完美硬盘版
- 标题: 关于DOS游戏在XP系统下运行的简单总结!, 新版VDMSound和DOSBox已经出炉啦!
- DOSBox / VDMSound常見FAQ
- D - F e n d 簡 單 教 學
- Get It Done with MySQL 5
- 《沪江日语原创作品资料集(新增交响情人梦)》(jp.hjenglish.com)合辑
- SQL语句教程 - 由范例学习SQL语句
- 飞奔·慢行
- 地铁换乘/时间/价钱
最新评论
-
Hibernate3.x调用存储过程 ...
您好!hibernate提供的在*.hbm.xml中配置调用存储过程,关于< ...
-- by cheatsky -
KMP字符串模式匹配详解
我虽然曾经学过,看来一下感觉有些时候还是比较难理解!
-- by zhoujj303030 -
一个老IT人的自白:看十年 ...
我不认同三星有多牛,说白了,我认为高丽棒子做的东西真的一般般
-- by xbwolf -
JavaTiger(Java5.0) 新特 ...
感谢分享,解决了我的困惑
-- by onwulc -
磨刀不误砍柴功—难忘的Ja ...
...
-- by wcj10051891






评论排行榜