Posts

Showing posts from August, 2018

Useful File java.io Mini API

useful-file-java-io-mini-apiJava 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 ClassExtends FromKey Constructor(s)Key MethodsFileObjectFile, String
String
String, StringcreateNewFile()
delete()
exists()
isDirectory()
isFile()
list()
mkdir()
renameTo()FileWriterWriterFile
Stringclose()
flush()
write()BufferedWriterWriterWriterclose()
flush()
newLine()
write()PrintWriterWriterFile
String
OuputStream
Writerclose()
flush()
format()
printf()
print()
println()
write()FileOutputStreamOutputStreamFile
Stringclose()
write()FileReaderReaderFile
Stringread()BufferedReaderReaderReaderread()
readLine()FileInputStreamInputStreamFile
Stringread()
close()Examplesimport java.io.*;classWriter1{publicstaticvoidmain(String... args){try{boolean newFile =false; File file =newFile("…

Load and Store Properties File

load-and-store-properties-fileProperties 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 RulesSince 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.UsageSystem Propertiesimport java.util.Properties;classSysProps{publicstaticvoidmain(String... args){ Properties sysProps = System.getProperties(); sysProps.setProperty("…

Locale and Resource Bundles

locale-and-resource-bundlesResource 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 FilePropertyResourceBundle 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.propertiesgreetings=How you doing? while Labels_en_UK…

Java 8 Time Package and Locale

java-8-time-package-and-localeJava 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;publicclassWinterOlympicGames{publicstaticvoidmain(String... args){ LocalDate nowDate = LocalDate.now(); LocalTime nowTime = LocalTime.now(); LocalDateTime nowDateTime = LocalDateTime.of(nowDate, nowTime); System.out.println("…