java打包exe技术初试

1.近期在研究java打包为exe可执行文件,举个实用的例子,快速生成指定位置,指定大小的文件。分享编辑源码如下:

importjava.io.*;

importjava.util.Date;;

 

publicclass test{

   public test(){

   }

   public static void main(String[] args){

            System.out.println("***********************************************************");

            System.out.println("1.本版本仅支持文件数值为长整形");

            System.out.println("2.非整形文件大小建议使用单位:KB");

            System.out.println("3.防止文件名相同,命名方式:testFlie_系统时间");

            System.out.println("4.本版本仅供自己娱乐使用");

           System.out.println("***********************************************************");

            BufferedReader br=newBufferedReader(new InputStreamReader(System.in));

            String line=null;

            //定义文件单位

            int unit=0;

            //定义文件大小

            long lager=0;

       //默认定义C

            Stringpartintion="C";

            while(true){

       //获取系统时间

            Date date=new Date();

            long time =date.getTime();

            System.out.println("请输入要生成文件位置,默认C;DD盘,以此类推");

                  try{

                           //选择指定文件位置

                           line=br.readLine();

                           partintion=line;

                           if(partintion.length()!=1){

                                    System.out.println("文件位置错误");

                                    continue;}

                           } catch (IOException ex) {

           ex.printStackTrace();

           System.out.println("文件位置错误");

           continue;}

                     System.out.println("请输入要生成文件单位,1KB,2MB,3GB");

                     try{

                              //选择指定文件单位

                              line=br.readLine();

                     } catch (IOException ex) {

               ex.printStackTrace();

               System.out.println("文件单位错误");

               continue;

           }           

                     try{

                              unit=Integer.parseInt(line);

                     }catch(NumberFormatException e2){

                              System.out.println("文件单位错误!");     

                              continue;

                     }

                     System.out.println("请输入生成文件大小(如文件大小=大小*单位=20*kb");

                     try{

                              //选择指定文件大小

                              line=br.readLine();

                     } catch (IOException e1) {

               e1.printStackTrace();

               System.out.println("选择指定文件大小,获取异常");  

               continue;

           }

                     try{

                              //获取文件大小

                              lager=Integer.parseInt(line);

                     }catch(NumberFormatException e2){

                              System.out.println("文件大小,获取异常");       

                              continue;

                     }

                     //判断文件单位后,获取大小

                     if(unit==1){

                              lager=lager*1024;

                     }

                     else if(unit==2){

                              lager=lager*1024*1024;

                     }       

                     else if(unit==3){

                              lager=lager*1024*1024*1024;

                     }else{

                              System.out.println("文件单位赋值错误!");

                              continue;

                     }

           try {

           //写入文件大小

           long lo = System.currentTimeMillis();

           java.io.RandomAccessFile raf = newjava.io.RandomAccessFile(partintion+":/testFile_"+time+".txt","rw");

           raf.setLength(lager);

           System.out.println("生成文件所消耗时间:"+ (System.currentTimeMillis() - lo));

           System.out.println("任务完成!文件生成成功!");

           System.out.println("***********************************************************");

           raf.close();         

       } catch (FileNotFoundException ex) {

           ex.printStackTrace();

       } catch (IOException ex) {

           ex.printStackTrace();

       }

       }

   }

}

2.项目右键Export该文件打包为jar文件,保存到自己已知位置

java打包exe技术初试_第1张图片

3.实用打包工具j2ewiz.exe将自己的jar文件,转化为exe可执行文件(打包工具.rar),生产可以用的工具(生成指定大小文件工具.exe

java打包exe技术初试_第2张图片



你可能感兴趣的:(java打包exe技术初试)