java 第一篇 常用、有用方法

 

1、對文件重命名的方法

File file = new File(“D:/abc.tx”);

file.renameTo(new File("D:/efg.txt"));

 

2、String 的一些常用方法:

(1)String[] s = str.split("\n");             將字符串 str 用 "\n"分隔,得到一個字符串數組

(2)常用轉義符:\t   Tab鍵         

       \n  將當前位置移到下一行開頭

       \r   回車

(3)截取字符串"adfa[dfdf]fad"中[和]之間的部分:

String str = "adfa[dfdf]fad";

int  a = str.indexOf("[");

int  b = str.indexOf("]");

String result = str.subString(a+1,b);

 

3、執行windows命令行和執行linux命令:

(1)String[] cmd = new String[] { "cmd.exe", "/C", "java -jar D:/root/statistics/eps-qa-analysis-0.0.1-SNAPSHOT.jar auto "+planId };  // windows命令

(2)String[] cmd = new String[] { "/bin/sh", "-c", "java -jar /root/statistics/eps-qa-analysis-0.0.1-SNAPSHOT.jar auto "+planId }; // linux命令

調用:Process process = Runtime.getRuntime().exec(cmd);

 

4、getMethod() 和 getDeclaredMethod() 

A.class.getMethod() 和 A.class.getDeclaredMethod() 這兩個方法用於反射時獲取類的方法,getMethod() 包含父類的方法;getDeclaredMethod() 不包含父類的方法。

 

5、StringBuffer 和 StringBuilder

     (1)StringBuffer 和 StringBuilder 都相當於可擴展的 String(String不可擴展),StringBuffer是線程安全的,速度較慢,適合在多線程使用;

     (2)StringBuilder不是線程安全的,速度較快,不適合在多線程使用;

 

6、List<Integer> list = new ArrayList<Integer>();

      Collections.addAll(list ,2,5,6,4,6);

     Collections.addAll(); 方法可以一次將多條數據加入集合中。

 

7、string.toLowCase() 和 string.toUpperCase()

     字符串大小寫轉換

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章