4 ways to read String from File in Java - Example

Just like there are many ways for writing String to text file, there are multiple ways to read String form File in Java. You can use FileReader, BufferedReader, Scanner, and FileInputStream to read text from file. One thing to keep in mind is character encoding. You must use correct character encoding to read text file in Java, failing to do so will result in logically incorrect value, where you don't see any exception but content you have read is completely different than file's original content. Many of the method which is used to read String by default uses platform's default character encoding but they do have overloaded version which accepts character encoding. 

How to download a file using HTTP in Java? Example

Hello guys, if you are looking for tutorial to understand about how to download a  file using http in Java to have deep knowledge of java basics then you will definitely learn from this article. uploading and downloading files are common task but many Java developer are not aware about how to complete that. They don't know about HTTP and how to send and receive data using HTTP. Many of them also doesn't know about HttpClient which was added in JDK 11 as well as third party libraries like Apache HttpClient which you can use before JDK 11. Anyway, don't worry, we are again here with new article that is on download file using http will give all basics about the topic to our viewers or readers.  Let's start with an example first.

How to read CSV file in Java without using a third-party library? FileReader Example Tutorial

Hello guys, as you learn Java, one thing that comes up early and often is reading from/ writing to files, and doing something useful with that data. This is also one thing which you will keep doing and also forgetting very soon, if you are not then you have good memory as I always forget it and then I do google search to find my own articles. If you read this blog then you know that in the past, I have shared how to load CSV file using OpenCSV library as well how to load a CSV file with header using Jackson but a lot of you asked me how to do this without using any third party library like Jackson or OpenCSV. So, I thought to write about this article and I actually written but forgot to publish it for a long time. But I today I found it and publishing it now, it still works well. 

How to write to a File with try-with-resource in Java? Example Tutorial

Hello Java programmers and all people learning Java, if you are familiar with try-wit-resource statement then you know that its a great language functionality and tool to open files, sockets, streams, and network connections or any resource which are require closing. Before try-with-resource was introduce in Java 7, Java developers have to manually write try catch finally block to close the connections for both success and failure cases to prevent resource leak but it was also tricky and many programmer make mistakes which actually resulted in resource leaks. One common example of that is running out of file descriptors which is used for both opening file and socket in Java.  

How to recursive copy directory in Java with sub-directories and files? Example

Hello guys, it's a common requirement in Java to copy a whole directory with sub-directories and files but it's not that straightforward, especially not until Java 7. JDK 7 introduced some new file utility methods as part of Java NIO 2 which we will use in this article to show you how to recursively copy a whole directory with sub-directories and files in Java. I'll also show you how you can use Apache Commons if you are not running on Java 7 or higher versions. Basically, You can copy a file or directory by using the copy(Path, Path, CopyOption...) method. The copy fails if the target file exists unless the REPLACE_EXISTING option is specified.

How to check if a File exists in Java with Example

Hello guys,  if you are working in Java for a couple of years, then you have come across a scenario where you need to check if a file exists or not. This is often required while writing defensive code that writes or reads from a file. While there is a method to check if a file exists in Java or not, it's not straightforward to use because it returns true if a given path represents a file or a directory. Yes, I am talking about the File. Exists () method of the java.io package. This method is the key method to solve this problem, but you need to keep in mind that it returns true if a given path exists and it's either file or directory. 

How to append text to file in Java? Example Tutorial

Hello guys, In this article, I'll teach you how to append text to a file in Java. Earlier, I have taught you guys how to create a file and write text into it, but, append is totally different than creating a new file and writing data into it. In the case of append, the file already exists and you just add text at the end of the file. One popular example of appending text into a file is a log file that is constantly updated because your application continuously appends log details into it. While you don't need to create a logging framework here, just knowing how to append text into an existing file in Java is useful for Java programmers of all kinds of experience.

How to work with Files and Directories in Java? Example Tutorial

The File API is one of the important parts of any programming language or API and even though Java's file API both new and old, is powerful, they are not intuitive enough compared to other languages like Python. Apart from knowing the essential classes and abstractions e.g. File, InputStream, OutputStream, Reader, Writer, Channel, etc, you also need to know and remember some nitty-gritty detail to avoid subtle issues. There are many articles out there on the internet which can teach you how to read and write data from the file but there are very few which will tell you to do it in the right way.

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 that 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 a large file and one of the common ways to write binary data in Java.

How to create a ZIP File in Java? ZipEntry and ZipOutputStream Compression Example

Since compressing and archiving old log files is an essential housekeeping job in any Java application environment, a Java programmer should know how to compress files in .zip format and then how to read them programmatically if required. The JDK provides full support to create and read ZIP files in Java. There is a separate package java.util.zip to hold all classes related to zipping and unzipping files and streams. In this series of articles, you will learn how to use those classes like ZipFile, ZipEntry, ZipInputStream, and ZipOutputStream, etc.