Found 9150 Articles for Object Oriented Programming

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

How to replace all occurrences of a word in a string with another word in java?

Maruthi Krishna
Updated on 10-Oct-2019 10:40:12

1K+ Views

The replaceAll() method of the String class accepts two strings, One representing a regular expression to find a string and the other representing a replacement string.And, replaces all the matched sequences with the given String. Therefore, to replace a particular word with another in a String −Get the required String.Invoke the replace all method on the obtained string by passing, a regular expression representing the word to be replaced (within word boundaries “\b”) and a replacement string as parameters.Retrieve the result and print it.Exampleimport java.io.File; import java.io.IOException; import java.util.Scanner; public class ReplacingWords {    public static void main(String[] args) throws ... Read More

What is the easiest way to reverse a String in Java?

Maruthi Krishna
Updated on 05-Dec-2023 09:29:13

1K+ Views

Built-in reverse() method The StringBuffer class provides you a method named reverse(). It reverses the contents of the current StringBuffer object and returns the resultant StringBuffer object. It is the easiest way to reverse a Sting using Java. To do so − Instantiate the StringBuffer class by passing the required String as a parameter. Invoke the reverse() method od the created object. Convert it into String again using the toString() method. Example public class Sample { public static void main(String args[]) { String str = new String("Hello how are you"); StringBuffer sb = new ... Read More

How to serialize the order of properties using the Jackson library in Java?

Aishwarya Naglot
Updated on 01-Sep-2025 13:33:44

4K+ Views

In this article, let's learn about the serialization of the order of properties using the Jackson library in Java. The Jackson library is used for processing JSON data in Java. The @JsonPropertyOrder is an annotation to be used at the class level. It takes as property a list of fields that defines the order in which fields can appear in the string resulting from the object JSON serialization. The properties included in the annotation declaration can be serialized first(in defined order), followed by any properties not included in the definition. Steps to serialize the order of properties using the Jackson ... Read More

How to delete a string inside a file(.txt) in java?

Aishwarya Naglot
Updated on 01-Sep-2025 13:59:21

7K+ Views

In this article, we will learn how to delete a string inside a file (.txt) in Java. The following are the ways to do so: Using FileOutputStream and replaceAll() method. Using replace() method. Using FileOutputStream The FileOutputStream is a class in the java.io package. It is mainly used for writing data to a file. It creates a file output stream to write data to the specified file. To delete a string inside a file using the FileOutputStream class, follow the steps below: Create a file ... Read More

How can we add an underscore before each capital letter inside a java String?

Maruthi Krishna
Updated on 01-Sep-2025 12:32:35

3K+ Views

In this article, we will learn how to add an underscore before each capital letter in a Java String. We also call this process as "camel case to snake case" conversion. We can solve this problem using the following ways: Using StringBuffer class. Using Regular Expressions. Using StringBuffer class We will use the method called append() of the StringBuffer class to add an underscore. In this method, we will traverse through each character of the string and check if it is an uppercase letter. If it is, we will append ... Read More

How to implement a custom serializer using the Jackson library in Java?

raja
Updated on 06-Jul-2020 11:52:25

1K+ Views

The Jackson API provides a number of methods to work with JSON data. By using Jackson API, we can convert Java objects to JSON string and reform the object from the JSON string. We can implement a custom serializer using the StdSerializer class and need to override the serialize(T value, JsonGenerator gen, SerializerProvider provider) method, the first argument value represents value to serialize(can not be null), the second argument gen represents generator used to output resulting Json content and the third argument provider represents provider that can be used to get serializers for serializing objects value.Syntaxpublic abstract void serialize(T value, JsonGenerator gen, SerializerProvider provider) throws IOExceptionExampleimport java.io.*; ... Read More

How to insert a string in beginning of another string in java?

Maruthi Krishna
Updated on 01-Sep-2025 14:02:49

7K+ Views

Let's learn how to add a string at the beginning of another string in Java. We can do this in the following ways: Using the toCharArray(). Using the String concatenation operator. Using the StringBuilder class. Using the StringBuffer class. Using the toCharArray() toCharArray() method of the String class that helps us to convert a string into a character array. We will use this method to insert a string at the beginning of another string. Following are the steps to do so: Get both strings, suppose we have a string str1 and the string to be added at ... Read More

How convert an array of Strings in a single String in java?

Maruthi Krishna
Updated on 01-Sep-2025 12:34:09

33K+ Views

In this article, we will learn how to convert an array of strings into a single string in Java. There are several ways to do so. The following are the different ways to convert an array of strings into a single string: Using the StringBuffer class. Using the toString() method of the Arrays class. Using the StringJoiner class. Using StringBuffer The StringBuffer class is used for creating mutable (modifiable) strings. It is similar to the String class, but it is mutable. The StringBuffer class is synchronized, which means it is thread-safe. To convert an array of strings into ... Read More

Advertisements