Java 基础知识总结 2

 

11.Java常用类:

  1. StringBuffer
    • StringBuffer 是使用缓冲区的,本身也是操作字符串的,但是与String类不同,String类的内容一旦声明之后则不可以改变,改变的只是其内存地址的指向,而StringBuffer中的内容是可以改变的,对于StringBuffer而言,本身是一个具体的操作类,所以不能像String那样采用直接赋值的方式进行对象的实例化,必须通过构造方法完成.
    • 在StringBuffer中使用append()方法,完成字符串的连接操作
    • public StringBuffer insert(int offset,String str)// 在第几个字符后面插入数据
      public StringBuffer reverse() //字符串反转操作
      public StringBuffer replace(int start,int end,String str)// 替换指定范围的内容
      public String substring(int start,int end) //字符串截取
      public StringBuffer delete(int start, int end) //移除此序列的字符串中的字符
      public int indexOf(String str)//查找指定的内容是否存在,如果存在返回位置,否则否则返回-1.
      public int length() //返回长度(字符数).
  2. Runtime
    • 运行时,是一个封装了JVM进程的类,每个JAVA程序实际上都是启动了一个JVM进程,那么每个JVM进程都是对应这个Runtime实例,此实例是由JVM为其实例化的.本类的定义中没有构造方法,构造方法被私有化了,在此类中肯定有一个方法可以返回本类的实例化对象.
    • public static Runtime getRuntime() //取得Runtime类的实例(如:Runtime run=Runtime.getRuntime();)
      public long freeMemory() //返回Java虚拟机中的空闲内存量
      public long maxMemory() //返回JVM的最大内存量
      public void gc() //运行垃圾回收器,释放空间 (如: Runtime run=Runtime.getRuntime(); run.gc();)
      public Process exec(String command)throws IOException //执行本机命令(如调用本机的记事本程序: Runtime run=Runtime.getRuntime();Process p=null; p=run.exec(“notepad.exe”);//需要异常处理 p.destroy();//结束进程)
  3. 国际化程序
    • 国际化的操作就是指一个程序可以同时适应多门语言
    • 想要实现Java程序的国际化操作必须通过以下的三个类完成
      • 1)java.util.Locale:用于表示一个国家语言类
      • 2)java.util.ResourceBundle:用于访问资源文件
      • 3)java.textMessageFormat:格式化资源文件的占位字符串
  4. 日期操作类
    • Date类:直接实例化Date对象:Date 名称=new Date();
      • Calendar类:可以按照自己需要的格式显示的时间,则就可以使用Calendar类
      • Calendar类是一个抽象类,既然是一个抽象类则肯定无法直接使用,此时就要利用对象多态性的概念.通过向上的转型关系实例化本类对象.
      • Calendar类取得一个完整的日期:
      • Calendar calendar=new GregorianCalendar();
        calendar.get(Calendar.YEAR) ; //取得年份
        calendar.get(Calendar.MONTH) ; //取得月份,注意:此处的月份比实际的少一个月,要打印时要+1;
        calendar.get(Calendar.DAY_OF_MONTH) ; //取得日期calendar.get(Calendar.HOUR_OF_DAY) ; //取得小时calendar.get(Calendar.MINUTE) ; //取得分
        calendar.get(Calendar.SECOND) ; //取得秒
        calendar.get(Calendar.MILLISECOND) ; //取得毫秒
    • DateFormat类:此类是一个日期的格式化类,专门格式化日期的操作.此类是定义在java.text包中的.
    • public static final DateFormat getDateInstance() //得到日期的DateFormat对象
      public static final DateFormat getDateTimeInstance() //得到日期时间的DateFormat对象
      public final String format(Date date) //直接使用DateFormat类完成Date类的转换功能
    • SimpleDateFormat类:
      • (一)此类 功能是完成日期的显示格式化的
        • 1)y 表示年, 年份是四位数字,所以需要使用”yyyy”表示年.
        • 2)M 表示月, 月份是两位数字,所以需要使用”MM”表示月.
        • 3)d 表示日, 日是两位数字,所以需要使用”dd”表示日.
        • 4)H 表示时, 时是两位数字,所以需要使用”HH”表示时.
        • 5)m 表示分, 分是两位数字,所以需要使用”mm”表示分.
        • 6)s 表示秒, 秒是两位数字,所以需要使用”ss”表示秒.
        • 7)S 表示毫秒,毫秒是三位数字,所以需要使用”SSS”表示毫秒.
      • (二)在SimpleDateFormat类使用的时候,必须注意的是在构造对象时要传人匹配的模板
        • 1)构造方法:publicSimpleDateFormat(String pattern)
        • 2)转换: public Date parse(String source) throws ParseException 此时取得的是全部的时间数.
        • 3)格式化: public final String format(Date date) 将时间重新格式化成字符串显示.
      • (三)具体的一个示例:
      • String strDate = "2008-10-19 10:11:30.345" ;// 准备第一个模板,从字符串中提取出日期数字
        String pat1 = "yyyy-MM-dd HH:mm:ss.SSS" ;// 准备第二个模板,将提取后的日期数字变为指定的格式
        String pat2 = "yyyy年MM月dd日 HH时mm分ss秒SSS毫秒" ;
        SimpleDateFormat sdf1 = new SimpleDateFormat(pat1) ;    // 实例化模板对象
        SimpleDateFormat sdf2 = new SimpleDateFormat(pat2) ;    // 实例化模板对象
        Date d = null ;
        try{
            d = sdf1.parse(strDate) ;    // 将给定的字符串中的日期提取出来
        }catch(Exception e){    // 如果提供的字符串格式有错误,则进行异常处理
            e.printStackTrace() ;    // 打印异常信息
        }
        System.out.println(sdf2.format(d)) ;    // 将日期变为新的格式
      • (四)得到系统时间:
      • SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS") ;
        System.out.println("系统日期:"+sdf.format(new Date()) );
      • (五)得到中文时间:
      • SimpleDateFormat sdf= new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒SSS毫秒") ;
        System.out.println("中文日期:"+sdf.format(new Date()) );
      • (六)得到时间戳:
      • SimpleDateFormat sdf= new SimpleDateFormat("yyyyMMddHHmmssSSS") ;
        System.out.println("时间戳:"+sdf.format(new Date()) );
  5. Math与Random类
    • Math类
      • Math.abs(-1);//求-1的绝对值
        Math.cbrt(8);//求2的立方根
        Math.max(1,5);//求两个数的最大值
        Math.min(2,4);//求两个数的最小值
        Math.sqrt(9);//求9的平方根
        Math.pow(2,3);//求2的3次方
        Math.round(2.6);//2.6的四舍五入(这个四舍五入是将小数点后面的全忽略掉了,要想准确的四舍五入则使用BigDecimal类完成)
    • Random类:Random类的主要功能是产生随机数,可以产生一个指定范围的随机数.
      • public int nextInt(int n);//产生一个0到n之间的一个整数
  6. NumberFormat类
    • 表示数字的格式化类.
    • 定义格式: public abstract class NumberFormat extends Format
    • 方法:
    1. public static Locale[] getAVailableLocales() 返回所有语言环境的数组
      public static final NumberFormat getInstance() 返回当前默认语言环境的数字格式
      public static NumberFormat getInstance(Locale inLocale) 返回指定语言环境的数字格式
      public static final NumberFormat getCurrencyInstance() 返回当前默认环境的货币格式
      public static NumberFormat getCurrencyInstance(Locale inLocale) 返回指定语言环境的数字格式
    • NumberFormat nf = null ;    // 声明一个NumberFormat对象
      nf = NumberFormat.getInstance() ;    // 得到默认的数字格式化显示
      System.out.println("格式化之后的数字:" + nf.format(10000000)) ;
      System.out.println("格式化之后的数字:" + nf.format(1000.345)) ;
    • DecimalFormat的基本使用:可以按照自己的格式格式化数字
      • (一)格式化模板:
      •  标记   位置   描述
        1) 0   数字 //每个0表示一位,如果该位不存在则显示0 2) #   数字 //每个#表示一位,如果该位不存在则不显示 3) .   数字 //小数点分隔符或货币的小数分隔符 4) -   数字 //代表负号 5) ,   数字 //分组分隔符 6) E   数字 //分隔科学计数法中的尾数和指数 7) ;   子模式边界 //分隔正数和负数子模式 8) %   前缀或后缀 //数字乘以100并显示为百分数 9) \u2030   前缀或后缀 //数字乘以1000兵显示为千分数 10) ¤ (\u00A4)   前缀或后缀 //货币记号,由货币符号替换。如果两个同时出现,则用国际货币符号替换。如果出现在某个模式中,则使用货币小数分隔符,而不使用小数分隔符。 11) ‘   前缀或后缀 //用于在前缀或或后缀中为特殊字符加引号,例如 "'#'#" 将 123 格式化为 "#123"。要创建单引号本身,请连续使用两个单引号:"# o''clock"。
      • (二)示例:
      • DecimalFormat df = null ;
        df = new DecimalFormat("###,###.###") ;
        String str = df.format(111222.34567) ;
        System.out.println(str) ;
        df = new DecimalFormat("000,000.000") ;
        String str = df.format(111222.34567) ;
        System.out.println(str) ;
        df = new DecimalFormat("###,###.###¥") ;
        String str = df.format(111222.34567) ;
        System.out.println(str) ;
        df = new DecimalFormat("000,000.000¥") ;
        String str = df.format(111222.34567) ;
        System.out.println(str) ;
        df = new DecimalFormat("##.###%") ;
        String str = df.format(0.345678) ;
        System.out.println(str) ;
        df = new DecimalFormat("00.###%") ;
        String str = df.format(0.345678) ;
        System.out.println(str) ;
        df = new DecimalFormat("##.###%") ;
        String str = df.format(0.0345678) ;
        System.out.println(str) ;
        df = new DecimalFormat("###.###\u2030") ;
        String str = df.format(0.345678) ;
        System.out.println(str) ;
  7. 大操作数(BigIntger,BigDecimal)
    • 操作整型:BigInteger
    • public BigInteger(String val) //将一个字符串变形为BigInteger类型的数据
      public BigInteger add(BigInteger val) //加法
      public BigInteger subtract(BigInteger val)// 减法
      public BigInteger muItiply(BigInteger val) //乘法
      public BigInteger divide(BigInteger val) //除法
      public BigInteger max(BigInteger val) //返回两个大数字中的最大值
      public BigInteger min(BigInteger val) //返回两个大数字中的最小值
      public BigInteger[] divideAndRemainder(BigInteger val)// 除法操作,数组的第一个元素为除法的商,第二个为元素为除法的余数
    • 示例:
    • BigInteger bi1 = new BigInteger("123456789") ;// 声明BigInteger对象
      BigInteger bi2 = new BigInteger("987654321") ;// 声明BigInteger对象
      System.out.println("加法操作:" + bi2.add(bi1)) ;    // 加法操作
      System.out.println("减法操作:" + bi2.subtract(bi1)) ;    // 减法操作
      System.out.println("乘法操作:" + bi2.multiply(bi1)) ;    // 乘法操作
      System.out.println("除法操作:" + bi2.divide(bi1)) ;    // 除法操作
      System.out.println("最大数:" + bi2.max(bi1)) ;    // 求出最大数
      System.out.println("最小数:" + bi2.min(bi1)) ;    // 求出最小数
      BigInteger result[] = bi2.divideAndRemainder(bi1) ;// 求出余数的除法操作
      System.out.println("商是:" + result[0] + ";余数是:" + result[1]) ;
    • 操作小数:BigDecimal
    • public BigDecimal(double val)// 将double表示形式转换为BigDecimal
      public BigDecimal(int val) //将int表示形式转换为BigDecimal
      public BigDecimal(String val) //将字符串表示形式转换为BigDecimal
      public BigDecimal add(BigDecimal augend)// 加法
      public BigDecimal subtract(BigDecimal Subtrahend) //减法
      public BigDecimal multiply(BigDecimal multiplicand) //乘法
      public BigDecimal divide(BigDecimal divisor) //除法
  8. Arrays数组类:Arrays表示数组的操作类,是直接定义在java.util包中的.
  9. pubic static boolean equals(int[] a,int[] a2) //判断两个数组是否相等,此方法被重载多次,可判断各种数据类型的数组
    public static void fill(int[] a,int val)// 将指定内容填充到数组之中,此方法被重载多次,可以填充各种数据类型的数组
    public static void sort(int[] a)// 数组排序,此方法被重载多次,可以对各种类型的数组进行排序
    public static int binarySearch(int[] a,int key) //对排序后的数组进行检索,此方法被重新多次,可以对各种类型数组进行搜索
    public static String toString(int[] a) //输出数组信息,此方法被重载多次,可以输出各种数据类型的数组.
  10. 比较器
    • Comparable接口的作用
      • 定义: 
        • public interface Comparable<T>{
            public int compareTo(T o);
          }

           

      • 此方法返回一个int类型的数据,但是此int的值只能是一下三种:
        • 1: 表示大于
        • -1:表示小于
        • 0:表示相等
    • Comparator
    • 定义格式: 
      public interface Comparator<T>{
          public int compare(T o1,T o2);
          boolean equals(Object obj);
      }
  11. 正则表达式
    • 正则表达式可以方便的对数据进行匹配,可以执行更加复杂的字符串验证,拆分,替换功能.
    • Pattern:主要作用是进行正则规范(如”[0-9]”就属于正则规范)的编写,在Pattern类中如果要想取得Pattern类实例,则必须调用compile()方法
    • public static Pattern compile(String regex) //指定正则表达式规则
      public Matcher matcher(CharSequence input)// 返回Matcher类实例
      public String[] split(CharSequence input) //字符串拆分
    • Matcher:主要是执行规范,验证一个字符串是否符合其规范.
    • public boolean matches() //执行验证
      public String replaceAll(String replacement //字符串替换
    • 示例:
      • (一) 匹配
      • String str = "1983-07-27" ;    // 指定好一个日期格式的字符串
        String pat = "\\d{4}-\\d{2}-\\d{2}" ;    // 指定好正则表达式
        Pattern p = Pattern.compile(pat) ;    // 实例化Pattern类
        Matcher m = p.matcher(str) ;    // 实例化Matcher类
        if(m.matches()){    // 进行验证的匹配,使用正则
            System.out.println("日期格式合法!") ;
        }else{
            System.out.println("日期格式不合法!") ;
        }
      • (二) 拆分
      • String str = "A1B22C333D4444E55555F" ;    // 指定好一个字符串
        String pat = "\\d+" ;    // 指定好正则表达式
        Pattern p = Pattern.compile(pat) ;    // 实例化Pattern类
        String s[] = p.split(str) ;    // 执行拆分操作
        for(int x=0;x<s.length;x++){
            System.out.print(s[x] + "\t") ;
        }
      • (三) 拆分替换
      • String str = "A1B22C333D4444E55555F" ;    // 指定好一个字符串
        String pat = "\\d+" ;    // 指定好正则表达式
        Pattern p = Pattern.compile(pat) ;    // 实例化Pattern类
        Matcher m = p.matcher(str) ;    // 实例化Matcher类的对象
        String newString = m.replaceAll("_") ;
        System.out.println(newString) ;
      • (四) 拆分替换验证
      • String str1 = "A1B22C333D4444E55555F".replaceAll("\\d+","_") ;
        boolean temp = "1983-07-27".matches("\\d{4}-\\d{2}-\\d{2}") ;
        String s[] = "A1B22C333D4444E55555F".split("\\d+") ;
        System.out.println("字符串替换操作:" + str1) ;
        System.out.println("字符串验证:" + temp) ;
        System.out.print("字符串的拆分:") ;
        for(int x=0;x<s.length;x++){
            System.out.print(s[x] + "\t") ;
        }
      • (五) 拆分
      • String info = "LXH:98|MLDN:90|LI:100" ;    // 定义一个字符串
        // 拆分的形式:
        /*
        LXH    -->    98
        MLDN    -->    90
        LI    -->    100
        */
        String s[] = info.split("\\|") ;
        System.out.println("字符串的拆分:") ;
        for(int x=0;x<s.length;x++){
        String s2[] = s[x].split(":") ;
        System.out.println(s2[0] + "\t" + s2[1]) ;
        }

         

12.定时调度

  1. 定时调度:每当一端时间,程序会自动执行,称为定时调度.必须保证程序始终运行着才可以,也就是说是想当于定时调度是在程序之外又启动了一个新的线程.
  2. Timer:是一种线程设施,可以用来实现在某一个时间或某一端时间后,安排某一个任务执行一次,或定期重复执行.该功能要与TimerTask配合使用,TimerTask类用来实现由Timer安排的一次或重复执行的某一个任务.每个Timer对象对应的是一个线程,因为次计时器所执行的任务应该迅速完成,否则可能会延迟后续任务的执行,而这些后续的任务就有可能堆在一起,等到该任务完成后才能快速连续执行.
  3. public Timer() //用来创建一个计时器并启动该计时器
    public void cancel() //用来终止该计时器,并放弃所有已安排的任务.对当前正在执行的任务没有影响
    public int purge() //将所有已经取消的任务移除,一般用来释放内存空间
    public void schedule(TimerTask task,Date time) //安排一个任务在指定的时间执行,如果已经超过该时间,则立即执行.
    public void schedule(TimerTask task,Date firstTime,long period) //安排一个任务在指定的时间执行,之后以固定的频率(单位:毫秒)重复执行.
    public void schedule(TimerTask task,long delay) //安排一个任务在一端时间(单位:毫秒) 后执行.
    public void schedule(TimerTask task,long delay,long period) //安排一个任务在一段时间(单位:毫秒)后执行,之后以固定的频率(单位:毫秒)重复执行
    public void scheduleAtFixedRate(TimerTask task,Date firstTime,long period) //安排一个任务在指定的时间执行,之后以近似固定的频率(单位:毫秒)重复执行
    public void scheduleAtFixedRate(TimerTask task,long delay,long period) //安排一个任务在一段时间(单位:毫秒)后执行,之后以近似固定的频率(单位:毫秒)重复执行.
  4. schedule()scheduleAtFixedRate()方法的区别:
    • 两者的区别在与重复执行任务时,对于时间间隔出现延迟的情况处理:
      • schedule() 方法的执行时间间隔永远是固定的,如果之前出现了延迟的情况,之后也会继续按照设定好的时间间隔时间来执行
      • scheduleAtFixedRate()方法可以根据出现的延迟时间自动调整下一次间隔的执行时间.
  5. TimerTask:要想执行具体的任务,则必须使用TimerTask类. TimerTask类是一个抽象类.如果要使用该类,需要自己建立一个类来继承此类,并实现其中的抽象方法.
  6. public void cancel() //用来终止此任务,如果该任务只执行一次且还没有执行,则永远不会再执行,如果为重复执行任务,则之后不会再执行(如果任务正在执行,则执行完成不会再执行).
    public void run() //该任务所要执行的具体操作,该方法为引入的接口Runnable中的方法,子类需要覆写此方法.
    public long scheduleExecutionTime() //返回最近一次要执行该任务的时间(如果正在执行,则返回此任务的执行安排时间),一般在run()方法中调用,用来判断当前是否有足够的时间来执行完成该任务.
  7. 示例:
  8. import java.util.TimerTask ;
    import java.util.Date ;
    import java.text.SimpleDateFormat ;
    class MyTask extends TimerTask{    // 任务调度类都要继承TimerTask
        public void run(){
            SimpleDateFormat sdf = null ;
            sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS") ;
            System.out.println("当前系统时间为:" + sdf.format(new Date())) ;
        }
    };
    import java.util.Timer ;
    public class TestTask{
    public static void main(String args[]){
        Timer t = new Timer() ;    // 建立Timer类对象
        MyTask mytask = new MyTask() ;    // 定义任务
        t.schedule(mytask,1000,2000) ;    // 设置任务的执行,1秒后开始,每2秒重复
        }
    };

     

 

posted @ 2017-04-13 13:44  全栈九九六  阅读(1019)  评论(1编辑  收藏  举报