Found 2619 Articles for Java

Reading data from keyboard using console class in Java

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

501 Views

The Console class is used to write/read data from the console (keyboard/screen) devices. It provides a readLine() method which reads a line from the key-board. You can get an object of the Console class using the console() method.Note − If you try to execute this program in a non-interactive environment like IDE it doesn’t work.ExampleFollowing Java program reads data from user using the Console class. Live Demoimport java.io.BufferedReader; import java.io.Console; import java.io.IOException; import java.io.InputStreamReader; class Student {    String name;    int age;    float percent;    boolean isLocal;    char grade;    Student(String name, int age, float percent, boolean isLocal, ... Read More

Character streams in Java

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

6K+ Views

Character Streams − These handle data in 16 bit Unicode. Using these you can read and write text data only.The Reader and Writer classes (abstract) are the super classes of all the character stream classes: classes that are used to read/write character streams. Following are the character array stream classes provided by Java −ReaderWriterBufferedReaderBufferedWriterCharacterArrayReaderCharacterArrayWriterStringReaderStringWriterFileReaderFileWriterInputStreamReaderInputStreamWriterFileReaderFileWriterExampleThe following Java program reads data from a particular file using FileReader and writes it to another, using FileWriter.import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class IOStreamsExample {    public static void main(String args[]) throws IOException {       //Creating FileReader object     ... Read More

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

950 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

286 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

51K+ 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

395 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

1K+ 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

846 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

Advertisements