欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java 对文件夹下指定后缀名的读写操作(运用递归的案例)

程序员文章站 2022-06-08 22:06:20
...
package test;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileFilter;
import java.io.FileWriter;
import java.io.FilenameFilter;
import java.io.IOException;

/***
* 文件的读写操作
*
* @ClassName: FileReadOrWrite
* @Description:
* @author:JH020
* @date 2014年2月21日 上午9:51:41
* @version V1.0
*/
public class FileReadOrWrite {

/**
* 文件的主程序入口
*
* @param args
*/
public static void main(String[] args) {
// String readFilePath = "D://CEDD//HWWTest";
String readFilePath = "Z://";
File writerFile = new File("D://CEDD//lcsd.txt");
if (!writerFile.exists()) {
try {
writerFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
FileWriter fw;
try {
fw = new FileWriter(writerFile);
FileReadOrWrite.listFile(readFilePath, fw);
} catch (IOException e) {
e.printStackTrace();
}

}

/**
* 调用两个读写的方法
*
* @param name
* @param fw
*/
public static void listFile(String name, FileWriter fw) {
try {
FileReadOrWrite.list_html(name, fw);
FileReadOrWrite.list_htm(name, fw);
fw.flush();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* 列出读文件夹目录下所有以 .html 结尾的文件
*
* @param name
* @param fw
*/
public static void list_html(String name, FileWriter fw) {
try {
File f = new File(name);
if (!f.exists()) {
System.out.println("文件不存在哦");
return;
}

if (f.isDirectory()) {
File[] list = f.listFiles(getFileExtensionFilter(".html"));
for (int i = 0; i < list.length; i++) {
String paths = "<a href=\"" + list[i].toString() + "\">"
+ list[i].toString() + "</a><br>";
String writePaths1 = paths.replace("Z:",
"http://localhost/lcsd");
String writePaths2 = writePaths1.replace("\\", "/");
fw.write(writePaths2);
System.out.println(writePaths2);
BufferedWriter bfw = new BufferedWriter(fw);
bfw.newLine();// 换行
}
list = f.listFiles(getNotDirFileFilter());
for (int i = 0; i < list.length; i++) {
if (list[i].isDirectory())
list_html(list[i].toString(), fw);
}
}
} catch (Exception e) {
System.out.println("IO error!/r/n" + e.toString());
}

}

/**
* 列出读文件夹目录下所有以 .htm 结尾的文件
*
* @param name
* @param fw
*/
public static void list_htm(String name, FileWriter fw) {
try {
File f = new File(name);
if (!f.exists()) {
System.out.println("文件不存在哦");
return;
}

if (f.isDirectory()) {
File[] list2 = f.listFiles(getFileExtensionFilter(".htm"));
for (int i = 0; i < list2.length; i++) {
String paths = "<a href=\"" + list2[i].toString() + "\">"
+ list2[i].toString() + "</a><br>";
String writePaths1 = paths.replace("Z:",
"http://localhost/lcsd");
String writePaths2 = writePaths1.replace("\\", "/");
fw.write(writePaths2);
System.out.println(writePaths2);
BufferedWriter bfw = new BufferedWriter(fw);
bfw.newLine();// 换行
}
list2 = f.listFiles(getNotDirFileFilter());
for (int i = 0; i < list2.length; i++)
if (list2[i].isDirectory())
list_htm(list2[i].toString(), fw);
}
} catch (Exception e) {
System.out.println("IO error!/r/n" + e.toString());
}
}

/***
* 指定扩展名过滤
*
* @param extension
* @return
*/
public static FilenameFilter getFileExtensionFilter(String extension) {
final String _extension = extension;
return new FilenameFilter() {
public boolean accept(File file, String name) {
boolean ret = name.endsWith(_extension);
return ret;
}
};
}

/**
* 如果是文件夹( 文件还是目录过滤)
*
* @return
*/
public static FileFilter getNotDirFileFilter() {

return new FileFilter() {
public boolean accept(File file) {
return file.isDirectory();// 关键判断点
}
};

}

}