CORE API / NOTES</>↗Core API27 Apr 2010
Below is an example that can be used to get the extension of a file. The code below assume that the extension is the last part of the file name after the last dot symbol. For instance if you have a file named data.txt the extension will be txt, but if you have a file […]
REGEX / NOTES</>↗Regex25 Apr 2010
The example below demonstrate the Matcher.lookingAt() method to check if a string starts with a pattern represented by the Pattern class. package org.kodejava.regex; import java.util.Locale; import java.util.Set; import java.util.TreeSet; import java.util.regex.Matcher; import java.util.regex.Pattern; public class MatcherLookingAtExample { public static void main(String[] args) { // Get the available countries Set<String> countries = new TreeSet<>(); Locale[] locales […]
CORE API / NOTES</>↗Core API24 Apr 2010
For minimizing the storage used by an ArrayList to the number of its elements we can use the ArrayList.trimToSize() method call. In the code below we create an instance of ArrayList with the initial capacity set to 100. Later we only add five data into the list. To make the capacity of the ArrayList minimized […]
CORE API / NOTES</>↗Core API22 Apr 2010
The code below show you how to get the process ID of a Java application. We can use the ManagementFactory.getRuntimeMXBean().getName() to get the process ID. In Windows the method return a string in the form of [PID]@[MACHINE_NAME]. Since JDK 10, we can use ManagementFactory.getRuntimeMXBean().getPid() method to get the process ID. This method returns the process […]
REGEX / NOTES</>↗Regex21 Apr 2010
The code below demonstrates the use Matcher.appendReplacement() and Matcher.appendTail() methods to create a program to find and replace a sub string within a string. Another solution that can be used to search and replace a string can be found on the following example: How do I create a string search and replace using regex?. package […]
AWT / NOTES</>↗AWT19 Apr 2010
The example code below shows you how to print a file using the default registered application’s print command for the corresponding file type. As an example, on Windows notepad.exe is the default application for printing a .txt file. To print a file using the default registered application we call the java.awt.Desktop.print(File) method. The print() method […]