Java

Table of Contents

1 Ant

Typically you still need to set the class path.

export CLASSPATH=".:/usr/local/lib/antlr-4.5-complete.jar:$CLASSPATH"

2 Eclipse

You can set the key binding to emacs style by: preference, general, key

You can export ant build.xml file by: project, export, general, ant build file. Un-check the "eclipse" mark, because we want an independent script.

3 List & ArrayList

List is the interface. There are many implementation, including ArrayList and LinkedList. ArrayList can be accessed by index expression.

4 String

  • charAt
  • compareTo
  • endsWith
  • getBytes
  • indexOf
  • isEmpty
  • lastIndexOf
  • length
  • matches(regex)
  • replace(old, new)
  • replaceAll
  • split(regex)
  • startsWith
  • substring
  • trim

5 File System

5.1 Files

Predicates:

  • Files.exists(path)
  • Files.notExists(path)
  • Files.isRegularFile(file)
  • Files.isReadable(file)
  • Files.isExecutable(file)
  • Files.isDirectory(Path, LinkOption)
  • Files.isSymbolicLink(Path)

Operations:

  • Files.delete(path)
  • Files.deleteIfExists(Path)
  • Files.copy(source, target, REPLACE_EXISTING)
  • Files.move(source, target, REPLACE_EXISTING)

6 IO

6.1 byte stream

  • InputStream
  • OutputStream
  • FileInputStream
  • FileOutputStream
FileInputStream in = null;
FileOutputStream out = null;
in = new FileInputStream("xanadu.txt");
out = new FileOutputStream("outagain.txt");
int c;
// c is used for last 8 bits
while ((c = in.read()) != -1) {
    out.write(c);
}

6.2 character stream

  • Reader
  • Writer
  • FileReader
  • FileWriter
FileReader inputStream = null;
FileWriter outputStream = null;
inputStream = new FileReader("xanadu.txt");
outputStream = new FileWriter("characteroutput.txt");

int c;
// use the last 16 bits of int
while ((c = inputStream.read()) != -1) {
    outputStream.write(c);
}

6.3 line-oriented IO

  • BufferedReader
  • PrintWriter
BufferedReader inputStream = null;
PrintWriter outputStream = null;
inputStream = new BufferedReader(new FileReader("xanadu.txt"));
outputStream = new PrintWriter(new FileWriter("characteroutput.txt"));

String l;
while ((l = inputStream.readLine()) != null) {
  outputStream.println(l);
}

6.4 Scanning

java.util.Scanner

Scanner s = null;
s = new Scanner(new BufferedReader(new FileReader("xanadu.txt")));
while (s.hasNext()) {
  System.out.println(s.next());
}

7 Misc

7.1 Static import

Import the public static field in the way that, can be used as a top level field, without the enclosing class.

Author: Hebi Li

Created: 2017-11-20 Mon 10:50

Validate