Maruthi Krishna has Published 1025 Articles

How to handle IllegalArgumentException inside an if using Java

Maruthi Krishna

Maruthi Krishna

Updated on 08-Feb-2021 11:30:06

While you use the methods that causes IllegalArgumentException, since you know the legal arguments of them, you can restrict/validate the arguments using if-condition before-hand and avoid the exception.We can restrict the argument value of a method using the if statement. For example, if a method accepts values of certain range, ... Read More

How to iterate the values of an enum using a for loop in Java?

Maruthi Krishna

Maruthi Krishna

Updated on 08-Feb-2021 11:26:53

Enumerations in Java represents a group of named constants, you can create an enumeration using the following syntax −enum Days {    SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }You can retrieve the contents of an enum using the values() method. This method returns an array containing all the values. ... Read More

How to search a directory with file extensions in Java?

Maruthi Krishna

Maruthi Krishna

Updated on 08-Feb-2021 11:21:27

Following example prints the files in a directory based on the extensions −Exampleimport java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.stream.Stream; public class Demo {    public static void main(String[] args) throws IOException {       Stream path = Files.walk(Paths.get("D:\ExampleDirectory"));       System.out.println("List of PDF files:");   ... Read More

How to list the hidden files in a directory in Java?

Maruthi Krishna

Maruthi Krishna

Updated on 08-Feb-2021 11:21:14

The ListFiles() method returns an array holding the objects (abstract paths) of all the files (and directories) in the path represented by the current (File) object.The File Filter interface is filter for the path names you can pass this as a parameter to the listFiles() method. This method filters the ... Read More

How to get the names of the empty directories in a directory in Java?

Maruthi Krishna

Maruthi Krishna

Updated on 08-Feb-2021 11:20:45

The ListFiles() method returns an array holding the objects (abstract paths) of all the files (and directories) in the path represented by the current (File) object.The File Filter interface is filter for the path names you can pass this as a parameter to the listFiles() method. This method filters the ... Read More

How to create object arrays Java? What are their disadvantages

Maruthi Krishna

Maruthi Krishna

Updated on 08-Feb-2021 11:15:48

Array is a container which can hold a fix number of items and these items should be of the same type. Most of the data structures make use of arrays to implement their algorithms. Following are the important terms to understand the concept of Array.Element: Each item stored in an ... Read More

How to get the directories (only) from a folder using Java?

Maruthi Krishna

Maruthi Krishna

Updated on 08-Feb-2021 11:15:36

The ListFiles() method returns an array holding the objects (abstract paths) of all the files (and directories) in the path represented by the current (File) object.The File Filter interface is filter for the path names you can pass this as a parameter to the listFiles() method. This method filters the ... Read More

How to list all files (only) from a directory using Java?

Maruthi Krishna

Maruthi Krishna

Updated on 08-Feb-2021 11:13:47

The ListFiles() method returns an array holding the objects (abstract paths) of all the files (and directories) in the path represented by the current (File) object.The File Filter interface is filter for the path names you can pass this as a parameter to the listFiles() method. This method filters the ... Read More

How to create directories (hierarchically) in Java?

Maruthi Krishna

Maruthi Krishna

Updated on 08-Feb-2021 11:10:25

Since Java 7 the Files class was introduced this contains (static) methods that operate on files, directories, or other types of files.The createDirectories() method creates the given directory including the non-existing parent directory.ExampleLive Demoimport java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Scanner; public class Demo {    public static void main(String ... Read More

How to read all files in a folder to a single file using Java?

Maruthi Krishna

Maruthi Krishna

Updated on 08-Feb-2021 11:10:12

The listFiles() method of the File class returns an array holding the objects (abstract paths) of all the files (and directories) in the path represented by the current (File) object.To read the contents of all the files in a folder into a single file −Create a file object by passing ... Read More

Advertisements