学习Java有一段时间了 。电脑上上有一本叫做《thinking in Java》的大作。作为Java基础书籍,这本书讲的还是有些深入的。之前我草率的读了一遍。现在我准备好好研究它了。
学习编程最快的的方式就是拿到源码,然后 run it ,看看结果,不懂的地方再打印调试。于是我从网上找到了 《thinking in Java 》的源码。
我用惯了eclipse,所以下载到源码后我准备把源码导入到ide中。
我准备通过mport功能将源码 导入整个工程(菜单->file->import—>general->)。我通过eclipse找到到源码路径,然后在选择之前常用的 existing project into workplace 选项,但是eclipse提示说当前目录没有工程存在。然后我发现了一个以前没用过的功能,就是file System 选项。
通过 file System导入源码后,我发现 eclipse报了很多错误,大多数是因为都是源码没有package 申明(这不是 thinking in Java 源码的错误,这份源码本来就是设计成可以脱离ide 编译的,不过,为了自己学习方便,导入到ide是不错的选择)。
开始的时候,我手工的去添加package申明,很快,我就发现这不是一个程序员应该做的事情。我应该写一段代码来完成这样重复的工作。我只需要将文件读出来,如果有package申明我就略过,如果没有,我就写入。
我新建了一个叫做 Recover 的类,代码虽然完成了任务,作为初学者,还是有很多写的不好的地方。
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import javax.sound.sampled.Line;
public class Recover {
/**
* @param args
*/
public static void main(String[] args) {
String filePath = "E:\\java_work_place\\thinkingInJava\\thinkingInJava\\src";
String subToPackageName = "E:\\java_work_place\\thinkingInJava\\thinkingInJava\\src\\";
getFile(filePath);
}
public static void getFile(String filePath) {
File file = new File(filePath);
File[] fileList = file.listFiles();
for (int i = 0; i < fileList.length; i++) {
if (fileList[i].isFile()) {
if (fileList[i].getName().endsWith("java")) {
boolean isChange = isChange(fileList[i].getPath());
if(isChange){
System.out.println("this file may change filename:--"+fileList[i].getPath());
change(fileList[i].getPath());
}
}
} else if (fileList[i].isDirectory()) {
getFile(fileList[i].getPath());
}
}
}
public static boolean isChange(String filePath) {
boolean change = true;
File file = new File(filePath);
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String tempString = null;
int line = 1;
// 一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
// 显示行号
System.out.println("tempString : " + tempString);
if (tempString.startsWith("package") == true ) {
change = false;
System.out.println("has packageName--"+line);
}
if(tempString.startsWith("public") || tempString.startsWith("class") || tempString.startsWith("interface") || tempString.startsWith("abstract")){
System.out.println("linnumber --end ---"+line);
break;
}
line++;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
return change;
}
public static void change(String filePath ){
File file = new File(filePath);
System.out.println("fileName----"+filePath);
String packageName = filePath.replace("E:\\java_work_place\\thinkingInJava\\thinkingInJava\\src\\", "");
packageName = packageName.replace("\\", ".");
System.out.println("packagename---"+packageName);
String fileNameWithoutPath = filePath.replace(file.getParent()+"\\", "");
System.out.println("fileNameWithoutPath----"+fileNameWithoutPath);
packageName = packageName.replace("."+fileNameWithoutPath, "");
System.out.println("package name----"+packageName);
String codeSrc = readFileToString(filePath);
System.out.println("code----"+codeSrc);
outPutPackageName(packageName, codeSrc, filePath);
}
public static String readFileToString(String filePath){
StringBuffer sb = new StringBuffer();
File file = new File(filePath);
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String tempString = null;
int line = 1;
// 一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
// 显示行号
sb.append(tempString);
sb.append(System.getProperty("line.separator"));
line++;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
return sb.toString();
}
public static void outPutPackageName(String packageName , String codeSrc , String filePath){
FileWriter fw = null ;
BufferedWriter bw = null;
StringBuffer sb = new StringBuffer("package");
sb.append(" ");
sb.append(packageName);
sb.append(";");
System.out.println("append str---"+sb.toString());
try {
fw = new FileWriter(filePath);
bw = new BufferedWriter(fw);
bw.write(sb.toString());
bw.newLine();
bw.write(codeSrc);
bw.flush();
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(fw!=null){
try {
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(bw!=null){
try {
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
代码只有一百多行,我花了一个多小时调试完成。现在工程中每一个类都有了package申明。不过还是有些代码报错了。报错原因是因为import了一类库,而这些类库我工程中还没有。