您的位置:首页 > 其它

File的基本实用操作

2007-09-14 11:45 274 查看
常用的文件操作,包括目录,文件的创建,删除和修改等功能.

File的API连接:http://www.leftworld.net/online/j2sedoc/javaref/java.io.file_dsc.htm

源代码如下:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class FileUtils {

/**
* 创建目录
*
* @param folderPath:目录路径
* @return
* @throws IOException
*/
public static boolean createDirectory(String directoryPath)
throws IOException {
boolean result = false;
File f = new File(directoryPath);
if (!f.exists()) {
result = f.mkdirs();
} else {
throw new IOException("the file " + f.getName() + " exited");
}

return result;
}

/**
* 删除目录下所有文件
*
* @param directory
* (File 对象)
*/
public static void deleteDirectory(String directoryPath) {
File directory = new File(directoryPath);
File[] entries = directory.listFiles();
for (int i = 0; i < entries.length; i++) {
if (entries[i].isDirectory()) {
deleteDirectory(entries[i].getPath());
entries[i].delete();
} else {
entries[i].delete();
}
}

}

/**
* 创建文件
*
* @param filepath:文件所在目录路径,比如:c:/test/test.txt
* @return
*/
public static boolean createFile(String filepath) throws IOException {
boolean result = false;
File file = new File(filepath);
result = file.createNewFile();
file = null;
return result;
}

/**
* 删除目录下所有文件
*
* @param directory
* (File 对象)
*/
public static void deleteFiles(File directory) {
File[] entries = directory.listFiles();
for (int i = 0; i < entries.length; i++) {
entries[i].delete();
}

}

/**
* 删除文件
*
* @param filepath:文件所在物理路径
* @return
*/
public static boolean isDel(String filepath) {
boolean result = false;
File file = new File(filepath);
result = file.delete();
file = null;
return result;
}

/**
* 文件重命名
*
* @param filepath:文件所在物理路径
* @param destname:新文件名
* @return
*/
public static boolean renamefile(String filepath, String destname) {
boolean result = false;
File f = new File(filepath);
String fileParent = f.getParent();
String filename = f.getName();
File rf = new File(fileParent + "//" + destname);
if (f.renameTo(rf)) {
result = true;
}
f = null;
rf = null;
return result;
}

public static void copy(String srcfilepath, String destfilepath)
throws IOException {

copy(new File(srcfilepath), new File(destfilepath));

}

/**
* 复制文件f1至f2
*
* @param f1
* 源文件
* @param f2
* 目标文件
* @throws IOException
*/
public static void copy(File f1, File f2) throws IOException {
// 创建文件输入输出流对象
FileInputStream is = new FileInputStream(f1);
FileOutputStream os = new FileOutputStream(f2);
// 设定读取的字节数
int count, n = 512;
byte buffer[] = new byte
;
// 读取输入流
count = is.read(buffer, 0, n);
while (count != -1) {
os.write(buffer, 0, count);
count = is.read(buffer, 0, n);
}
// 关闭输入输出流
is.close();
os.close();
}

public boolean copyDirectory(String SrcDirectoryPath,
String DesDirectoryPath) {

try {
// 创建的目录不存在
File F0 = new File(DesDirectoryPath);
if (!F0.exists()) {
if (!F0.mkdir()) {
System.out.println("创建的目录不存在!");
}
}
File F = new File(SrcDirectoryPath);
File[] files = F.listFiles(); //

String srcName = "";
String desName = "";
int currentFile = 0;
// 这个拷贝
for (currentFile = 0; currentFile < files.length; currentFile++) {
if (!files[currentFile].isDirectory()) {
// 是文件,则直接拷贝
srcName = files[currentFile].toString();
desName = DesDirectoryPath + "//"
+ files[currentFile].getName();

copy(srcName, desName);
}
// 文件夹
else {
//
if (copyDirectory(files[currentFile].getPath().toString(),
DesDirectoryPath + "//"
+ files[currentFile].getName().toString())) {

} else {
System.out.println("SubDirectory Copy Error!");
}
}
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}

/**
* 取得文件或文件夹信息:路径、修改时间
*
* @param file
* @throws IOException
* @throws IOException
*/
public static void getInfo(String filepath) throws IOException {
getInfo(new File(filepath));
}

public static void getInfo(File file) throws IOException {
// 初始化时间格式
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
if (file.isFile())
// 返回抽象路径名的绝对路径、文件长度、最后修改时间
System.out.println("<FILE>/t" + file.getAbsolutePath() + "/t"
+ sdf.format(new Date(file.lastModified())));
else {
System.out.println("/t" + file.getAbsolutePath());
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++)
getInfo(files[i]);
}
}

public static void renameFile(String filepath, String token, String value) {
renameFile(new File(filepath), token, value);
}

public static void renameFile(File file, String token, String value) {

int count = 0;
String oldName = file.getName();
if (oldName.indexOf(token) == -1)
return;
count++;
String oldPath = file.getAbsolutePath();
String newName = file.getName().replaceAll(token, value);
String newPath = oldPath.replaceAll(oldName, newName);
File newfile = new File(newPath);
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
file.renameTo(newfile);
// log.debug((new StringBuilder()).append("the return value of rename
// operation is ").append().toString());
}

public static void renameAll(String dirpath, String token, String value) {
File path = new File(dirpath);
List dirs = new ArrayList();
dirs.add(path);
File files[] = null;
for (; dirs.size() != 0; dirs.remove(0)) {
File dir = dirs.get(0) != null ? (File) dirs.get(0) : null;
files = dir.listFiles();
for (int i = 0; i < files.length; i++)
if (files[i].isDirectory())
dirs.add(files[i]);
else
renameFile(files[i], token, value);

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: