package util;
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;
public class FileUtil {
private String endFix = ".java";
private String tempFile = "C:/tempFile.txt";
private String packageStr = "package";
private String importStr = "import";
public void changePackageAndImport(String path) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(path));
BufferedWriter bw = new BufferedWriter(new FileWriter(tempFile));
String str1 = "";
str1 = br.readLine();
while(str1!=null) {
// if(str1.startsWith(packageStr)) {
// str1 = str1.replace("package ", "package sam.");
// bw.write(str1);
// bw.newLine();
//
// if(path.contains("F:/Root/MyLife/workspace/Java官方类学习/src/java/util")) {
// bw.write("import java.util.Iterator;");
// bw.newLine();
// }
// } else if(str1.startsWith(importStr)) {
//
// str1 = str1.replace("import ", "import sam.");
// bw.write(str1);
// bw.newLine();
// } else
if(str1.contains("(java.io.ObjectOutputStream")) {
str1 = str1.replace("(java.io.ObjectOutputStream", "(sam.java.io.ObjectOutputStream");
bw.write(str1);
bw.newLine();
} else if(str1.contains("(java.io.ObjectInputStream")) {
str1 = str1.replace("(java.io.ObjectInputStream", "(sam.java.io.ObjectInputStream");
bw.write(str1);
bw.newLine();
} else if(str1.contains("throws java.io.IOException")) {
str1 = str1.replace("throws java.io.IOException", "throws sam.java.io.IOException");
bw.write(str1);
bw.newLine();
} else if(str1.contains("throw new java.io.StreamCorruptedException")) {
str1 = str1.replace("throw new java.io.StreamCorruptedException", "throw new sam.java.io.StreamCorruptedException");
bw.write(str1);
bw.newLine();
}
else {
bw.write(str1);
bw.newLine();
}
str1 = br.readLine();
}
bw.close();
br.close();
br = new BufferedReader(new FileReader(tempFile));
bw = new BufferedWriter(new FileWriter(path));
str1 = br.readLine();
while(str1!=null) {
bw.write(str1);
bw.newLine();
str1 = br.readLine();
}
bw.close();
br.close();
}
public void parsePathOrFile(String path) throws IOException {
System.out.println(path);
if(path.equals("C:/MyCitiLife/LearnPlan/JDK/java/awt/font")) {
int ss = 0;
}
File[] file = new File(path).listFiles();
if(file!=null && file.length!=0) {
for(int i = 0; i< file.length; i++) {
if(file[i].getName().endsWith(endFix)) {
changePackageAndImport(path.concat("/".concat(file[i].getName())));
} else {
parsePathOrFile(path.concat("/".concat(file[i].getName())));
}
}
}
}
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
FileUtil FileUtil = new FileUtil();
FileUtil.parsePathOrFile("F:/Root/MyLife/workspace/Java官方类学习/src");
}
}
change package and import
最新推荐文章于 2023-09-27 16:24:42 发布