现在的位置: 首页 > 综合 > 正文

最近在开发中遇到的问题总结

2012年12月12日 ⁄ 综合 ⁄ 共 3763字 ⁄ 字号 评论关闭

1.查看eclipse源代码方法:

a.创建plugin项目,将所有插件加入依赖,然后在该项目中open type(ctrl + shift + T),可以查到所需要的类。

b.File->import->Plug-in Development->Plug-ins and Fragments 然后可以add所有需要的plugin到工作空间中。

 

2.java判断路径是否存在:

由选择路径的对话框选择路径,由于文本框可写,所以无法保证路径是否合法(即在本地是否存在这个路径),简单的判断方法:

 

       public String getDirectory(){

       File f = new File(fDirectoryText.getText());

       if(f.exists())

           return fDirectoryText.getText();

       else

           return null;

        } 

 

3.java判断文件名是否合法(不含*<>?//等字符):

由保存文件的对话框选择路径,由于文本框可写,所以无法保证文件名是否合法可以通过创建该文件的返回值判断:

代码:

    //create the file based on the Directory and Name
    File f = new File(getDirectory());
       boolean canCreate;
       try {
            canCreate = f.createNewFile();
       } catch (IOException e) {
           canCreate = false;
           e.printStackTrace();
       }
       //If the file can be created
       if(canCreate){
           f.delete();
           return getDirectory();
       }
       //If the file can’t be created
       else
           return null;

 

4.在windows下怎样通过查找功能(F3)查找java代码中的内容

直接查找时 .java文件不被识别,可以在命令行中通过ren(rename缩写)命令将java文件改名为.txt即可。

ren *.java *.txt

 

5.在eclipse中查看实现某个接口的类?

在editor中 ctrl+T 即可

 

6.try catch的奇怪问题

try {
offset = document.getLineOffset(startLine-1);
       } catch (BadLocationException e1) {
                  e1.printStackTrace();
       }

一个普通的try catch,但是try报错:Unhandled exception type BadLocationExceptioncatch报错:Unreachable catch block for BadLocationException. This exception is never thrown from the try statement body问题是导入了错误的包,致使BadLocationException不是需要的BadLocationException,想起初学java时出现的cannot cast form Statement to Statement.的错误,相同的原因。

 

English version:

 

1.the way to view the eclipse source code:

a.create new plugin project and add all plugins to the dependencies,then we can get the class form the open type(ctrl + shift + T).

b.File->import->Plug-in Development->Plug-ins and Fragments then add the plugin project needed to the workspace.

2.the way to judge a directory is exist:

The typical solution:

public String getDirectory(){
              File f = new File(fDirectoryText.getText());
              if(f.exists())
                     return fDirectoryText.getText();
              else
                     return null;
}

 

3. How to ensure an file name is available(not include the char:*<>?//and so on)?

We can create new File and determine if the name is available base on its return. The typical code:             //create the file based on the Directory and Name

             File f = new File(getDirectory());
             boolean canCreate;           
              try {
                      canCreate = f.createNewFile();
              } catch (IOException e) {
                     canCreate = false;
                     e.printStackTrace();
              }
              //If the file can be created
              if(canCreate){
                     f.delete();
                     return getDirectory();
              }
              //If the file can’t be created
              else
                     return null;

4. How to search contents of the java file in Windows?

In the Windows OS ,press F3 to search the content of specified file ,the .java file is not identified. We can use the follow command in the command line:(ren is the short of rename)

ren *.java *.txt

change the .java file to the .txt file ,then we can search the content we want.

5. How to view the class implement specified interface in eclipse:

Press ctrl+T in eclipse editor

6. An strange problem:

try {
offset = document.getLineOffset(startLine-1);
       } catch (BadLocationException e1) {
                  e1.printStackTrace();
       }

An normal try-catch block,but the try statement tells you the BadLocationException not catch (Unhandled exception type BadLocationException),and the catch statement tells you try block not throws this Exception,the catch block is not arrived(Unreachable catch block forBadLocationException. This exception is never thrown from the try statement body)! How strange!The problem is that the BadLocationException is not the BadLocationException needed,because the import statement imported a wrong package!It is recalled me another similar wrong one year ago:The error tell me :cannot cast form Statement to Statement.The reason is the same!

 

抱歉!评论已关闭.