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()

     字符串大小写转换

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