zoukankan      html  css  js  c++  java
  • JAVA Useful Program(1)

    public static void main(String[] args){
          //字符串有整型的相互转换
             String str=String.valueOf(123);
             int i=Integer.parseInt(str);
             System.out.println(i);
             //向文件末尾添加内容
             BufferedWriter out = null;
             try{
              out = new BufferedWriter(new FileWriter("c://logs//log.out", true));
              out.write("aString");
             }catch(IOException ex){
              System.out.println(ex.getMessage());
             }finally{
              if(out!=null){
               try {
         out.close();
        } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
        }
              }
             }
             //得到当前方法的名字
             String methodName = Thread.currentThread().getStackTrace()[1].getMethodName();
             System.out.println(methodName);
            
             //转字符串到日期
             SimpleDateFormat format = new SimpleDateFormat( "dd.MM.yyyy" );
             try {
       Date date = format.parse("01.01.2013");
       System.out.println(date);
      } catch (ParseException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      }
      
      //把 Java util.Date 转成 sql.Date
      java.util.Date utilDate = new java.util.Date();
      java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
      System.out.println(sqlDate);
      
      //使用NIO进行快速的文件拷贝
      File f1=new File("c://logs//log.out");
      File f2=new File("c://logs//log1.out");
      FileChannel inChannel=null;
      FileChannel outChannel=null;
      try {
       inChannel = new FileInputStream(f1).getChannel();
          outChannel = new FileOutputStream(f2).getChannel();
       try{
        int maxCount = (64 * 1024 * 1024) - (32 * 1024);
        long size = inChannel.size();
        long position = 0;
        while ( position < size ){
         position += inChannel.transferTo( position, maxCount, outChannel );
         
        }
       }catch(FileNotFoundException ex){
        ex.printStackTrace();
       } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
       }
      } catch (FileNotFoundException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      }finally{
       if(inChannel!=null){
        try {
         inChannel.close();
        } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
        }
       }
       if(outChannel!=null){
        try {
         outChannel.close();
        } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
        }
       }
      }
      
         }

    Output:

    123
    main
    Tue Jan 01 00:00:00 CST 2013
    2013-06-17

  • 相关阅读:
    修改Chrome设置伪装成手机M站
    使用Chrome浏览器自动将文件下载到指定路径
    操作HTML5存储对象
    在HTML5的画布元素上进行绘画操作
    测试HTML5语言实现的视频播放器
    封装操作表格的公用类
    测试过程中发生异常或断言失败时进行屏幕截图
    浏览器中新开标签页(Tab)
    高亮显示正在操作的页面元素
    精确比较页面截图图片
  • 原文地址:https://www.cnblogs.com/snake-hand/p/3141029.html
Copyright © 2011-2022 走看看