常用目录文件操作类
来源:互联网 发布:知乎 欧猪四国 编辑:程序博客网 时间:2024/05/29 09:06
package com.wen;
import java.io.*;
/**
* <p>Title:常用目录文件操作类 </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author explorer
* @version 1.0
*/
public class FileUtils {
public FileUtils() {
}
public static void copy(File src, File dest, boolean cover) throws
IOException {
FileInputStream fis = new FileInputStream(src);
FileOutputStream fos = new FileOutputStream(dest);
byte buf[] = new byte[1024];
int i;
try {
while ( (i = fis.read(buf)) >= 0) {
fos.write(buf, 0, i);
}
}
finally {
try {
fis.close();
}
catch (Exception exception1) {
}
try {
fos.close();
}
catch (Exception exception2) {
}
}
}
public static void copy(String src, String dest, boolean cover) throws
IOException {
File fsrc = new File(src);
File fdest = new File(dest);
copy(fsrc, fdest, cover);
}
public static void copytree(File src, File dest, boolean cover) throws
IOException {
if (src.isFile()) {
copy(src, dest, cover);
}
else {
File children[] = src.listFiles();
for (int i = 0; i < children.length; i++) {
File f = new File(dest, children[i].getName());
if (children[i].isDirectory()) {
f.mkdirs();
copytree(children[i], f, cover);
}
else {
copy(children[i], f, cover);
}
}
}
}
public static void copytree(String src, String dest, boolean cover) throws
IOException {
File fsrc = new File(src);
File fdest = new File(dest);
copytree(fsrc, fdest, cover);
}
public static void movetree(File src, File dest, boolean cover) throws
IOException {
copytree(src, dest, cover);
deltree(src);
}
public static void deltree(File f) {
File children[] = f.listFiles();
if (children != null && children.length != 0) {
for (int i = 0; i < children.length; i++) {
deltree(children[i]);
}
}
f.delete();
}
public static void deltree(String path) {
File f = new File(path);
deltree(f);
}
public static void move(String src, String dest, boolean cover) throws
IOException {
File fsrc = new File(src);
File fdest = new File(dest);
copy(fsrc, fdest, cover);
fsrc.delete();
}
public static boolean find(File root, FileFilter filter) {
if (filter.accept(root)) {
return true;
}
File children[] = root.listFiles();
if (children == null || children.length == 0) {
return false;
}
for (int i = 0; i < children.length; i++) {
if (find(children[i], filter)) {
return true;
}
}
return false;
}
public static boolean contains(File file, String suffix) {
if (!file.exists() || !file.isDirectory()) {
return false;
}
File children[] = file.listFiles();
for (int i = 0; i < children.length; i++) {
if (children[i].isFile() && children[i].getName().endsWith(suffix)) {
return true;
}
}
return false;
}
//读取单行文件内容
public static String getFline(String filePath) {
String fc = null;
try {
BufferedReader f = new BufferedReader(new FileReader(filePath));
fc = f.readLine();
f.close();
}
catch (IOException e) {
System.out.println("readLine problem, terminating.");
}
return fc;
}
//读取单行文件内容并加一写入返回
public static String getIline(String filePath) {
String fc = null;
try {
BufferedReader f = new BufferedReader(new FileReader(filePath));
fc = f.readLine();
long l = Long.parseLong(fc);
l++;
fc = String.valueOf(l);
f.close();
BufferedWriter bw = new BufferedWriter(new FileWriter(filePath));
bw.write(fc + "/n");
bw.flush();
bw.close();
}
catch (IOException e) {
System.out.println("readLine problem, terminating.");
}
return fc;
}
public static String getFileExt(String fileName) {
String ext = "";
int dot = fileName.lastIndexOf(".");
if (dot != -1) {
ext = fileName.substring(dot + 1);
}
return ext;
}
public static String getFullFileName(String filePath) {
String fileName = filePath;
int i = filePath.lastIndexOf("/");
if (i != -1) {
fileName = filePath.substring(i + 1);
}
return fileName;
}
public static String getFileName(String filePath) {
String fileName = filePath;
int i = filePath.lastIndexOf("/");
int j = filePath.lastIndexOf(".");
if (i != -1) {
fileName = filePath.substring(i + 1, j);
}
return fileName;
}
public static String getPath(String filePath) {
String path = "";
int i = filePath.lastIndexOf("/");
if (i != -1) {
path = filePath.substring(0, i);
}
return path;
}
public static boolean WriteTo(String path, String info) {
try {
File f = new File(path);
PrintWriter out = new PrintWriter(new FileWriter(f));
out.print(info);
out.close();
return true;
}
catch (IOException e) {
return false;
}
}
//追加字符串
public static boolean Append(String path, String info) {
try {
File f = new File(path);
PrintWriter out;
FileWriter theFile;
if (f.exists()) {
theFile = new FileWriter(path, true);
out = new PrintWriter(theFile);
}
else {
theFile = new FileWriter(f);
out = new PrintWriter(theFile);
}
out.print(info + "/n");
out.close();
theFile.close();
return true;
}
catch (IOException e) {
return false;
}
}
public static String getFileContent(File srcfile) {
String strReturn = "";
try {
BufferedInputStream buff = new BufferedInputStream(new FileInputStream(
srcfile));
int in = 0;
do {
in = buff.read();
if (in != -1) {
strReturn += (char) in;
}
}
while (in != -1);
return strReturn;
}
catch (Exception e) {
return strReturn;
}
}
public static String getFileContent(String fpath) {
File srcfile = new File(fpath);
return getFileContent(srcfile);
}
public static boolean isType(String ext, String ptype) {
if (ext.equals("jpg") || ext.equals("jpeg") || ext.equals("gif") ||
ext.equals("png") || ext.equals("bmp")) {
if (ptype.equals("pic")) {
return true;
}
}
else if (ext.equals("mid") || ext.equals("amr") || ext.equals("wma")) {
if (ptype.equals("ring")) {
return true;
}
}
else if (ext.equals("htm") || ext.equals("html")) {
if (ptype.equals("webpage")) {
return true;
}
}
else if (ext.equals("css")) {
if (ptype.equals("css")) {
return true;
}
}
else if (ext.equals("htt") || ext.equals("ini") || ext.equals("inc")) {
if (ptype.equals("htt")) {
return true;
}
}
else if (ext.equals("js") || ext.equals("htc")) {
if (ptype.equals("js")) {
return true;
}
}
return false;
}
}
- 常用目录文件操作类
- 文件目录常用操作的类
- 文件目录常用操作的类
- DotNET文件目录常用操作
- 文件目录常用操作[转]
- 编写一个文件目录常用操作的类
- 编写一个文件目录常用操作的类
- 编写一个文件目录常用操作的类
- 编写一个文件目录常用操作的类
- Windows 文件、目录操作编程 常用API
- Windows 文件、目录操作编程常用API
- Windows 文件、目录操作编程常用API
- python常用的文件目录操作函数
- MFC常用的字符串、文件、目录操作
- Python 常用文件及目录操作
- Linux-常用文件和目录操作命令
- Windows 文件、目录操作编程 常用API
- windows文件及目录常用操作命令
- Domain Model 探索
- 对String的操作
- DirectCast和CType还是有点区别
- String操作,单例模式实现
- 一个工具类方法
- 常用目录文件操作类
- 数据库底层连接
- [Forward]Engineer Notebook: An Extreme Programming Episode
- 用于处理画面翻页的类
- 一个Numbers类
- DataGrid资料
- 统计在线人数
- 一个分页组件
- 一个获得随机数的类