The Java IO API provides two kinds of interfaces for reading files, streams and readers. The streams are used to read binary data and readers to read character data. Since a text file is full of characters, you should be using a Reader implementations to read it. There are several ways to read a plain text file in Java e.g. you can use FileReader, BufferedReader or Scanner to read a text file. Every utility provides something special e.g. BufferedReader provides buffering of data for fast reading, and Scanner provides parsing ability.
Thursday, April 10, 2025
Wednesday, May 24, 2023
How to check File Permission in Java with Example - Java IO Tutorial
Java provides several methods to check file and directory permissions. In the last couple of articles, we have seen how to create Files in java and how to read a text file in Java, and in this article, we will learn how to check whether the file is read-only, whether the file has to write permission or not, etc. In Java we know we have a file object to deal with Files if we have created any file in our application using the file object, we have the privilege to check the access permission of that file using a simple method of File class in Java. Let see what the methods are and how to use that method
Labels:
core java
,
Java File Tutorial
,
java IO tutorial
How to Change File Permissions in Java – Example Tutorial
Hello guys, In the last article, we saw how to check whether an application can access the file and perform any read-write or execute an operation on that file by using the inbuilt method provided by File Object. Now we deal with some more methods of file class which will use to provide some privileges to users so that they can perform read, write, and execute operations on the particular file. There are few more methods added with the new File API in Java 7, but in this tutorial, we will only learn about traditional ways to change the permission of a file from the Java program, which should work on all versions of JDK.
Labels:
core java
,
Java File Tutorial
,
java IO tutorial
How to check if a File is hidden in Java? Example
This article is a quick tip to find hidden files in Java by checking the hidden properties of a File or directory from the Java Program. File Handling is a crucial topic in java already we have discussed how to create a file in java, how to create a directory, how to read write in a text file in Java, how to set permissions on the file, and some more aspects and we will discuss one more property of the file, i.e. hidden property. Now the question arises can we make a file Hidden using our java code or not.
Labels:
core java
,
Java File Tutorial
,
java IO tutorial
Saturday, April 22, 2023
How to create Soft and Hard Links in Java? Files + Path + Java NIO Example
Hello guys, not many people know that Java NIO File API also allows you to create links in Java. Many of Java programmer are familiar with ln command in Linux which is used to create soft and hard link but you can also do that using Java File API. Path represents not only files and directories, but links to files and directories, symbolic or otherwise. In fact, the Path class detects and handles links automatically, without requiring you to do anything specific when one is encountered in your application. However, you do have options available on how to handle symbolic links when they occur, and you can also create new links in Java for file systems that support them.
Labels:
core java
,
Java File Tutorial
,
java IO tutorial
Tuesday, February 8, 2022
3 Ways to Read File line by line in Java 8? Examples
Java 8 has added a new method called lines() in the Files class which can be used to read a file line by line in Java. The beauty of this method is that it reads all lines from a file as Stream of String, which is populated lazily as the stream is consumed. So, if you have a huge file and you only read the first 100 lines then the rest of the lines will not be loaded into memory, which results in better performance. This is slightly different from the Files.readAllLines() method (which reads all lines into a List) because this method reads the file lazily, only when a terminal operation is called on Stream like forEach(), count(), etc. By using the count() method you can actually count a number of lines in files or the number of empty lines by filtering empty lines.
Labels:
core java
,
Java 8
,
Java File Tutorial
Friday, September 10, 2021
Reading/Writing to/from Files using FileChannel and ByteBuffer in Java - Example Tutorial
In the past, I have talked about RandomAccessFile and how it can be used for doing faster IO in Java, and in this Java NIO tutorial, we are going to see how to use read/write data from using FileChannel and ByteBuffer. Channel provides an alternate way to read data from a file, it provides better performance than InputStream or OutputStream. It can also be opened in blocking and non-blocking mode. Though FileChannles are read/write channels and they are always blocking, they cannot be put into non-blocking mode.
Labels:
core java
,
Java File Tutorial
,
Java NIO Tutorial
Sunday, September 5, 2021
How to get the last modified date and time of a File or Directory in Java? Example Tutorial
Sometimes before processing a file, you want to check its last modified date to avoid processing an old file. Though some programmers prefer to attach the date in the file name itself, I don't find it a cleaner approach. For example, suppose you are downloading closing prices of stocks and processing them at the start of the day, and loading them into the database. In order to accidentally process an old file, you can check the last modified date before processing and if it's in the acceptable range, you can process the file. You can get the last modified date of a file in Java by using java.io.File class.
Labels:
Java File Tutorial
,
java IO tutorial
Wednesday, July 28, 2021
7 Examples to Read File into a Byte Array in Java
Hello guys, Java programmers often face scenarios in real-world programming, where they need to load data from a file into a byte array, it could be text or binary file. One example is to convert the contents of a file into String for display. Unfortunately, Java's File class, which is used to represent both files and directories, doesn't have a method say toByteArray(). It only holds path and allows you to perform certain operations like opening and closing file, but doesn't allow you to directly convert File to a byte array. Anyway, no need to worry as there are several other ways to read File into a byte array and you will learn those in this Java file tutorial.
Labels:
core java
,
Java File Tutorial
Friday, July 23, 2021
How to read a text file using Scanner in Java? Example Tutorial
As I told you before that there are multiple ways to read a file in Java e.g. FileReader, BufferedReader, and FileInputStream. You chose the Reader or InputStream depending upon whether you are reading text data or binary data, for example, the BufferedReader class is mostly used to read a text file in Java. The Scanner class is also another way to read a text file in java. Even though Scanner is more popular as a utility to read user input from the command prompt, you will be glad to know that you can also read a file using Scanner.
Labels:
Java File Tutorial
,
java IO tutorial
Subscribe to:
Posts
(
Atom
)