Found 9150 Articles for Object Oriented Programming

BufferedReader class in Java.

Maruthi Krishna
Updated on 12-Sep-2019 06:26:28

8K+ 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 named read() and readLine() which reads and returns the character and next line from the source (respectively) and returns them.Instantiate an InputStreamReader class bypassing your InputStream object as a parameter.Then, create a BufferedReader, bypassing the above obtained InputStreamReader object as a parameter.Now, read data from the current reader as String using the readLine() or read() method.ExampleThe following Java program demonstrates how to read integer data ... Read More

What are variable arguments in java?

Maruthi Krishna
Updated on 12-Sep-2019 06:19:50

669 Views

While defining a method, In general, we will specify the arguments it accepts along with the type as −myMethod(int a, String b){ }Suppose if you need to accept more than one variable of the same type you need to specify the variables one after the other as −myMethod(int a, int b, int c){ }You can also pass a variable number of arguments of a particular type, to a method. These are known as variable arguments or, varargs. They are represented by three dots (…)Syntaxpublic myMethod(int ... a) {    // method body }Once you use variable arguments as a parameter ... Read More

How to add/append content to an existing file using Java?

Maruthi Krishna
Updated on 12-Sep-2019 06:15:25

481 Views

In most scenarios, if you try to write content to a file, using the classes of the java.io package, the file will be overwritten i.e. data existing in the file is erased and the new data is added to it.But, in certain scenarios like logging exceptions into a file (without using logger frameworks) you need to append data (message) in the next line of the file.You can do this using the Files class of the java.nio package. This class provides a method named write() which acceptsAn object of the class Path, representing a file.A byte array holding the data to ... Read More

When can we use the getClass() method in Java?

raja
Updated on 01-Dec-2023 14:43:00

225 Views

The getClass() method is from Object class and it returns an instance of the Class class. When we declare a new instance of an object, it will be referring to a class. There can only be one class per JVM but multiple object referring to it. So when we get the class of two objects, they might be referring to the same class. Syntax public final Class getClass() Example class User { private int id; private String name; public User(int id, String name) { this.id = id; this.name ... Read More

How to write data to .csv file in Java?

Maruthi Krishna
Updated on 11-Sep-2019 14:24:40

17K+ Views

A library named OpenCSV provides API’s to read and write data from/into a.CSV file. Here it is explained how to write the contents of a .csv file using a Java program.Maven dependency    com.opencsv    opencsv    4.4    org.apache.commons    commons-lang3    3.9 The CSVWriter class of the com.opencsv package represents a simple csv writer. While instantiating this class you need to pass a Writer object representing the file, to which you want to write the data, as a parameter to its constructor.It provides methods named writeAll() and writeNext() to write data to a .csv file.Using the ... Read More

How to read data from .csv file in Java?

Maruthi Krishna
Updated on 11-Sep-2019 14:16:41

4K+ Views

A library named OpenCSV provides API’s to read and write data from/into a.CSV file. Here it is explained how to read the contents of a .csv file using a Java program.Maven dependency    com.opencsv    opencsv    4.4    org.apache.commons    commons-lang3    3.9 The CSVReader class of the com.opencsv package represents a simple CSV reader. While instantiating this class you need to pass a Reader object representing the file to be read as a parameter to its constructor. It provides methods named readAll() and readNext() to read the contents of a .csv fileUsing the readNext() methodThe readNext() ... Read More

How to create a directory in project folder using Java?

Maruthi Krishna
Updated on 11-Sep-2019 14:10:53

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.Therefore, to create a directory −Instantiate the File class by passing the path of the directory you need to create, as a parameter (String).Invoke the mkdir() method using the above-created file object.ExampleThe following Java example reads the path and name of the directory to be created, from the user, and creates it. Live Demoimport java.io.File; import java.util.Scanner; ... Read More

How to move files using FileUtils in Java?

Maruthi Krishna
Updated on 11-Sep-2019 14:07:37

824 Views

Using the File classThe class named File of the java.io package represents a file or directory (pathnames) in the system. This class provides various methods to perform various operations on files/directories.This class provides various methods to manipulate files, The rename() method of the File class accepts a String representing a destination file and, renames the abstract file path of the current file to the given one.This method actually moves the file from the source path to the destination path.Example Live Demoimport java.io.File; public class MovingFile {    public static void main(String args[]) {       //Creating a source file object ... Read More

How to read data from all files in a directory using Java?

Maruthi Krishna
Updated on 11-Sep-2019 14:04:20

12K+ Views

The class named File of the java.io package represents a file or directory (pathnames) in the system. This class provides various methods to perform various operations on files/directories.To get the list of all the existing files in a directory this class provides five different methods to get the details of all files in a particular folder −String[] list()File[] listFiles()String[] list(FilenameFilter filter)File[] listFiles(FilenameFilter filter)File[] listFiles(FileFilter filter)The ListFiles() methodThis method returns an array holding the objects (abstract paths) of all the files (and directories) in the path represented by the current (File) object.The following Java program prints the name, path and, size ... Read More

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

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

785 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

Advertisements