Learn to read file content into string in java. Examples use Files.readAllBytes, Files.lines and FileReader & BufferedReader to read file content.
Learn to create enum using strings, iterate over all enum constants, getting single enum value and doing reverse lookup to find enum by string value. Create enum of string constants Example of creating enum with strings. This enum contains deployment environments and their respective URLs. Iterate all enum constants To iterate over all enums, use […]
To convert comma separated string to list in java, you can use below methods. Comma-separated String to List Using Regex You can use regular expression “\\s*,\\s*” to match comma into CSV list and then use String.split() method. Output: [how, to, do, in, java] This resulting List is a fixed-size List backed by the array. It […]
You may need custom serialization in java in may cases. For example, you have legacy java classes which you are not willing to modify for any reason. There can be some design constraints as well. Or even simply, the class is expected to be changed in future releases which could break the deserialization of previously […]
Learn to convert XMLGregorianCalendar to Date class or string value. Also learn to apply timezone changes and daylight saving effects as well.
In this example, we will learn to format XMLGregorianCalendar class to string in multiple patterns e.g. ‘MM/dd/yyyy hh:mm a z’. XMLGregorianCalendar to String in Date/TimeStamp Pattern In below code sample, I am creating instance of XMLGregorianCalendar with current Date object. Then I am formatting it to String format in given pattern. Output: 01/27/2017 01:32 PM […]
To iterate list in java is very basic operation, but over the years it’s gone through some significant changes. We will go through all these changes in given examples.
In this java example, we will learn to convert OutputStream to InputStream which you may need when you read data from one source which return an outputstream; and write/pass the data to other target which wants data in inputstream.
In java, java.util.Locale class represents a specific language and region of the world. If a class varies its behavior according to Locale, it is said to be locale-sensitive. Learn more.
Example of java code to unzip and extract files and sub-directories from a compressed zip file using java.util.zip package.
XMLEncoder class is used to serialize or encode a java object into XML file, while XMLDecoder is used to deserialize the xml file back to java object. Learn to use them in detail.
Knowing the difference between Externalizable vs Serializable is important in two aspects, one – if could be asked as an interview question, two – you can use the knowledge to make better informed decision for performance improvement for applying serialization into your application.
You can achieve more efficient serialization by implementing Externalizable interface and overriding it’s methods writeExternal() and readExternal().
Use given syntax examples for setting CLASSPATH for any java application runtime, in windows and linux environments.
This tutorial cover few examples of reading a file from resources folder in any java project. Table of Contents Project Structure Read file using ClassLoader.getResource().toURI() Read file using Spring’s ResourceUtils.getFile() Project Structure Below image describe the folder structure used in this example. Read file using ClassLoader.getResource().toURI() In this example, we will use the ClassLoader reference […]
A file in java can be read by N number of ways. Find below some good ways to read a file in java. Table of Contents Read file with BufferedReader Read file with try-with-resources Read file with java.nio.file.Files Read file with Apache Commons IOUtils Read file with Guava Read file with BufferedReader A most simple, […]
BlockingQueue is excellent when you want to skip the complexity involved in wait–notify statements. This BlockingQueue can be used to solve the producer-consumer problem as well as given blow example. As this problem is well known to every programmer, I am not going in detail of problem description. How BlockingQueue fit into Solution Any effective […]
Below examples uses different techniques to iterate over java collections. Use what’s suitable and easy for you in any situation. Iterable.forEach method (Java 8) Recently introduced in java 8, This method can be called on any Iterable and takes one argument implementing the functional interface java.util.function.Consumer. e.g. Java “foreach” loop (Java 5) The “foreach” loop […]
A java thread can be in any of following thread states during it’s life cycle i.e. New, Runnable, Blocked, Waiting, Timed Waiting or Terminated. These are also called life cycle events of a thread in java. Let’s understand each state in more detail. Java Thread Life Cycle States New As soon as, you create new […]
Concurrency means multiple tasks which start, run, and complete in overlapping time periods, in no specific order. Parallelism is when multiple tasks OR several part of a unique task literally run at the same time, e.g. on a multi-core processor. Remember that Concurrency and parallelism are NOT the same thing. Let’s understand more in detail […]