《Thinking in Java》源码导入Eclipse报错解决方法
来源:互联网 发布:自动读取数据 电子称 编辑:程序博客网 时间:2024/05/11 06:03
这段时间在看《Thinking in Java》,下载了他的源码,但是导入Eclipse却报错,发现很多源文件是缺少了package语句。所以写了一个Demo解决此问题。
运行代码时会弹出文件选择框,需要选择你的《Thinking in Java》的源代码的根目录。代码如下
package sample;import java.awt.Container;import java.awt.FlowLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.*;import java.util.ArrayList;import java.util.Arrays;import java.util.List;import javax.swing.JButton;import javax.swing.JFileChooser;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.SwingUtilities;import javax.swing.UIManager;import javax.swing.UnsupportedLookAndFeelException;class Filter implements FileFilter{public boolean accept(File file){if(file.getName().endsWith(".java")||file.isDirectory())return true;return false;}}public class AddPackage { public static final Filter FILTER=new Filter(); public File getRootFile(){ File file=null; try{ UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); JFileChooser jfc=new JFileChooser(); jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); int result=jfc.showOpenDialog(null); if(result==JFileChooser.APPROVE_OPTION){ file=jfc.getSelectedFile(); System.out.println(file.getName()); } }catch(Exception e){ e.printStackTrace(); System.out.println(e.getMessage()); } return file; } public void start(){ File file=getRootFile(); if(file!=null){ addPackage(file,""); }else{ System.out.println("the file is null"); } } public void addPackage(File file,String path){ if(file.isFile()){ add(file,path); return; }else{ File files[]=file.listFiles(FILTER); // System.out.println("files.size="+files.length); for(File temp:files){ if(temp.isFile()){ addPackage(temp,path); }else{ addPackage(temp,path+"."+temp.getName()); } } } } public boolean add(File file,String path){ if(path.equals("")) return false; if(path.startsWith(".")){ path=path.substring(1, path.length()); } RandomAccessFile raf; try{ raf=new RandomAccessFile(file,"rw"); if(isContainPackage(raf)==false){ writePackage(raf,path); return true; } raf.close(); }catch(Exception e){ e.printStackTrace(); System.out.println(e.getMessage()); } return false;// never reach here } public void writePackage(RandomAccessFile raf,String target) { try{ target="package "+target+";\n"; raf.seek(0); int length=(int)raf.length(); int size=200;//一次读入字节数 byte []array; int count=length/size; //System.out.println("the file length="+length); //System.out.println("count="+count); List<byte[]> list=new ArrayList<byte[]>(); for(int i=0;i<count+1;i++){ array=new byte[size]; raf.read(array); list.add(array); // System.out.println(new String(array)); } raf.seek(0); raf.write(target.getBytes()); // System.out.println("write"); int listSize=list.size(); for(int i=0;i<listSize-1;i++){ raf.write(list.get(i)); } String lastLine=new String(list.get(listSize-1)); // System.out.println("the lastLine is"+lastLine+"end"); raf.write(lastLine.trim().getBytes()); /* for(byte[] temp:list){ System.out.println("toString:"+Arrays.toString(temp)+"end"); raf.write(temp); }*/ // System.out.println(list.size()); // System.out.println("write after"); raf.close(); }catch(Exception e){ e.printStackTrace(); System.out.println(e.getMessage()); } } public boolean isContainPackage(File file) throws FileNotFoundException{ return isContainPackage(new RandomAccessFile(file,"r")); } public boolean isContainPackage(RandomAccessFile raf){ try{ raf.seek(0); String s=""; while((s=raf.readLine())!=null){ // System.out.println("read: "+s); if(s.startsWith("package")){ return true; } } }catch(Exception e){ e.printStackTrace(); System.out.println(e.getMessage()); } return false; } public static void main(String args[]) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException{ AddPackage app=new AddPackage(); app.start(); System.out.println("exit"); } }
0 0
- 《Thinking in Java》源码导入Eclipse报错解决方法
- 使用Eclipse运行Thinking in Java 3rd 例子源码
- 使用Eclipse运行Thinking in Java 4rd 例子源码
- 使用Eclipse运行Thinking in Java 4rd例子源码
- Eclipse报错no opencv_java249 in java.library.path解决方法
- Think in java 源码如何导入 eclipse
- ofbiz导入eclipse报错解决方法
- eclipse导入maven项目报错解决方法
- Android studio导入eclipse项目报错Cause: error in opening zip file解决方法
- Thinking in Java之HashMap源码分析
- zt:mina源码经maven2处理后,导入eclipse有Unbound classpath variable: 'M2_REPO报错的解决方法
- zt:mina源码经maven2处理后,导入eclipse有Unbound classpath variable: 'M2_REPO报错的解决方法
- eclipse 导入 java项目报错
- Eclipse导入java项目@Override报错
- eclipse 导入 java项目报错
- Eclipse for j2e 报错no opencv_* in java.library.path的解决方法
- eclipse 导入Play工程的demo,报错及解决方法!
- eclipse导入Android项目报错的解决方法
- 学习笔记-java线程
- Docker时代来了,你准备好了吗
- VirtualBox安装虚拟机Unable to boot – please use a kernel ...
- POJ 2823 Sliding Window
- 配置服务和开关
- 《Thinking in Java》源码导入Eclipse报错解决方法
- Six Degrees of Separation
- HDU 4390 Number Sequence(组合+容斥)
- 粒子特效
- Python菜鸟晋级01----Python3.x和Python2.x的区别
- Unity粒子系统,探照灯效果,相机跟随
- Base64编码原理
- iOS性能优化系列
- zip与流+JAVA7+Files