понедельник, 26 сентября 2016 г.

Java – Read a file from resources folder

 this tutorial, we will show you how to read a file from a resources folder, in both Java and Unit Test environment. In simple, put files in a resources folder, and read the file with following code snippets :
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("file/test.xml").getFile());

1. Project Directory

Review a Maven project Structure.

2. Classic Example
Example to read a file “test.txt” from a resources folder.
This is line 1
This is line 2
This is line 3
This is line 4
This is line 5
package com.mkyong;

import java.util.Scanner;

public class Hello {

  public static void main(String[] args) {
 Hello obj = new Hello();

  private String getFile(String fileName) {

 StringBuilder result = new StringBuilder("");

 //Get file from resources folder
 ClassLoader classLoader = getClass().getClassLoader();
 File file = new File(classLoader.getResource(fileName).getFile());

 try (Scanner scanner = new Scanner(file)) {

  while (scanner.hasNextLine()) {
   String line = scanner.nextLine();


 } catch (IOException e) {

 return result.toString();


This is line 1
This is line 2
This is line 3
This is line 4
This is line 5

суббота, 24 сентября 2016 г.

9 Things about Null in Java

Java and null are uniquely bonded. There is hardly a Java programmer, who is not troubled by null pointer exception, it is the most infamous fact about. Even inventor of null concept has called it his billion dollar mistake, then why Java kept it? Null was there from long time and I believe Java designer knows that null creates more problem than it solves, but still they went with it. It surprise me even more because Java's design philosophy was to simplify things, that's why they didn't bothered with pointers, operator overloading and multiple inheritance of implementation, they why null. Well I really don't know the answer of that question, what I know is that, doesn't matter how much null is criticized by Java developers and open source community, we have to live with that. Instead of ruing about null it's better to learn more about it and make sure we use it correct. Why you should learn about null in Java? because If you don't pay attention to null, Java will make sure that you will suffer from dreaded java.lang.NullPointerException and you will learn your lesson hard way. Robust programming is an art and your team, customer and user will appreciate that. In my experience, one of the main reasons of NullPointerException are not enough knowledge about null in Java. Many of you already familiar with null but for those, who are not, can learn some old and new things about null keyword. Let's revisit or learn some important things about null in Java.

What is Null in Java

As I said, null is very very important concept in Java. It was originally invented to denote absence of something e.g. absence of user, a resource or anything, but over the year it has troubled Java programmer a lot with nasty null pointer exception. In this tutorial, we will learn basic facts about null keyword in Java and explore some techniques to minimize null checks and how to avoid nasty null pointer exceptions.

1) First thing, first,  null is a keyword in Java, much like publicstatic or final. It's case sensitive, you cannot write null as Null or NULL, compiler will not recognize them and give error.

Object obj = NULL; // Not Ok
Object obj1 = null  //Ok

Are there any Java method ordering conventions?

  1. Class (static) variables: First the public class variables, then the protected, and then the private.
  2. Instance variables: First public, then protected, and then private.
  3. Constructors
  4. Methods: These methods should be grouped by functionality rather than by scope or accessibility. For example, a private class method can be in between two public instance methods. The goal is to make reading and understanding the code easier.

How to Find the Median Value

It's the middle of a sorted list of numbers.

Median Value

The Median is the "middle" of a sorted list of numbers.

How to Find the Median Value

To find the Median, place the numbers in value order and find the middle.

Example: find the Median of 12, 3 and 5

Put them in order:
3, 5, 12
The middle is 5, so the median is 5.


One’s Complement and Two’s Complement

One’s complement and two’s complement are two important binary concepts. Two’s complement is especially important because it allows us to represent signed numbers in binary, and one’s complement is the interim step to finding the two’s complement.

Two’s complement also provides an easier way to subtract numbers using addition instead of using the longer, more involving subtraction method discussed in Lesson 10.
We will save the two’s complement subtraction for another lesson, but here, we will look at what these terms mean and how to calculate them.

One’s Complement

If all bits in a byte are inverted by changing each 1 to 0 and each 0 to 1, we have formed the one’s complement of the number.
Original       One's Complement
10011001  -->  01100110
10000001  -->  01111110
11110000  -->  00001111
11111111  -->  00000000
00000000  -->  11111111

Converting to one's complement.
Converting to one’s complement.
And that is all there is to it! One’s complement is useful for forming the two’s complement of a number.

binary search algorithm visualization