import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
功能:实现指定文件目录,将该目录和子目录下所有的格式为.java的文件转换成格式为.txt的文件
public class JavaToTxt {
public static void main(String[] args) {
File f = new File("F:/C");
tree(f, 0);
} private static void tree(File f, int level) {
if (f.isDirectory()) {
level++;
tree(f, level);
} else {
if (f.getName().matches(".*\\.java$")) {
transferTXT(f);
}
}
} private static void transferTXT(File f) {
BufferedReader br = null;
BufferedWriter bw = null;
String[] str = f.getName().split("\\.");
String fileName = str[0] + ".txt"; //这是文件名字符串,不是截取文件时的正则表达式
String directory = "F:/" + f.getParent();
File fTXT = new File(directory, fileName);
try {
br = new BufferedReader(new FileReader(f));
bw = new BufferedWriter(new FileWriter(fTXT));
String temp = br.readLine();
while (temp != null) {
bw.write(temp + "\r\n");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
br = null;
if (bw != null)
bw.close();
bw = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
功能:实现指定文件目录,将该目录和子目录下所有的格式为.java的文件转换成格式为.txt的文件
public class JavaToTxt {
public static void main(String[] args) {
File f = new File("F:/C");
tree(f, 0);
} private static void tree(File f, int level) {
if (f.isDirectory()) {
level++;
tree(f, level);
} else {
if (f.getName().matches(".*\\.java$")) {
transferTXT(f);
}
}
} private static void transferTXT(File f) {
BufferedReader br = null;
BufferedWriter bw = null;
String[] str = f.getName().split("\\.");
String fileName = str[0] + ".txt"; //这是文件名字符串,不是截取文件时的正则表达式
String directory = "F:/" + f.getParent();
File fTXT = new File(directory, fileName);
try {
br = new BufferedReader(new FileReader(f));
bw = new BufferedWriter(new FileWriter(fTXT));
String temp = br.readLine();
while (temp != null) {
bw.write(temp + "\r\n");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
br = null;
if (bw != null)
bw.close();
bw = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
话说我只自己测试的时候弄了个无限递归 看见过这个异常 解决办法的话 就不要用递归操作 改用for 而且平时也不建议用递归的 效率太低了 希望有帮助
import java.io.File;
import java.io.IOException;
import java.util.Collection;
import org.apache.commons.io.DirectoryWalker;
import org.apache.commons.io.FilenameUtils; /**
* 批量重命名文件的一个demo,依赖于apache的commons.io组件
* @author ioe_gaoyong
*
*/
@SuppressWarnings("rawtypes")
public class FileRenamer extends DirectoryWalker {
private String extension = null;//extension表示文件的后缀
private String newExtension=null;//newExtension表示新文件的后缀
public FileRenamer(String extension,String newExtension) {
super();
this.extension = extension;
this.newExtension=newExtension;
}
/** 启动对某个文件夹的筛选 */
@SuppressWarnings("unchecked")
public void startRenameFiles(File rootDir) throws IOException {
walk(rootDir, null);
}
protected void handleFile(File file, int depth, Collection results) throws IOException {
if (extension == null
|| extension.equalsIgnoreCase(FilenameUtils.getExtension(file.toString()))) {
//调用具体业务逻辑
rename(file);
}
}
/** 重命名文件 */
private void rename(File file) throws IOException {
String path=file.getAbsolutePath();
path=path.substring(0, path.length()-extension.length());
String renameFilePath=path+newExtension;
file.renameTo(new File(renameFilePath));
} public static void main(String[] args) throws IOException {
//例子:指定文件夹下(含子文件夹)所有.java重命名为.txt
new FileRenamer("java","txt").startRenameFiles(new File("e:/test"));
}
}程序运行依赖的jar在这个网站可以下载http://commons.apache.org/io/