Found 9321 Articles for Object Oriented Programming

How to check the Existence of a File using Java?

Monica Mona
Updated on 20-Feb-2020 08:27:51

96 Views

The file class provides a method named exists() which returns true if the file specified in the current file object exists.ExampleLive Demoimport java.io.File; public class FileHandling {    public static void main(String args[]) {       File file = new File("samplefile");       if(file.exists()) {          System.out.println("Given file existed");       } else {          System.out.println("Given file does not existed");      }    } }OutputGiven file does not existed

How to list all files in a directory using Java?

karthikeya Boyini
Updated on 20-Feb-2020 08:21:45

769 Views

You can get the list of files in a directory −Create a directory object using the File class.Get the list of directories in it using the getName() method.Exampleimport java.io.File; public class FindingDirectories {    public static void main(String args[]) {       String dir ="C:/Users/Tutorialspoint/Desktop/movies";       File directory = new File(dir);       File[] fileList = directory.listFiles();       for(File file: fileList) {          System.out.println(file.getName());       }    } }OutputArundhati HD.mp4 Okka Ammai Tappa.mkv Padamati Sandhya Ragam.mp4

What is the difference between System.out.println() and System.out.print() in Java?

Sharon Christine
Updated on 20-Feb-2020 08:19:37

1K+ Views

The println() terminates the current line by writing the line separator string. The print() method just prints the given content.ExampleLive Demopublic class Sample {    public static void main(String args[]) {       System.out.println("Hello");       System.out.println("how are you");       System.out.print("Hello");       System.out.print("how are you");    } }OutputHello how are you Hellohow are you

What is the difference between System.out, System.in and System.err streams in Java?

Swarali Sree
Updated on 30-Jul-2019 22:30:20

971 Views

All the programming languages provide support for standard I/O where the user's program can take input from a keyboard and then produce an output on the computer screen. Similarly, Java provides the following three standard streams:Standard Input: This is used to feed the data to user's program and usually a keyboard is used as a standard input stream and represented as System.in.Standard Output: This is used to output the data produced by the user's program and usually a computer screen is used for standard output stream and represented as System.out.Standard Error: This is used to output the error data produced ... Read More

What is the difference between Serialization and Deserialization in Java?

Samual Sam
Updated on 25-Feb-2020 10:28:26

2K+ Views

Serialization Java provides a mechanism, called object serialization where an object can be represented as a sequence of bytes that includes the object's data as well as information about the object's type and the types of data stored in the object.Exampleimport java.io.*; public class SerializeDemo {    public static void main(String [] args) {       Employee e = new Employee();       e.name = "Reyan Ali";       e.address = "Phokka Kuan, Ambehta Peer";       e.SSN = 11122333;       e.number = 101;       try {         ... Read More

What is Externalizable in Java?

Lakshmi Srinivas
Updated on 30-Jul-2019 22:30:20

129 Views

Externalization is used whenever we need to customize serialization mechanism. If a class implements an Externalizable interface then, object serialization will be done using writeExternal() method.Whereas at receiver’s end when an Externalizable object is a reconstructed instance will be created using no argument constructor and then the readExternal() method is called.If a class implements only Serializable interface object serialization will be done using ObjectoutputStream.At the receiver’s end, the serializable object is reconstructed using ObjectInputStream.

What is Deserialization in Java?

Monica Mona
Updated on 25-Feb-2020 10:30:46

199 Views

After a serialized object has been written into a file, it can be read from the file and Deserialized that is, the type information and bytes that represent the object and its data can be used to recreate the object in memory.Exampleimport java.io.*; public class DeserializeDemo {    public static void main(String [] args) {       Employee e = null;       try {          FileInputStream fileIn = new FileInputStream("/tmp/employee.ser");          ObjectInputStream in = new ObjectInputStream(fileIn);          e = (Employee) in.readObject();          in.close(); ... Read More

What is an I/O filter in Java?

Debarpito Sarkar
Updated on 05-Sep-2022 12:33:42

704 Views

This article will help you understand what I/O filter in Java is. The Java I/O Filter The Java I/O Filter is inside java.io package. It provides sets of input and output streams used for reading and writing data to input and output sources. There are different types of classes in java.io, naming Input Stream, Output Stream, etc. Some of the important types are discussed below − Input Stream The InputStream class of java.io is an abstract superclass which reads data from an input source. The source can be a file, a string, or anything that can contain data. This class ... Read More

What are I/O classes in Java?

Swarali Sree
Updated on 25-Feb-2020 10:31:37

3K+ Views

The java.io package contains nearly every class you might ever need to perform input and output (I/O) in Java. All these streams represent an input source and an output destination. The stream in the java.io package supports many data such as primitives, object, localized characters, etc.A stream can be defined as a sequence of data. There are two kinds of Streams −InputStream: The InputStream is used to read data from a source.OutputStream: The OutputStream is used for writing data to a destination.As described earlier, a stream can be defined as a sequence of data. The InputStream is used to read data from ... Read More

How to check if a file exists or not in Java?

Samual Sam
Updated on 30-Jul-2019 22:30:20

309 Views

The File class provides exists() method this returns true if a file in the specified path exists else it returns false.ExampleLive Demoimport java.io.File; public class FileHandling {    public static void main(String args[]) {       File file = new File("samplefile");       if(file.exists()) {          System.out.println("Given file existed");       } else {          System.out.println("Given file does not existed");       }    } }OutputGiven file does not existed

Advertisements