Packages Containing Wrapper Classes in Java

Maruthi Krishna
Updated on 10-Sep-2019 12:42:21

2K+ Views

Java provides certain classes called wrapper classes in the java.lang package. The objects of these classes wrap primitive datatypes within them. Following is the list of primitive data types and their respective classes −Primitive datatypeWrapper classcharCharacterbyteByteshortShortintIntegerlongLongfloatFloatdoubleDoublebooleanBooleanPackageWrapper classes in Java belong to the java.lang package, Therefore there is no need to import any package explicitly while working with them.ExampleThe following Java example accepts various primitive variables from the user and creates their respective wrapper classes. Live Demoimport java.util.Scanner; public class WrapperClassesExample {    public static void main(String args[]){       Scanner sc = new Scanner(System.in);       System.out.println("Enter an integer ... Read More

Read Data from One File and Print to Another File in Java

Maruthi Krishna
Updated on 10-Sep-2019 12:38:40

3K+ Views

Java provides I/O Streams to read and write data where a Stream represents an input source or an output destination which could be a file, i/o devise, other programs, etc.In general, a Stream will be an input stream or, an output stream.InputStream − This is used to read data from a source.OutputStream − This is used to write data to a destination.Based on the data they handle there are two types of streams −Byte Streams − 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, ... Read More

Read and Write Data from Properties File in Java

Maruthi Krishna
Updated on 10-Sep-2019 12:33:06

12K+ Views

The .properties is an extension in java which is used to store configurable application. It is represented by the Properties class in Java, you can store a properties file and read from it using the methods of this class. This class inherits the HashTable class.Creating a .properties file −To create a properties file −Instantiate the Properties class.Populate the created Properties object using the put() method.Instantiate the FileOutputStream class by passing the path to store the file, as a parameter.ExampleThe Following Java program creates a properties file in the path D:/ExampleDirectory/ Live Demoimport java.io.FileOutputStream; import java.io.IOException; import java.util.Properties; public class CreatingPropertiesFile { ... Read More

Exception Hierarchy in Case of Multiple Catch Blocks

Maruthi Krishna
Updated on 10-Sep-2019 12:28:11

6K+ Views

An exception is an issue (run time error) that occurred during the execution of a program. When an exception occurred the program gets terminated abruptly and, the code past the line that generated the exception never gets executed.Multiple exceptions in a codeWhenever we have a code that may generate more than one exception and if you need to handle them specifically you can use multiple catch blocks on a single try.try{    //code } catch(Exception1 ex1) {    // } catch(Exception2 ex2) {    // }Example Live Demoimport java.util.Arrays; import java.util.Scanner; public class MultipleCatchBlocks {    public static void main(String [] ... Read More

Find Strings Within a Text File in Java

Maruthi Krishna
Updated on 10-Sep-2019 12:18:24

8K+ Views

Following Java program accepts a String value from the user, verifies whether a file contains the given String and prints the number of occurrences of the word too.Example Live Demoimport java.io.FileInputStream; import java.io.FileNotFoundException; import java.util.Scanner; public class FindingWordFromFile {    public static void main(String args[]) throws FileNotFoundException {       //Reading the word to be found from the user       Scanner sc1 = new Scanner(System.in);       System.out.println("Enter the word to be found");       String word = sc1.next();       boolean flag = false;       int count = 0;       ... Read More

Read String from a TXT File in Java using readUTF

Maruthi Krishna
Updated on 10-Sep-2019 12:13:58

452 Views

The readUTF() method of the java.io.DataOutputStream reads data that is in modified UTF-8 encoding, into a String and returns it.ExampleThe following Java program reads a UTF-8 text from a .txt file using the readUTF() method.import java.io.DataInputStream; import java.io.EOFException; import java.io.FileInputStream; import java.io.IOException; public class UTF8Example {    public static void main(String args[]) {       StringBuffer buffer = new StringBuffer();       try {          //Instantiating the FileInputStream class          FileInputStream fileIn = new FileInputStream("D:\test.txt");          //Instantiating the DataInputStream class          DataInputStream inputStream = new DataInputStream(fileIn); ... Read More

Read UTF-8 Data from a File Using Java

Maruthi Krishna
Updated on 10-Sep-2019 12:07:15

4K+ Views

In general, data is stored in a computer in the form of bits (1 or, 0). There are various coding schemes available specifying the set of bytes represented by each character.Unicode (UTF) − Stands for Unicode Translation Format. It is developed by The Unicode Consortium. if you want to create documents that use characters from multiple character sets, you will be able to do so using the single Unicode character encodings. It provides 3 types of encodings.UTF-8 − It comes in 8-bit units (bytes), a character in UTF8 can be from 1 to 4 bytes long, making UTF8 variable width.UTF-16 ... Read More

Write UTF-8 Data to a File Using Java

Maruthi Krishna
Updated on 10-Sep-2019 12:02:31

4K+ Views

In general, data is stored in a computer in the form of bits (1 or, 0). There are various coding schemes available specifying the set of bytes represented by each character.Unicode (UTF) − Stands for Unicode Translation Format. It is developed by The Unicode Consortium. if you want to create documents that use characters from multiple character sets, you will be able to do so using the single Unicode character encodings. It provides 3 types of encodings.UTF-8 − It comes in 8-bit units (bytes), a character in UTF8 can be from 1 to 4 bytes long, making UTF8 variable width.UTF-16 ... Read More

Check If File Exists Anywhere on the System in Java

Maruthi Krishna
Updated on 10-Sep-2019 11:58:33

384 Views

You can verify whether a particular file exists in the system in two ways using the File class and using the Files class.Using The File classThe 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.This class provides various methods to manipulate files, The exists a () method of it verifies whether the file or directory represented by the current File object exists if so, it returns true else it returns false.ExampleThe following Java program verifies whether a specified file exists in ... Read More

Read Integers from a File Using BufferedReader in Java

Maruthi Krishna
Updated on 10-Sep-2019 11:54:20

20K+ Views

The BufferedReader class of Java is used to read the stream of characters from the specified source (character-input stream). The constructor of this class accepts an InputStream object as a parameter.This class provides a method known as readLine() which reads and returns the next line from the source and returns it in String format.The BufferedReader class doesn’t provide any direct method to read an integer from the user you need to rely on the readLine() method to read integers too. i.e. Initially you need to read the integers in string format.The parseInt() method of the Integer class accepts a String ... Read More

Advertisements