Programming Articles - Page 2396 of 3366

How to execute an external program like windows media player in Java?

Maruthi Krishna
Updated on 11-Oct-2019 06:56:18

1K+ Views

Using the Runtime classJava provides a class named java.lang.Runtime, using this class you can interface with the current environment.The getRunTime() (static) method of this class returns a Runtime object associated with the current application.The exec() method accepts a String value representing the command to execute a process in the current environment (system) and executes it.Therefore, to execute an external application using the Runtime class −Get the run time object using the getRuntime() method.Execute the required process by passing the path of it as a String value to the exec() method.Exampleimport java.io.IOException; public class Trail {    public static void main(String ... Read More

How to check that a string is parse-able to a double in java?

Maruthi Krishna
Updated on 01-Sep-2025 14:10:42

6K+ Views

Let's learn how to check if a string is parseable to a double in Java. We have multiple ways to do this. Some of them are: Using Double.parseDouble() Using Double.valueOf() Using the constructor of the Double class Using Double.parseDouble() The parseDouble() method of the java.lang.Double class accepts a String value, parses it, and returns the double value of the given String. If you pass a null value to this method, it throws a NullPointerException and if this method is not able to parse the given string ... Read More

How to read the contents of a web page without using any external library in Java?

Maruthi Krishna
Updated on 11-Oct-2019 06:38:41

390 Views

The URL class of the java.net package represents a Uniform Resource Locator which is used to point a resource (file or, directory or a reference) in the world wide web.The openStream() method of this class opens a connection to the URL represented by the current object and returns an InputStream object using which you can read data from the URL.Therefore, to read data from web page (using the URL class) −Instantiate the java.net.URL class by passing the URL of the desired web page as a parameter to its constructor.Invoke the openStream() method and retrieve the InputStream object.Instantiate the Scanner class ... Read More

When can we call @JsonAnyGetter and @JsonAnySetter annotations in Java?

raja
Updated on 06-Jul-2020 12:14:04

2K+ Views

The @JsonAnyGetter annotation enables to use a Map as a container for properties that we want to serialize to JSON and @JsonAnySetter annotation instructs Jackson to call the same setter method for all unrecognized fields in the JSON object, which means that all fields that are not already mapped to a property or setter method in the Java object.Syntaxpublic @interface JsonAnyGetter public @interface JsonAnyGetterExampleimport java.io.*; import java.util.*; import com.fasterxml.jackson.core.*; import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.annotation.*; public class JsonAnyGetterAndJsonAnySetterTest {    public static void main(String args[]) throws JsonGenerationException, JsonMappingException, IOException {       Employee emp1 = new Employee();       emp1.setFirstName("Adithya");       emp1.setLastName("Sai"); ... Read More

How can we ignore the fields during JSON serialization in Java?

Aishwarya Naglot
Updated on 01-Sep-2025 13:31:48

14K+ Views

If there are fields in Java objects that do not wish to be serialized, we can use the @JsonIgnore annotation in the Jackson library. The @JsonIgnore can be used at the field level, for ignoring fields during the serialization and deserialization. In this article, we will learn how to ignore the fields during JSON serialization in Java using the Jackson library. Steps to ignore the fields during JSON serialization in Java: In order to use Jackson, you will need to add it to your project. If you use Maven, add the following dependency to your ... Read More

Java program to check for prime and find next Prime in Java

Aishwarya Naglot
Updated on 01-Sep-2025 13:54:06

6K+ Views

In this article, let's learn how to check for a prime and find the next prime number in Java. Following are the different ways to do so: Using the isPrime() method. Using the nextPrime() method. Manual checking for prime and next prime. Using the isProbablePrime() method The isProbablePrime() is method of the java.math.BigInteger class. It accepts an integer value as a parameter, checks if it is prime or not, and returns true if it is prime, else it returns false. To check if a number is prime using the isPrime() method, follow the steps below: Instantiate the ... Read More

Redirecting System.out.println() output to a file in Java

Aishwarya Naglot
Updated on 01-Sep-2025 13:54:55

10K+ Views

In Java, the System is a class that provides access to the resources called "System resources". It is a final class, which means it cannot be inherited by any other class. It is a part of the java.lang package. The System.out.println() method prints the data on the console. In this article, we will learn how to redirect the output of the System.out.println() method to a file. Following are few of the ways through we achieve this: Using System.setOut() with PrintStream Using FileOutputStream with PrintStream The filed named out of the System class represents a standard output Stream, an ... Read More

Java program to delete all the files in a directory recursively (only files)

Aishwarya Naglot
Updated on 01-Sep-2025 13:56:02

2K+ Views

To delete all the files in a directory recursively (only files), we can use the class Files, which is part of the java.nio.file package. The Files class helps in performing file operations such as creating, deleting, and copying files. Suppose we have a directory named test with the following structure: file1.txt file2.txt file3.txt subdir1 file4.txt file5.txt subdir2 ... Read More

Java program to List all files in a directory recursively

Aishwarya Naglot
Updated on 12-May-2025 10:04:59

5K+ Views

The given task is to list all the files from a specific directory, recursively, which means we need to retrieve all the files in the main directory and all the subdirectories as well. For example, if we have a directory structure like below - MyDirectory / \ / ... Read More

Java program to delete duplicate lines in text file

Maruthi Krishna
Updated on 08-Jul-2024 12:02:44

3K+ Views

The interface set does not allow duplicate elements. The add() method of this interface accepts elements and adds to the Set object, if the addition is successful it returns true, if you try to add an existing element using this method, the addition operations fails returning false. Problem Statement Given a file which contains duplicate lines, write a program in Java to read the file, remove duplicate lines, and write the unique lines to a new file. Input Hello how are you Hello how are you welcome to Tutorialspoint Output Hello how are you welcome to Tutorialspoint ... Read More

Advertisements