Found 4338 Articles for Java 8

How to read/write data from/to .properties file in Java?

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

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

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

How to find the Strings within a text file in Java?

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

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

Can we use readUTF() to read a string from a .txt file in Java?

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

285 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

Reading UTF8 data from a file using Java

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

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

Writing UTF8 data to a file using Java

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

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

How can we check if file exists anywhere on the system in Java?

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

255 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

How to read integers from a file using BufferedReader in Java?

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

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

How do you upcast and downcast the same object in Java?

Maruthi Krishna
Updated on 11-Sep-2019 07:22:54

234 Views

Converting one data type to others in Java is known as casting.Up casting − If you convert a higher datatype to lower datatype, it is known as narrowing (assigning higher data type value to the lower data type variable).Example Live Demoimport java.util.Scanner; public class NarrowingExample {    public static void main(String args[]){       char ch = (char) 67;       System.out.println("Character value of the given integer: "+ch);    } }OutputCharacter value of the given integer: CDown casting − If you convert a lower datatype to a higher data type, it is known as widening (assigning lower data type ... Read More

Why subclass doesn't inherit the private instance variables of superclass in Java?

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

5K+ Views

When you declare the instance variables of a class private, you cannot access them in another class if you try to do so a compile-time error will be generated.But, if you inherit a class that has private fields, including all other members of the class the private variables are also inherited and available for the subclass.But, you cannot access them directly, if you do so a compile-time error will be generated.Example Live Democlass Person{    private String name;    public Person(String name){       this.name = name;    }    public void displayPerson() {       System.out.println("Data of the ... Read More

Advertisements