Quickest way to append text to a file in Java

In Java, there are several different ways to append text in a file. For file operations, Java provides several classes. To appending text quickly you can use Files class. Append text to file example: try { Files.write(Paths.get(“file1.txt”), “text to append”.getBytes(), StandardOpenOption.APPEND); } catch (IOException e) { // To exception handling here } But above method Read More …

How does Java for-each loop works?

Loops in programming languages used to iterate over collections like for loop, while .. do etc. In Java iterating over collections looks very ugly. Consider method in the following example which uses for loop to iterate over a collection. public static void main(String[] args) { List fruitList = new ArrayList(); fruitList.add(“mango”); fruitList.add(“banana”); fruitList.add(“apple”); printFruits(fruitList); } Read More …

Apache HttpClient with CookieStore (cookie support) in Java

In java many time we need to make a request with cookies support. For example, if a REST service is configured to manage session using cookie session or cookie-based session key then we need to use HTTP client with cookie support. For this Apache HttpClient client provides HttpClientBuilder Class, CookieStore interface and BasicCookieStore. Using these Read More …

Manually Closing connection of Apache HttpClient in Java

While Using Apache HttpClient library to make Http requests, we should always release connection immediately after completion of the operation. For making HttpRequest Apache HttpClient library provides CloseableHttpClient and CloseableHttpResponse classes which implements java closable interface. By calling the close method of a closeable object in finally block we can close or dispose of connection Read More …

Apache HttpClient Response Hadling in Java

Apache HttpClient library provides the ResponseHandler interface. By implementing ResponseHandler you can handle HTTP responses in a clean way. Using the response handler is the recommended way of executing HTTP requests and processing HTTP responses. This approach of handling response enables the caller to concentrate on the process of digesting HTTP responses and to delegate Read More …

Best way to generate random integers within a specific range in Java

There are many classes in Java to generate random numbers. Random and ThreadLocalRandom are best to generate numbers between ranges. ThreadLocalRandom class Since Java 7, it provides ThreadLocalRandom class to generate random numbers. A random number generator isolated to the current thread. Like the global Random generator used by the Math class, a ThreadLocalRandom is Read More …

Best and Simplest ways to print Java Array

Many time printing arrays in Java are required to logging and debugging purpose. In Java provides Arrays.toString(arr) or Arrays.deepToString(arr) for arrays within arrays (a nested array) to formatting simple arrays into printable strings. Example of printing a simple array: String names[] = new String[] {“ssp”,”anu”,”kirti”,”shaurya”}; System.out.println(names); System.out.println(Arrays.toString(names)); the output of above code is given below: [Ljava.lang.String;@2a139a55 [ssp, anu, Read More …