首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

Java编程知识点小结[1]

2012-12-22 
Java编程知识点总结[1]1. 用java程序判断一个.java文件是否能编译通过.com.sun.tools.javac.Main javacne

Java编程知识点总结[1]
1. 用java程序判断一个.java文件是否能编译通过.

com.sun.tools.javac.Main javac  =  new com.sun.tools.javac.Main(); String[] args   =  new String[] {"-d","D:/", "fileName" };int   compileStatus   =   javac.compile(args); 
注释:param1:用编译的文件的目录,"-d"表示当前目录.param2:生成的编译文件的目录,默认生成my/test/fileName.class的目录结构.param3:文件名称.   在Sun的JDK1.2及后续版本中,JDK安装路径的/lib路径下包含了一个tools.jar文件,这个类库包含了一个完整的编译器包。com.sun.tools.javac.Main是编译器的主类入口,如果已经熟悉了javac编译器命令行的使用方法,很容易理解这个类的使用方法。方法 compile(String[]   p)执行编译动作,参数p是一个String数组,用来存放javac命令的参数
选项,编译后的状态返回一个Int值,其对应值参考如下表所示:
  表                 状态参数与对应值
    EXIT_OK              0
  EXIT_ERROR         1
  EXIT_CMDERR      2
  EXIT_SYSERR        3
    EXIT_ABNORMAL  4

2. dos命令编译和运行java类
a. 使用java程序执行某些windows命令 Runtime rt= Runtime.getRuntime(); Process p=null; rt.exec("notepad.exe"); //运行记事本程序 rt.exec("C:\\Program Files\\iexplore.exe"); //运行一个exe程序 rt.exec("D://test.bat"); //bat内容:javac D:\StringAdd.java rt.exec("shutdown -r"); //重启 rt.exec("shutdown -l"); //注销 rt.exec("shutdown -s"); //注销 b. 上传一个.java文件,在指定的环境中跑junit test,返回执行结果(生成结果的文件路径) public static String checkCodeResult(String testSuiteDriver,String filePath, String username) throws Exception {com.sun.tools.javac.Main javac=new com.sun.tools.javac.Main(); String[] argstt = new String[] {"-d",ServletActionContext.getServletContext().getRealPath("") + File.separator + "docs"+ File.separator, filePath};int compileStatus = javac.compile(argstt); // 判断是否编译通过if(compileStatus==0){String jUnitPath = System.getenv("JUNITPATH");if (jUnitPath == null || jUnitPath.isEmpty() == true|| jUnitPath.equals("")) {jUnitPath = System.getenv("JAVA_HOME");}File workDir = new File(jUnitPath);Runtime rt = Runtime.getRuntime();username = username + new Date().getTime();Process proc = rt.exec(testSuiteDriver + " " + filePath + " "+ username, null, workDir); File file = new File(jUnitPath + "\\logs.txt");if (file.exists()) {file.delete();}InputStream stdin = proc.getInputStream();InputStreamReader isr = new InputStreamReader(stdin);BufferedReader br = new BufferedReader(isr);FileOutputStream fout = new FileOutputStream(file);DataOutputStream out = new DataOutputStream(fout);StringBuffer logsBuffer = new StringBuffer();String line = null;while ((line = br.readLine()) != null) {out.writeBytes(line + "\r\n");logsBuffer.append(line + "\r\n");}out.close();fout.close();return jUnitPath + "\" + username + "_result.txt";}return "";} 注意事项: 1. 环境的配置 (JUnitPath:D:\dev\kit\jdk\lib; classpath:.;%JAVA_HOME%\lib;%JAVA_HOME%\lib\tools.jar;%JAVA_HOME%\lib\junit-4.8.1.jar) 2. 执行的文件testStringAdd.cmd 3. 上传的StringAdd.java文件 4. 参数的作用、执行命令的环境jdk/lib/ 5. 相关资料(附件或mark_qi@163.com中)

4. 创建和删除文件夹(folder)
  
 1. 新建、删除folder       File myFilePath = new File(filePath);         if (!myFilePath.exists())       {           myFilePath.mkdir();          myFilePath .delete();         }     2. 如果folder里有子文件,运用递归方法来删除欲删除的文件夹下的所有子文件夹以及文件。       public void removeFile(String path) {             this.removeFile(new File(path));          }          public void removeFile(File path) {             if (path.isDirectory())           {                File[] child = path.listFiles();                if (child != null && child.length != 0)             {                    for (int i = 0; i < child.length; i++)                  {                       removeFile(child[i]);                       child[i].delete();                    }                }             }             path.delete();          }  


5 struts2上传单个文件
  Jsp:  <form name="coursewarefrm" action="courseware!add.ptm" method="post" enctype ="multipart/form-data" >             <s:file name ="myFile" id="coursefile" label ="定制课件包" />           <ul ) + "/" +           imageFileName);     // 获取服务器路径并新建 一个新的file          copy(myFile, imageFile);  // 把上传的文件以数据流的形式,复制给新的file          try {                 // 读取文件,可以执行service操作          if(excelData == null)          {        excelData = new ArrayList<TemplateMetaData>();          }          excelData = ExcelParse.readHSFWorkbook(imageFile.getAbsolutePath());          templateProjectService.saveTemplete(excelData);  } catch (IOException e) {e.printStackTrace();  }         catch (MultipleResultException e) {e.printStackTrace();  }         return SUCCESS;       } 

热点排行