Found 2616 Articles for Java

Byte Streams in Java

Maruthi Krishna
Updated on 15-Oct-2019 07:51:15

4K+ Views

These handle data in bytes (8 bits) i.e., the byte stream classes read/write data of 8 bits. Using these you can store characters, videos, audios, images etc.The InputStream and OutputStream classes (abstract) are the super classes of all the input/output stream classes: classes that are used to read/write a stream of bytes. Following are the byte array stream classes provided by Java −InputStreamOutputStreamFIleInputStreamFileOutputStreamByteArrayInputStreamByteArrayOutputStreamObjectInputStreamObjectOutputStreamPipedInputStreamPipedOutputStreamFilteredInputStreamFilteredOutputStreamBufferedInputStreamBufferedOutputStreamDataInputStreamDataOutputStreamExampleFollowing Java program reads data from a particular file using FileInputStream and writes it to another, using FileOutputStream.import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class IOStreamsExample {    public static void main(String args[]) throws IOException { ... Read More

How to create a directory hierarchy using Java?

Maruthi Krishna
Updated on 12-Mar-2024 18:37:31

1K+ Views

The class named File of the java.io package represents a file or directory (path names) in the system. This class provides various methods to perform various operations on files/directories. The mkdir() method of this class creates a directory with the path represented by the current object. Creating directory hierarchy There are two main ways to create a directory hierarchy in Java − Using mkdirs() Method Using createDirectories() Method Let us look at these solutions one by one. Using mkdirs() Method To create a hierarchy of new directories you can using the method mkdirs() of the same class. This ... Read More

What is the purpose of the flush() method of the BufferedWriter class in java?

Maruthi Krishna
Updated on 15-Oct-2019 07:44:38

297 Views

While you are trying to write data to a Stream using the BufferedWriter object, after invoking the write() method the data will be buffered initially, nothing will be printed.The flush() method is used to push the contents of the buffer to the underlying Stream.ExampleIn the following Java program, we are trying to print a line on the console (Standard Output Stream). Here we are invoking the write() method by passing the required String.import java.io.BufferedWriter; import java.io.IOException; import java.io.OutputStreamWriter; public class BufferedWriterExample {    public static void main(String args[]) throws IOException {       //Instantiating the OutputStreamWriter class     ... Read More

Converting a StringBuilder to String in Java

Maruthi Krishna
Updated on 02-Sep-2023 15:06:14

52K+ Views

The toString() method of the StringBuilder class reruns String value of the current object. To convert a StringBuilder to String value simple invoke the toString() method on it.Instantiate the StringBuilder class.Append data to it using the append() method.Convert the StringBuilder to string using the toString() method.ExampleIn the following Java program we are converting an array of Strings to a single String using the toString() method of the StringBuilder. Live Demopublic class StringToStringBuilder {    public static void main(String args[]) {       String strs[] = {"Arshad", "Althamas", "Johar", "Javed", "Raju", "Krishna" };       StringBuilder sb = new StringBuilder(); ... Read More

Converting String to StringBuilder in Java

Maruthi Krishna
Updated on 15-Oct-2019 07:38:52

411 Views

The append() method of the StringBuilder class accepts a String value and adds it to the current object.To convert a String value to StringBuilder object −Get the string value.Append the obtained string to the StringBuilder using the append() method.ExampleIn the following Java program, we are converting an array of Strings to a single StringBuilder object. Live Demopublic class StringToStringBuilder {    public static void main(String args[]) {       String strs[] = {"Arshad", "Althamas", "Johar", "Javed", "Raju", "Krishna" };       StringBuilder sb = new StringBuilder();       sb.append(strs[0]);       sb.append(" "+strs[1]);       sb.append(" ... Read More

Removing leading zeros from a String using apache commons library in Java

Maruthi Krishna
Updated on 15-Oct-2019 07:31:57

2K+ Views

The stripStart() method of the org.apache.commons.lang.StringUtils class accepts two strings and removes the set of characters represented by the second string from the string of the first string.To remove leading zeros from a string using apache communal library −Add the following dependency to your pom.xml file    org.apache.commons    commons-lang3    3.9 Get the string.Pass the obtained string as first parameter and a string holding 0 as second parameter to the stripStart() method of the StringUtils class.ExampleThe following Java program reads an integer value from the user into a String and removes the leading zeroes from it using the stripStart() ... Read More

Remove Leading Zeroes from a String in Java using regular expressions

Maruthi Krishna
Updated on 15-Oct-2019 12:51:11

6K+ Views

The replaceAll() method of the String class accepts two strings representing a regular expression and a replacement String and replaces the matched values with given String.Following is the regular expression to match the leading zeros of a string −The ^0+(?!$)";To remove the leading zeros from a string pass this as first parameter and “” as second parameter.ExampleThe following Java program reads an integer value from the user into a String and removes the leading zeroes from it using the Regular expressions. Live Demoimport java.util.Scanner; public class LeadingZeroesRE {    public static String removeLeadingZeroes(String str) {       String strPattern ... Read More

How to wrap a JSON using flexjson in Java?

raja
Updated on 06-Jul-2020 12:45:15

887 Views

The Flexjson library is a lightweight Java library for serializing and de-serializing java beans, maps, arrays, and collections in a JSON format. A JSONSerializer is the main class for performing serialization of Java objects to JSON and by default performs a shallow serialization. We can wrap a JSON object using the rootName() method of JSONSerializer class, this method wraps the resulting JSON in a javascript object that contains a single field named rootName.Syntaxpublic JSONSerializer rootName(String rootName)Exampleimport flexjson.JSONSerializer; public class JSONRootNameTest {    public static void main(String[] args) {       JSONSerializer serializer = new JSONSerializer().rootName("My_Employee").prettyPrint(true);       Employee emp = new Employee("Adithya", "Jai", 28, ... Read More

Can an interface in Java extend multiple interfaces?

Maruthi Krishna
Updated on 15-Oct-2019 12:48:00

758 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 as shown below:interface ArithmeticCalculations {    public abstract int addition(int a, int b);    public abstract int subtraction(int a, int b); } interface MathCalculations extends ArithmeticCalculations {    public abstract double squareRoot(int a);    public abstract double powerOf(int a, int b); }In the same way you can extend multiple interfaces from an interface using the extends keyword, by separating the interfaces using comma (, ) ... Read More

What is loose coupling how do we achieve it using Java?

Maruthi Krishna
Updated on 15-Oct-2019 07:22:53

719 Views

Coupling refers to the dependency of one object type on another, if two objects are completely independent of each other and the changes done in one doesn’t affect the other both are said to be loosely coupled.You can achieve loose coupling in Java using interfaces -Example Live Demointerface Animal {    void child(); } class Cat implements Animal {    public void child() {       System.out.println("kitten");    } } class Dog implements Animal {    public void child() {       System.out.println("puppy");    } } public class LooseCoupling {    public static void main(String args[]) {       Animal obj = new Cat();       obj.child();    } }Outputkitten

Advertisements