Posts

Showing posts from August, 2018

Useful File java.io Mini API

useful-file-java-io-mini-api Java io is a very extensive library. There are numerous ways and combinations to write/read data on a file. This article summaries some very useful classes in java.io package. Here is a table from OCP Java SE 8 Programmer II Exam Guide (Exam 1Z0-809) that summarizes the basic classes in file io. java.io Class Extends From Key Constructor(s) Key Methods File Object File, String String String, String createNewFile() delete() exists() isDirectory() isFile() list() mkdir() renameTo() FileWriter Writer File String close() flush() write() BufferedWriter Writer Writer close() flush() newLine() write() PrintWriter Writer File String OuputStream Writer close() flush() format() printf() print() println() write() FileOutputStream OutputStream File String close() write() FileReader Reader File String read() BufferedReader Reader Reader read() readLine() FileInputStream InputStream F

Load and Store Properties File

load-and-store-properties-file Properties files are usually used to externalize configuration from applications to text files. This article introduces the syntax and usage of properties files. java.util.Properties class allows developers to load, change, store properties easily while providing a way to optionally save it in a filesystem. Syntax Rules Since a property file is just a text file and read as an input stream, it could have any name and any extension. it could have two styles of commenting: ! comments... or # comments... (space after the initial character is optional). A backslash could be used to break up a single line into multiple lines. Properties files contain key-value pairs. Key-value pair could be defined as key=value , key:value , key value . space around the value is ignored when reading the file. Usage System Properties import java . util . Properties ; class SysProps { public static void main ( String . . . args ) {

Locale and Resource Bundles

locale-and-resource-bundles Resource bundles allow you to move locale-specific information out from your main source code to a properties file or a java class. In this article, we will introduce the usage of both implementations of the ResourceBundle interface – PropertyResourceBundle and ListResourceBundle class. Using a Properties File PropertyResourceBundle takes an InputStream , so you could point it to a properties file for where the locale-specific information is. A locale properties file must have the extension of .properties , and optionally suffixed with _language or _language_COUNTRY . e.g. Labels_zh.properties , Labels_zh_CN.properties , Foo_fr_CA.properties , Foo_de.properties , Foo.properties are all legal locale properties files. The ResourceBundle always searches from the most specific properties first and will stop search when there is a match. A locale properties file just looks like a normal properties file, e.g. Labels_en_US.properties

Java 8 Time Package and Locale

java-8-time-package-and-locale Java 8 provides an extensive time computational package - java.time that is recommended for any new code to use because it provides so many helper methods to compute time and easily used with java.util.Locale class to support localization (l10n) and internationalization (i18n) . In this article, we will learn some most useful operations using the java.time package with java.util.Locale class through a scenario of Beijing Winter Olympic Game opening day reminder. Something to know: China only use one timezone which is standardized as China Standard Time (CST). Shanghai and Beijing thus use the same time. import java . time . * ; import java . time . format . * ; import java . time . temporal . * ; import java . util . Locale ; public class WinterOlympicGames { public static void main ( String . . . args ) { LocalDate nowDate = LocalDate . now ( ) ; LocalTime nowTime = LocalTime . now