Basic grammars of java for beginners
Running Environment
# run with encoding
java -Dfile.encoding=UTF-8 … com.x.Main
java -Dfile.encoding=EUC-KR … com.x.Main
#########################
# run.sh
#########################
for i in ../lib/*.jar; do
CLASSPATH="$CLASSPATH":"$i"
done
# Not using package
java -classpath $CLASSPATH:. TestSender
# Using package (class or jar)
java -Xdiag -classpath $CLASSPATH:./test.jar:. com.test.TestSender -f/app/inf/java/cfg/test.json -l/app/inf/java/lib
# Using charset
# java -Dsun.jnu.encoding=UTF-8 -Dfile.encoding=UTF-8 ...
- -Dfile.encoding=UTF-8 : change the default character set to UTF-8
https://www.programmersought.com/article/95251457972/
System.out.println("file.encoding:"+System.getProperty("file.encoding"));
System.out.println("sun.jnu.encoding:"+System.getProperty("sun.jnu.encoding"));
sun.jnu.encoding refers to the default encoding of the operating system, file.encoding refers to the encoding of JAVA files (remember, not class files, all class files are encoded in UTF-8), so, in the same The JAVA application running on the operating system has the same sun.jnu.encoding, and the file.encoding can be encoded differently even in the same JAVA application.
Basic grammar
Expressions
// switch/case
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
// for
String[] temp = { "aa", "bb", "cc" };
for (int i = 0; i < temp .length; i++) {
System.out.println(temp[i]);
}
// foreach
String[] temp = { "aa", "bb", "cc" };
for (String el : temp) {
System.out.println(el);
}
Split, Tokenize
spaces are used to separate words
- java.util.StringTokenizer : Not used these days
- java.util.Scanner
- String[] String.split
String phrase = "the music made it hard to concentrate";
String delims = "[ ]+";
String[] tokens = phrase.split(delims);
for (int i = 0; i < tokens.length; i++)
System.out.println(tokens[i]);
Java Exception
try
{
parseTask.execute(result); // Now we start the task to parse the xml
} catch(Exception ex) {
Log.e("Exception Message: ", ex.getMessage());
}
URL functions
toString() : Full URL string
getProtocol() : Protocol(scheme) information such as http
getHost() : Host information
getPort() : Port
getPath() : Path (Starts with '/')
getQuery() : Parameter information
getFile() : path + query
getAuthority() : host + port
getRef()
File exist
// if the file does not exist, it occurs FileNotFoundException...
try {
File file = new File("filename");
if (file.exist()) {
} else {
// when ???
}
} catch (FileNotFoundException e) {
// File Not Found
} catch (Exception e) {
}
Delete File
try {
File file = new File(C.MenuFilePath, C.MenuFileName);
if(file.exists())
{
boolean result = file.delete();
Log.i(TAG, "File is deleted " + C.MenuFilePath + "/" + C.MenuFileName + " " + result);
finish();
}
else
{
Log.e(TAG, "Failed to delete file " + C.MenuFilePath + "/" + C.MenuFileName);
}
} catch (Exception e) {
Log.e(TAG, "ERROR:" + e.getMessage());
}
Get hostname
String hname = Runtime.getRuntime().exec("hostname");
// This can be failed. it depends on /etc/hosts
InetAddress.getLocalHost().getHostName();
Remove file path
String fname = "/tmp/test.txt";
int idx = fname.lastIndexOf(File.separator);
String _fileName = fname.substring(idx + 1); // Remove path
Run external programs
// mv example
Process process = new ProcessBuilder("mv", filename, targetfile).start();
/**
* Gets a string representing the pid of this program - Java VM
*/
public static String getPid() throws IOException, InterruptedException {
Vector<String> commands = new Vector<String>();
commands.add("/bin/bash");
commands.add("-c");
commands.add("echo $PPID");
ProcessBuilder pb = new ProcessBuilder(commands);
Process pr = pb.start();
pr.waitFor();
if (pr.exitValue() == 0) {
BufferedReader outReader = new BufferedReader(new InputStreamReader(pr.getInputStream()));
return outReader.readLine().trim();
} else {
System.out.println("Error while getting PID");
return "";
}
}
Leave a comment