Found 4338 Articles for Java 8

FileOutputStream in Java.

Maruthi Krishna
Updated on 12-Sep-2019 06:42:20

242 Views

This writes data into a specific file or, a file descriptor (byte by byte). It is usually used to write the contents of a file with raw bytes, such as images.To write the contents of a file using this class −First of all, you need to instantiate this class by passing a string variable or a File object, representing the path of the file to be read.FileOutputStream outputStream = new FileOutputStream("file_path"); or, File file = new File("file_path"); FileOutputStream outputStream = new FileOutputStream (file);You can also instantiate a FileOutputStream class by passing a FileDescriptor object.FileDescriptor descriptor = new FileDescriptor(); FileOutputStream outputStream ... Read More

Writing data to a file using BufferedWriter class in Java

Maruthi Krishna
Updated on 12-Sep-2019 06:37:17

730 Views

The BufferedWriter class of Java is used to write a stream of characters to the specified destination (character-output stream). It initially stores all the characters in a buffer and pushes the contents of the buffer to the destination, making the writing of characters, arrays and Strings efficient.You can specify the required size of the buffer at the time of instantiating this class.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. Live Demoimport java.io.BufferedWriter; import java.io.IOException; import java.io.OutputStreamWriter; public class BufferedWriterExample ... Read More

BufferedReader class in Java.

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

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

511 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

370 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

How to write data to .csv file in Java?

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

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

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

975 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

505 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

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

Advertisements