Next, the previous article will introduce to you the common methods of Java applications and input and output for your reference. The specific content is as follows
1. Application
1. Use StringBuilder or StringBuffer
// 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();}2. Generate random integers in a range
Random rand = new Random(); // Between 1 and 6, includingint diceRoll() { return rand.nextInt(6) + 1;} 3. Use Iterator.remove()
void filter(List<String> list) { for (Iterator<String> iter = list.iterator(); iter.hasNext(); ) { String item = iter.next(); if (...) iter.remove(); }}The remove() method acts on the recently returned entry of the next() method. Each entry can only use the remove() method once.
4. Return string
String reverse(String s) { return new StringBuilder(s).reverse().toString();} This method should probably be added to the Java standard library.
5. Start a thread <br />The following three examples use different methods to accomplish the same thing.
How to implement Runnnable:
void startAThread0() { new Thread(new MyRunnable()).start();} class MyRunnable implements Runnable { public void run() { ... }}How to inherit Thread:
void startAThread1() { new MyThread().start();} class MyThread extends Thread { public void run() { ... }}How to inherit Thread anonymously:
void startAThread2() { new Thread() { public void run() { ... } }.start();}Do not call the run() method directly. The Thread.start() method is always called, which creates a new thread and causes the newly created thread to call run().
6. Use try-finally
I/O stream example:
void writeStuff() throws IOException { OutputStream out = new FileOutputStream(...); try { out.write(...); } finally { out.close(); }}Lock example:
void doWithLock(Lock lock) { lock.acquire(); try { ... } finally { lock.release(); }}2. Input/Output
1. Read byte data from the input stream
InputStream in = (...);try { while (true) { int b = in.read(); if (b == -1) break; (... process b ...) }} finally { in.close();}The read() method either returns the next number of bytes read from the stream (0 to 255, including 0 and 255), or returns -1 when the end of the stream is reached.
2. Read block data from the input stream
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();}Remember that the read() method does not necessarily fill the entire buf, so you have to consider the length of the return in the processing logic.
3. Read text from the file
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();}4. Write text to the file
PrintWriter out = new PrintWriter( new OutputStreamWriter(new FileOutputStream(...), "UTF-8"));try { out.print("Hello "); out.print(42); out.println("world!");} finally { out.close();}The above is all about this article, I hope it will be helpful to everyone's learning.