接著上一篇再為大家介紹java應用和輸入輸出常用方法,供大家參考,具體內容如下
一、應用
1、使用StringBuilder或StringBuffer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
// join(["a", "b", "c"]) -> "a and b and c" String join(List<String> strs) { StringBuilder sb = new StringBuilder(); boolean first = true ; for (String s : strs) { if (first) first = false ; else sb.append( " and " ); sb.append(s); } return sb.toString(); } |
- 不要像這樣使用重復的字符串連接:s += item ,因為它的時間效率是O(n^2)。
- 使用StringBuilder或者StringBuffer時,可以使用append()方法添加文本和使用toString()方法去獲取連接起來的整個文本。
- 優(yōu)先使用StringBuilder,因為它更快。StringBuffer的所有方法都是同步的,而你通常不需要同步的方法。
2、生成一個范圍內的隨機整數
1
2
3
4
5
6
7
8
9
10
11
|
Random rand = new Random(); // Between 1 and 6, inclusive int diceRoll() { return rand.nextInt( 6 ) + 1 ; } |
- 總是使用Java API方法去生成一個整數范圍內的隨機數。
- 不要試圖去使用 Math.abs(rand.nextInt()) % n 這些不確定的用法,因為它的結果是有偏差的。此外,它的結果值有可能是負數,比如當rand.nextInt() == Integer.MIN_VALUE時就會如此。
3、使用Iterator.remove()
1
2
3
4
5
6
7
8
9
10
11
12
13
|
void filter(List<String> list) { for (Iterator<String> iter = list.iterator(); iter.hasNext(); ) { String item = iter.next(); if (...) iter.remove(); } } |
remove()方法作用在next()方法最近返回的條目上。每個條目只能使用一次remove()方法。
4、返轉字符串
1
2
3
4
5
|
String reverse(String s) { return new StringBuilder(s).reverse().toString(); } |
這個方法可能應該加入Java標準庫。
5、啟動一條線程
下面的三個例子使用了不同的方式完成了同樣的事情。
實現Runnnable的方式:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
void startAThread0() { new Thread( new MyRunnable()).start(); } class MyRunnable implements Runnable { public void run() { ... } } |
繼承Thread的方式:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
void startAThread1() { new MyThread().start(); } class MyThread extends Thread { public void run() { ... } } |
匿名繼承Thread的方式:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
void startAThread2() { new Thread() { public void run() { ... } }.start(); } |
不要直接調用run()方法。總是調用Thread.start()方法,這個方法會創(chuàng)建一條新的線程并使新建的線程調用run()。
6、使用try-finally
I/O流例子:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
void writeStuff() throws IOException { OutputStream out = new FileOutputStream(...); try { out.write(...); } finally { out.close(); } } |
鎖例子:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
void doWithLock(Lock lock) { lock.acquire(); try { ... } finally { lock.release(); } } |
- 如果try之前的語句運行失敗并且拋出異常,那么finally語句塊就不會執(zhí)行。但無論怎樣,在這個例子里不用擔心資源的釋放。
- 如果try語句塊里面的語句拋出異常,那么程序的運行就會跳到finally語句塊里執(zhí)行盡可能多的語句,然后跳出這個方法(除非這個方法還有另一個外圍的finally語句塊)。
二、輸入/輸出
1、從輸入流里讀取字節(jié)數據
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
InputStream in = (...); try { while ( true ) { int b = in.read(); if (b == - 1 ) break ; (... process b ...) } } finally { in.close(); } |
read()方法要么返回下一次從流里讀取的字節(jié)數(0到255,包括0和255),要么在達到流的末端時返回-1。
2、從輸入流里讀取塊數據
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
InputStream in = (...); try { byte [] buf = new byte [ 100 ]; while ( true ) { int n = in.read(buf); if (n == - 1 ) break ; (... process buf with offset= 0 and length=n ...) } } finally { in.close(); } |
要記住的是,read()方法不一定會填滿整個buf,所以你必須在處理邏輯中考慮返回的長度。
3、從文件里讀取文本
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
BufferedReader in = new BufferedReader( new InputStreamReader( new FileInputStream(...), "UTF-8" )); try { while ( true ) { String line = in.readLine(); if (line == null ) break ; (... process line ...) } } finally { in.close(); } |
- BufferedReader對象的創(chuàng)建顯得很冗長。這是因為Java把字節(jié)和字符當成兩個不同的概念來看待(這與C語言不同)。
- 你可以使用任何類型的InputStream來代替FileInputStream,比如socket。
- 當達到流的末端時,BufferedReader.readLine()會返回null。
- 要一次讀取一個字符,使用Reader.read()方法。
- 你可以使用其他的字符編碼而不使用UTF-8,但最好不要這樣做。
4、向文件里寫文本
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
PrintWriter out = new PrintWriter( new OutputStreamWriter( new FileOutputStream(...), "UTF-8" )); try { out.print( "Hello " ); out.print( 42 ); out.println( " world!" ); } finally { out.close(); } |
- Printwriter對象的創(chuàng)建顯得很冗長。這是因為Java把字節(jié)和字符當成兩個不同的概念來看待(這與C語言不同)。
- 就像System.out,你可以使用print()和println()打印多種類型的值。
- 你可以使用其他的字符編碼而不使用UTF-8,但最好不要這樣做。
以上就是本文的全部內容,希望對大家的學習有所幫助。