Maruthi Krishna

Maruthi Krishna

500 Articles Published

Articles by Maruthi Krishna

Page 6 of 50

Can we define an enum inside a class in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 08-Feb-2021 17K+ Views

Enumerations in Java represents a group of named constants, you can create an enumeration using the following syntaxenum Days {    SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }Yes, we can define an enumeration inside a class. You can retrieve the values in an enumeration using the values() method.ExampleLive Demopublic class EnumerationExample {    enum Enum {       Mango, Banana, Orange, Grapes, Thursday, Apple    }    public static void main(String args[]) {       Enum constants[] = Enum.values();       System.out.println("Value of constants: ");         for(Enum d: constants) {         ...

Read More

How to search a directory with file extensions in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 08-Feb-2021 1K+ Views

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:");       path = path.filter(var -> var.toString().endsWith(".pdf"));       path.forEach(System.out::println);             path = Files.walk(Paths.get("D:\ExampleDirectory"));       System.out.println("List of JPG files:");       path = path.filter(var -> var.toString().endsWith(".jpg"));       path.forEach(System.out::println);           path = Files.walk(Paths.get("D:\ExampleDirectory"));       ...

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 9K+ Views

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 the required file path as a parameter.Read the contents of each file using Scanner or any other reader.Append the read contents into a StringBuffer.Write the StringBuffer contents into the required output file.Exampleimport java.io.DataOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.Scanner; public class Test {    public static void main(String ...

Read More

How to get the list of jpg files in a directory in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 08-Feb-2021 1K+ Views

The String[] list(FilenameFilter filter) method of a File class returns a String array containing the names of all the files and directories in the path represented by the current (File) object. But the retuned array contains the filenames which are filtered based on the specified filter. The FilenameFilter is an interface in Java with a single method.accept(File dir, String name)To get the file names based on extensions implement this interface as such and pass its object to the above specified list() method of the file class.Assume we have a folder named ExampleDirectory in the directory D with 7 files and 2 directories ...

Read More

How to create Directories using the File utility methods in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 08-Feb-2021 430 Views

Since Java 7 the File.02s class was introduced this contains (static) methods that operate on files, directories, or other types of files.The createDirectory() method of the Files class accepts the path of the required directory and creates a new directory.ExampleFollowing Java example reads the path and name of the directory to be created, from the user, and creates it.Live 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 Test {    public static void main(String args[]) throws IOException {       System.out.println("Enter the path to create a directory: ");       Scanner sc = new Scanner(System.in);     ...

Read More

How to inherit multiple interfaces in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 06-Feb-2021 519 Views

An interface in Java is similar to class but, it contains only abstract methods and fields which are final and static.Just like classes you can extend one interface from another using the extends keyword. You can also extend multiple interfaces from an interface using the extends keyword, by separating the interfaces using comma (, ) as −interface MyInterface extends ArithmeticCalculations, MathCalculations{ExampleFollowing is the Java program demonstrating, how to extend multiple interfaces from a single interface.interface ArithmeticCalculations{    public abstract int addition(int a, int b);    public abstract int subtraction(int a, int b); } interface MathCalculations {    public abstract double ...

Read More

What is Java reg ex to check for date and time?

Maruthi Krishna
Maruthi Krishna
Updated on 06-Feb-2021 2K+ Views

To match a regular expression with the given string You need to:.Compile the regular expression of the compile() method of the Pattern class.Get the Matcher object bypassing the required input string as a parameter to the matcher() method of the Pattern class.The matches() method of the Matcher class returns true if a match occurs else it returns false. Therefore, invoke this method to validate the data.ExampleFollowing is a Java regular expression example matches only dateLive Demoimport java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Sample {    public static void main(String args[]){       //Creating the list to ...

Read More

What are date temporal fields in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 06-Feb-2021 3K+ Views

A temporal field is a field of date-time, such as month-of-year or hour-of-minute. These fields are represented by the TemporalField interface and the ChronoField class implements this interface.Following are the list of various temporal fields regarding date supported by the ChronoField class −FieldDescriptionALIGNED_DAY_OF_WEEK_IN_MONTHThis field represents the day of the week with in a month.ALIGNED_DAY_OF_WEEK_IN_YEARThis field represents the aligned day of a week in an year.ALIGNED_WEEK_OF_MONTHThis field represents the aligned wee of a month.ALIGNED_WEEK_OF_YEARThis field represents the aligned week of an year.DAY_OF_MONTHThis field represents the day of a month.DAY_OF_WEEKThis field represents the day of a week.DAY_OF_YEARThis field represents the day of ...

Read More

How to measure elapsed time in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 06-Feb-2021 5K+ Views

In general, the elapsed time is the time from the starting point to ending point of an event. Following are various ways to find elapsed time in Java −Using the currentTimeMillis() methodThe currentTimeMillis() method returns the current time in milliseconds. To find the elapsed time for a method you can get the difference between time values before and after the execution of the desired method.ExampleLive Demopublic class Example {    public void test(){ int num = 0;       for(int i=0; i

Read More

How to use formatting with printf() correctly in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 06-Feb-2021 378 Views

The printf() method is used to print a formatted string, it accepts a string representing a format string and an array of objects representing the elements that are to be in the resultant string, if the number of arguments are more than the number of characters in the format string the excess objects are ignored.Following table lists the various format characters to format time by the Java printf() method along with their description −Format CharactersDescription'H'The corresponding argument is formatted as Hour of the day (00-24).'I'The corresponding argument is formatted as hour of the day (01 -12).'k'The corresponding argument is formatted ...

Read More
Showing 51–60 of 500 articles
« Prev 1 4 5 6 7 8 50 Next »
Advertisements