Found 4338 Articles for Java 8

How to read certain number of elements from a file in Java?

Maruthi Krishna
Updated on 11-Sep-2019 13:59:07

615 Views

To read a fixed number of elements from a file you can either read a required number of data elements from the file and process them or, read the entire file into a collection or an array and process it for every n element.ExampleFollowing Java program, reads the contents of a file 10 words at a time and prints them in a separate line.import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class ReadingData {    public static void main(String args[]) throws FileNotFoundException {       //Creating an object of the File to read data       File file = ... Read More

How to read data from PDF file and display on console in Java?

Maruthi Krishna
Updated on 10-Sep-2019 13:44:22

10K+ Views

There are several libraries to read data from a pdf using Java. Let us see how to read data from a PDF document and display it on the console using a library named PDFBox.You can extract text using the getText() method of the PDFTextStripper class. This class extracts all the text from the given PDF document to use this.Load an existing PDF document using the static method load() of the PDDocument class.Instantiate the PDFTextStripper class.Retrieve.read the contents of the PDF page to a String using the getText() method of the PDFTextStripper class.Finally, close the document using the close() method of ... Read More

How to calculate String Buffer capacity in Java?

Maruthi Krishna
Updated on 10-Sep-2019 13:38:26

824 Views

The String class of the java.lang package represents a set of characters. All string literals in Java programs, such as "abc", are implemented as instances of this class. Strings objects are immutable, once you create a String object you cannot change their values, if you try to do so instead of changing the value a new object is created with the required value and the reference shifts to the newly created one leaving the previous object unused.The StringBuffer (and StringBuilder) class is used when there is a necessity to make a lot of modifications to a String.Unlike Strings, objects of ... Read More

How can we replace specific part of String and StringBuffer in Java?

Maruthi Krishna
Updated on 10-Sep-2019 13:33:41

218 Views

The String class of the java.lang package represents a set of characters. All string literals in Java programs, such as "abc", are implemented as instances of this class.Example Live Demopublic class StringExample {    public static void main(String[] args) {       String str = new String("Hello how are you");       System.out.println("Contents of the String: "+str);    } }OutputHello how are youStrings objects are immutable, once you create a String object you cannot change their values, if you try to do so instead of changing the value a new object is created with the required value and the ... Read More

How to trim white space in StringBuffer in Java?

Maruthi Krishna
Updated on 10-Sep-2019 13:28:48

2K+ Views

The String class of the java.lang package represents a set of characters. All string literals in Java programs, such as "abc", are implemented as instances of this class. Strings objects are immutable, once you create a String object you cannot change their values, if you try to do so instead of changing the value a new object is created with the required value and the reference shifts to the newly created one leaving the previous object unused.The StringBuffer (and StringBuilder) class is used when there is a necessity to make a lot of modifications to a String.Unlike Strings, objects of ... Read More

How can we check if specific string occurs multiple times in another string in Java?

Maruthi Krishna
Updated on 10-Sep-2019 13:24:09

2K+ Views

You can find whether a String contains a specified sequence of characters using any of the methods −The indexOf() method − The indexOf() method of the String class accepts a string value and finds the (starting) index of it in the current String and returns it. This method returns -1 if it doesn’t find the given string in the current one.The contains() method − The contains a () method of the String class accepts a sequence of characters value and verifies whether it exists in the current String. If found it returns true else it returns false.In addition to these, you ... Read More

How can we split a string by sentence as a delimiter in Java?

Maruthi Krishna
Updated on 10-Sep-2019 13:16:45

10K+ Views

The split() method of the String class accepts a String value representing the delimiter and splits into an array of tokens (words), treating the string between the occurrence of two delimiters as one token.For example, if you pass single space “ ” as a delimiter to this method and try to split a String. This method considers the word between two spaces as one token and returns an array of words (between spaces) in the current String.If the String does not contain the specified delimiter this method returns an array containing the whole string as an element.Example Live Demopublic class SplitExample ... Read More

Can I define more than one public class in a Java package?

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

6K+ Views

No, while defining multiple classes in a single Java file you need to make sure that only one class among them is public. If you have more than one public classes a single file a compile-time error will be generated.ExampleIn the following example we have two classes Student and AccessData we are having both of them in the same class and declared both public. Live Demoimport java.util.Scanner; public class Student {    private String name;    private int age;    Student(){       this.name = "Rama";       this.age = 29;    }    Student(String name, int age){   ... Read More

Which packages contain Wrapper class in Java?

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

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

How to 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

Advertisements