How to lock a File before writing in Java? Example

A file is one of the oldest ways to store data and share data but if you are working in a shared file i.e a file which can be read or write by multiple readers and writers, you need to make sure that the file is locked before you try to write on it. This is needed to ensure that someone doesn't overwrite the data you are writing. Fortunately, Java provides a mechanism to lock a file before writing using the FileLock interface. You can get the handle of FileLock by using FileChannel for writing to a file. The FileChannel class is generally used to write faster in the large file and one of the common way to write binary data in Java.

How to Send HTTP Request from Java Program - Example Tutorial

If you are thinking is it possible to send an HTTP request from a Java program and if yes, how to send a simple HTTP GET request in Java, then you have come to the right place. In this article, I'll show you how you can use the HttpURLConnection class from package to send a simple HTTP request in Java. But, first let me answer your first question, is it possible to send HTTP request in Java? Yes, it's possible and you can send any kind of HTTP request e.g. GET, POST, PUT, DELETE, HEAD, or PATCH. The package provides a class called HttpURLConnection, which can be used to send any kind of HTTP or HTTPS request from Java program.

How to add/remove new elements to an array in Java?

Java beginners often ask how to add new elements to an array? or how to remove elements from an array in Java? is that possible in Java? Well, no, it's not possible in Java because arrays length cannot be changed once created. Which means neither you can add new elements nor you can remove an old one. All you can do is that reassign values to the different bucket. For example, if you have a String array of length 3 e.g. {"java", "is", "great"} then you can just replace the individual index to make it like {"Python", "is", "great"} by replace "java" with "Python" at first index, but you cannot add or remove elements.