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;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang.StringUtils;
/**
* 规整java文件开头的package路径
*
* @author zhaigx
* @date 2013-7-12
*/
public class ConvertPkgPath4j {
public static List<String> fileList = new ArrayList<String>();
public static void main(String[] args) {
test2();
}
private static void test2() {
List arrayList = ConvertPkgPath4j.getListFiles(
"I:/workspace70/study_test/src/Java实例精通", "java", true);
process(arrayList);
}
private static void process(List arrayList) {
System.out.println(arrayList);
int size = arrayList.size();
String pkgpath = "";
for (int i = 0; i < size; i++) {
String file = (String) arrayList.get(i);
pkgpath = createPkgPath(file);
String cont = read(new File(file));
cont = cont.replaceAll("package .*\n", pkgpath);
write(cont, new File(file));
}
}
/**
* 根据文件路径创建package字符串
*
* @param file
* @return
* @author zhaigx
* @date 2013-7-12
*/
private static String createPkgPath(String file) {
String fileName = StringUtils.substringAfterLast(file, "\\");
String pkg = StringUtils.replace(file, "\\", ".");
pkg = pkg.substring(pkg.indexOf("src") + 4);
pkg = "package " + StringUtils.remove(pkg, "." + fileName) + ";\n";// java开头的包路径
return pkg;
}
/**
*
* @param path
* 文件路径
* @param suffix
* 后缀名
* @param isdepth
* 是否遍历子目录
* @return
*/
public static List getListFiles(String path, String suffix, boolean isdepth) {
File file = new File(path);
return ConvertPkgPath4j.listFile(file, suffix, isdepth);
}
public static List listFile(File f, String suffix, boolean isdepth) {
// 是目录,同时需要遍历子目录
if (f.isDirectory() && isdepth == true) {
File[] t = f.listFiles();
for (int i = 0; i < t.length; i++) {
listFile(t[i], suffix, isdepth);
}
} else {
String filePath = f.getAbsolutePath();
if (suffix != null) {
int begIndex = filePath.lastIndexOf(".");// 最后一个.(即后缀名前面的.)的索引
String tempsuffix = "";
if (begIndex != -1)// 防止是文件但却没有后缀名结束的文件
{
tempsuffix = filePath.substring(begIndex + 1,
filePath.length());
}
if (tempsuffix.equals(suffix)) {
fileList.add(filePath);
}
} else {
// 后缀名为null则为所有文件
fileList.add(filePath);
}
}
return fileList;
}
public static String read(File src) {
StringBuffer res = new StringBuffer();
String line = null;
try {
BufferedReader reader = new BufferedReader(new FileReader(src));
while ((line = reader.readLine()) != null) {
res.append(line + "\n");
}
reader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return res.toString();
}
public static boolean write(String cont, File dist) {
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(dist));
writer.write(cont);
writer.flush();
writer.close();
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
}
将java文件开头的package路径进行规整,使包路径正确
最新推荐文章于 2023-10-01 09:16:34 发布