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
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
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
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
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
When working with text in Java we often need to include new line characters to the format output properly. Different operating systems have different conventions for new line characters: Windows: This uses \r (Carriage Return + Line Feed). Unix/Linux: This uses (Line Feed). Mac (pre-OS X): This uses \r (Carriage Return). To write code that works seamlessly across all platforms and we need to use a platform-independent way of handling new line characters. This article will guide us through the different methods available in the Java to achieve this. Using Platform-Independent New Line Characters The recommended way ... Read More
Jackson is a Java-based library, and it can be useful to convert Java objects to JSON and JSON to Java objects. A Jackson API is faster than other API, needs less memory, and is good for large objects. We can format a date using the setDateFormat() of ObjectMapper class. This method can be used for configuring the default DateFormat when serializing time values as Strings and deserializing from JSON Strings. Syntax of setDateFormat() public ObjectMapper setDateFormat(DateFormat df) Here, df is the DateFormat instance to be used for formatting dates. Steps to format a date using Jackson in Java Following ... Read More
Jackson is a library that allows you to convert Java objects into XML and vice versa. In this example, we will demonstrate how to convert a POJO (Plain Old Java Object) into XML using the Jackson library. Well, if you are not familiar with POJO, it is a simple Java object that does not follow any specific framework or design pattern. It is just a regular Java class with fields and methods. We will use the method writeValueAsString() of the XmlMapper class to convert a POJO into XML. The XmlMapper class is part of the Jackson library and it ... Read More
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
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