SlideShare una empresa de Scribd logo
1 de 28
Nadar Saraswathi College of Arts and
Science
Advanced Java
Programming
Input/Output
Exploring java.io
By
Nagapandiyammal N
Input / Output:
Exploringjava.io
INTRODUCTION
 Data is retrieved from an input source. The results
of a program are sent to an output destination.
 In Java, these sources or destinations are defined
very broadly.
 For example, a network connection, memory
buffer, or disk file can be manipulated by the Java
I/O classes.
The Java I/O Classes and
InterfacesThe I/O classes defined by java.io are listed here
BufferedInputStream FileWriter PipedOutputStream
BufferedOutputStream FilterInputStream PipedReader
BufferedReader FilterOutputStream PipedWriter
BufferedWriter FilterReader PrintStream
ByteArrayInputStream FilterWriter PrintWriter
ByteArrayOutputStream InputStream PushbackInputStream
CharArrayReader InputStreamReader PushbackReader
CharArrayWriter LineNumberReader RandomAccessFile
Console was added by Java SE 6.
 The java.io package also contains two deprecated classes that
are not shown in the preceding table: LineNumberInputStream
and StringBufferInputStream. These classes should not be
used for new code.
The following interfaces are defined by java.io:
Closeable FileFilter ObjectInputValidation
DataInput FilenameFilter ObjectOutput
DataOutput Flushable ObjectStreamConstant
Externalizable ObjectInput Serializable
File
 File object is used to obtain or manipulate the information associated with a disk
file, such as the permissions, time, date, and directory path, and to navigate
subdirectory hierarchies.
 A directory in Java is treated simply as a File with one additional property a list
of filenames that can be examined by the list( ) method.
The following constructors can be used to create File objects:
File(String directoryPath)
File(String directoryPath, String filename)
File(File dirObj, String filename)
File(URIuriObj)
 Here, directoryPath is the path name of the file, filename is the name of the file
or subdirectory, dirObj is a File object that specifies a directory, and uriObj is a
URI object that describes a file.
The following example demonstrates several of the File methods:
// Demonstrate File.
import java.io.File;
class FileDemo {
static void p(String s) {
System.out.println(s);
}
public static void main(String args[]) {
File f1 = new File("/java/COPYRIGHT");
p("File Name: " + f1.getName());
p("Path: " + f1.getPath());
p("Abs Path: " + f1.getAbsolutePath());
p("Parent: " + f1.getParent());
p(f1.exists() ? "exists" : "does not exist");
p(f1.canWrite() ? "is writeable" : "is not writeable");
p(f1.canRead() ? "is readable" : "is not readable");
p("is " + (f1.isDirectory() ? "" : "not" + " a directory"));
p(f1.isFile() ? "is normal file" : "might be a named pipe");
p(f1.isAbsolute() ? "is absolute" : "is not absolute");
p("File last modified: " + f1.lastModified());
p("File size: " + f1.length() + " Bytes");
}
}
Directories A directory is a File that contains a list of other files and directories. When you
create a File object and it is a directory, the isDirectory( ) method will return true.
In this case, you can call list( ) on that object to extract the list of other files and
directories inside. It has two forms.
// Using directories.
import java.io.File;
class DirList {
public static void main(String args[]) {
String dirname = "/java";
File f1 = new File(dirname);
if (f1.isDirectory()) {
System.out.println("Directory of " + dirname);
String s[] = f1.list();
for (int i=0; i < s.length; i++) {
File f = new File(dirname + "/" + s[i]);
if (f.isDirectory()) {
System.out.println(s[i] + " is a directory");
} else {
System.out.println(s[i] + " is a file");
}
}
} else {
System.out.println(dirname + " is not a directory");
}
}
}
Using FilenameFilter
 You will often want to limit the number of files returned by the list()
method to include only those files that match a certain filename
pattern, or filter.
 In this form, FFObj is an object of a class that implements the
FilenameFilter interface.
 FilenameFilter defines only a single method, accept( ), which is
called once for each file in a list.
 Its general form is given here:
boolean accept(File directory, String filename)
The listFiles( ) Alternative
 There is a variation to the list( ) method, called listFiles( ), which
you might find useful. The signatures for listFiles( ) are shown
here:
File[ ] listFiles( )
File[ ] listFiles(FilenameFilter FFObj)
File[ ] listFiles(FileFilter FObj)
• The first method returns all files, and the second returns those files
that satisfy the specified FilenameFilter.
 The third version of listFiles( ) returns those files with path names
that satisfy the specified FileFilter.
 FileFilter defines only a single method, accept( ), which is called
once for each file in a list.
Creating Directories
 Another two useful File utility methods are mkdir( )
and mkdirs( ).
 The mkdir( ) method creates a directory, returning true
on success and false on failure.
 Failure indicates that the path specified in the File object
already exists, or that the directory cannot be created
because the entire path does not exist yet.
 To create a directory for which no path exists, use the
mkdirs( ) method. It creates both a directory and all the
parents of the directory.
The Closeable and Flushable Interfaces
 Recently (with the release of JDK 5), two interfaces were added to
java.io: Closeable and Flushable.
 The interfaces are implemented by several of the I/O classes. Their
inclusion does not add new functionality to the stream classes.
 They simply offer a uniform way of specifying that a stream can be
closed or flushed.
Objects of a class that implements Closeable can be closed.
It defines the close( ) method, shown here:
void close( ) throws IOException
void flush( ) throws IOException
 Flushing a stream typically causes buffered output to be physically
written to the underlying device. This interface is implemented by
all of the I/O classes that write to a stream.
The Stream Classes
 Java's stream-based I/O is built upon four abstract
classes: InputStream, OutputStream, Reader, and
Writer.
 InputStream and OutputStream are designed for
byte streams. Reader and Writer are designed for
character streams.
 In general, you should use the character stream
classes when working with characters or strings, and
use the byte stream classes when working with
bytes or other binary objects.
The Byte Streams
 The byte stream classes provide a rich environment for handling byte-
oriented I/O. A byte stream can be used with any type of object,
including binary data.
 Since the byte stream classes are topped by InputStream and
OutputStream, our discussion will begin with them.
InputStream :
 InputStream is an abstract class that defines Java's model of streaming
byte input. It implements the Closeable interface. Most of the methods
in this class will throw an IOException on error conditions.
OutputStream :
 OutputStream is an abstract class that defines streaming byte output. It
implements the Closeable and Flushable interfaces. Most of the
methods in this class return void and throw an IOException in the case
of errors.
FileInputStream
 The FileInputStream class creates an InputStream that you can
use to read bytes from a file. Its two most common constructors are
shown here:
FileInputStream(String filepath)
FileInputStream(File fileObj)
 Either can throw a FileNotFoundException. Here, filepath is the
full path name of a file, and fileObj is a File object that describes
the file.
FileOutputStream
 FileOutputStream creates an OutputStream that you can use to
write bytes to a file. Its most commonly used constructors are
shown here:
FileOutputStream(String filePath)
FileOutputStream(File fileObj)
FileOutputStream(String filePath, boolean append)
FileOutputStream(File fileObj, boolean append)
ByteArrayInputStream
 ByteArrayInputStream is an implementation of an
input stream that uses a byte array as the source. This
class has two constructors, each of which requires a byte
array to provide the data source:
ByteArrayInputStream(byte array[ ])
ByteArrayInputStream(byte array[ ], int start, int numBytes)
 Here, array is the input source. The second constructor
creates an InputStream from a subset of your byte array
that begins with the character at the index specified by
start and is numBytes long.
ByteArrayOutputStream
 ByteArrayOutputStream is an implementation of an
output stream that uses a byte array as the destination.
ByteArrayOutputStream has two constructors, shown
here:
ByteArrayOutputStream( )
ByteArrayOutputStream(int numBytes)
 In the first form, a buffer of 32 bytes is created. In the
second, a buffer is created with a size equal to that
specified by numBytes.
 The buffer size will be increased automatically, if
needed. The number of bytes held by the buffer is
contained in the protected count field of
ByteArrayOutputStream.
Filtered Byte Streams
 Filtered streams are simply wrappers around underlying
input or output streams that transparently provide some
extended level of functionality.
 Typical extensions are buffering, character translation,
and raw data translation. The filtered byte streams are
FilterInputStream and FilterOutputStream. Their
constructors are shown here:
FilterOutputStream(OutputStream os)
FilterInputStream(InputStream is)
 The methods provided in these classes are identical to
those in InputStream and OutputStream
Buffered Byte Streams
 For the byte-oriented streams, a buffered stream extends
a filtered stream class by attaching a memory buffer to
the I/O streams.
 This buffer allows Java to do I/O operations on more
than a byte at a time, hence increasing performance.
Because the buffer is available, skipping, marking, and
resetting of the stream become possible.
 The buffered byte stream classes are
BufferedInputStream and BufferedOutputStream.
PushbackInputStream also implements a buffered
stream.
BufferedInputStream
 Buffering I/O is a very common performance
optimization. Java's BufferedInputStream class allows
you to "wrap" any InputStream into a buffered stream
and achieve this performance improvement.
BufferedInputStream has two constructors:
BufferedInputStream(InputStream inputStream)
BufferedInputStream(InputStream , int bufSize)
BufferedOutputStream
 A BufferedOutputStream is similar to any
OutputStream with the exception of an added flush( )
method that is used to ensure that data buffers are
physically written to the actual output device.
Here are the two available constructors:
 BufferedOutputStream(OutputStream outputStream)
 BufferedOutputStream(OutputStream , int bufSize)
 The first form creates a buffered stream using the default
buffer size. In the second form, the size of the buffer is
passed in bufSize.
The Character Streams
 While the byte stream classes provide sufficient
functionality to handle any type of I/O operation,
they cannot work directly with Unicode characters.
 Since one of the main purposes of Java is to support
the "write once, run anywhere" philosophy, it was
necessary to include direct I/O support for
characters.
 In this section, several of the character I/O classes
are discussed. As explained earlier, at the top of the
character stream hierarchies are the Reader and
Writer abstract classes.
Reader :
Reader is an abstract class that defines Java's model
of streaming character input. It implements the Closeable
and Readable interfaces. All of the methods in this class
(except for markSupported()) will throw an IOException
on error conditions.
Writer :
Writer is an abstract class that defines streaming
character output. It implements the Closeable, Flushable,
and Appendable interfaces. All of the methods in this
class throw an IOException in the case of errors.
FileReader :
The FileReader class creates a Reader that you can use
to read the contents of a file.
FileReader(String filePath)
FileReader(File fileObj)
Either can throw a FileNotFoundException. Here,
filePath is the full path name of a file, and fileObj is a File
object that describes the file.
FileWriter :
FileWriter creates a Writer that you can use to write to
a file.
FileWriter(String filePath)
FileWriter(String filePath, boolean append)
FileWriter(File fileObj)
FileWriter(File fileObj, boolean append)
Serialization
Serializable :
 Only an object that implements the Serializable
interface can be saved and restored by the serialization
facilities.
 The Serializable interface defines no members. It is
simply used to indicate that a class may be serialized. If
a class is serializable, all of its subclasses are also
serializable.
Externalizable
 The Java facilities for serialization and deserialization
have been designed so that much of the work to save and
restore the state of an object occurs automatically.
 For example, it may be desirable to use compression or
encryption techniques. The Externalizable interface is
designed for these situations.
 The Externalizable interface defines these two methods:
void readExternal(ObjectInput inStream)throws IOException,
ClassNotFoundException
void writeExternal(ObjectOutput outStream)throws IOException
Stream Benefits
 Java programs written to adhere to the abstract,
high-level InputStream, OutputStream, Reader,
and Writer classes will function properly in the
future even when new and improved concrete
stream classes are invented.
 As you will see in the next chapter, this model
works very well when we switch from a file system
based set of streams to the network and socket
streams.
 Finally, serialization of objects plays an important
role in many types of Java programs.
 Java's serialization I/O classes provide a portable
solution to this sometimes tricky task.
THE END

Más contenido relacionado

La actualidad más candente

Intermediate code generator
Intermediate code generatorIntermediate code generator
Intermediate code generatorsanchi29
 
Object-oriented Programming-with C#
Object-oriented Programming-with C#Object-oriented Programming-with C#
Object-oriented Programming-with C#Doncho Minkov
 
Class, object and inheritance in python
Class, object and inheritance in pythonClass, object and inheritance in python
Class, object and inheritance in pythonSantosh Verma
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++Vishal Patil
 
Classes, Objects and Method - Object Oriented Programming with Java
Classes, Objects and Method - Object Oriented Programming with JavaClasses, Objects and Method - Object Oriented Programming with Java
Classes, Objects and Method - Object Oriented Programming with JavaRadhika Talaviya
 
Java Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingJava Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingNithyaN19
 
Memory allocation in c
Memory allocation in cMemory allocation in c
Memory allocation in cPrabhu Govind
 
Polymorphism in java
Polymorphism in java Polymorphism in java
Polymorphism in java Janu Jahnavi
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).pptAlok Kumar
 
Intermediate code generation1
Intermediate code generation1Intermediate code generation1
Intermediate code generation1Shashwat Shriparv
 
Exception Handling
Exception HandlingException Handling
Exception HandlingReddhi Basu
 

La actualidad más candente (20)

Intermediate code generator
Intermediate code generatorIntermediate code generator
Intermediate code generator
 
Python - object oriented
Python - object orientedPython - object oriented
Python - object oriented
 
Advance oops concepts
Advance oops conceptsAdvance oops concepts
Advance oops concepts
 
Object-oriented Programming-with C#
Object-oriented Programming-with C#Object-oriented Programming-with C#
Object-oriented Programming-with C#
 
I/O Streams
I/O StreamsI/O Streams
I/O Streams
 
Access modifiers in java
Access modifiers in javaAccess modifiers in java
Access modifiers in java
 
Class, object and inheritance in python
Class, object and inheritance in pythonClass, object and inheritance in python
Class, object and inheritance in python
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Classes, Objects and Method - Object Oriented Programming with Java
Classes, Objects and Method - Object Oriented Programming with JavaClasses, Objects and Method - Object Oriented Programming with Java
Classes, Objects and Method - Object Oriented Programming with Java
 
Java Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingJava Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overriding
 
Memory allocation in c
Memory allocation in cMemory allocation in c
Memory allocation in c
 
Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
 
Method overriding
Method overridingMethod overriding
Method overriding
 
Threads in JAVA
Threads in JAVAThreads in JAVA
Threads in JAVA
 
Polymorphism in java
Polymorphism in java Polymorphism in java
Polymorphism in java
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).ppt
 
Intermediate code generation1
Intermediate code generation1Intermediate code generation1
Intermediate code generation1
 
Exception Handling
Exception HandlingException Handling
Exception Handling
 
Program activation records
Program activation recordsProgram activation records
Program activation records
 

Similar a Input/Output Exploring java.io

Input output files in java
Input output files in javaInput output files in java
Input output files in javaKavitha713564
 
Itp 120 Chapt 19 2009 Binary Input & Output
Itp 120 Chapt 19 2009 Binary Input & OutputItp 120 Chapt 19 2009 Binary Input & Output
Itp 120 Chapt 19 2009 Binary Input & Outputphanleson
 
File Handling in Java.pdf
File Handling in Java.pdfFile Handling in Java.pdf
File Handling in Java.pdfSudhanshiBakre1
 
Java program file I/O
Java program file I/OJava program file I/O
Java program file I/ONem Sothea
 
File Input and output.pptx
File Input  and output.pptxFile Input  and output.pptx
File Input and output.pptxcherryreddygannu
 
Java - Processing input and output
Java - Processing input and outputJava - Processing input and output
Java - Processing input and outputRiccardo Cardin
 
Jedi Slides Intro2 Chapter12 Advanced Io Streams
Jedi Slides Intro2 Chapter12 Advanced Io StreamsJedi Slides Intro2 Chapter12 Advanced Io Streams
Jedi Slides Intro2 Chapter12 Advanced Io StreamsDon Bosco BSIT
 
Java - File Input Output Concepts
Java - File Input Output ConceptsJava - File Input Output Concepts
Java - File Input Output ConceptsVicter Paul
 
What is java input and output stream?
What is java input and output stream?What is java input and output stream?
What is java input and output stream?kanchanmahajan23
 
What is java input and output stream?
What is java input and output stream?What is java input and output stream?
What is java input and output stream?kanchanmahajan23
 
Basic i/o & file handling in java
Basic i/o & file handling in javaBasic i/o & file handling in java
Basic i/o & file handling in javaJayasankarPR2
 
Unit No 5 Files and Database Connectivity.pptx
Unit No 5 Files and Database Connectivity.pptxUnit No 5 Files and Database Connectivity.pptx
Unit No 5 Files and Database Connectivity.pptxDrYogeshDeshmukh1
 
Files and streams In Java
Files and streams In JavaFiles and streams In Java
Files and streams In JavaRajan Shah
 

Similar a Input/Output Exploring java.io (20)

Java I/o streams
Java I/o streamsJava I/o streams
Java I/o streams
 
Java Day-6
Java Day-6Java Day-6
Java Day-6
 
Input output files in java
Input output files in javaInput output files in java
Input output files in java
 
Itp 120 Chapt 19 2009 Binary Input & Output
Itp 120 Chapt 19 2009 Binary Input & OutputItp 120 Chapt 19 2009 Binary Input & Output
Itp 120 Chapt 19 2009 Binary Input & Output
 
File Handling in Java.pdf
File Handling in Java.pdfFile Handling in Java.pdf
File Handling in Java.pdf
 
Java program file I/O
Java program file I/OJava program file I/O
Java program file I/O
 
File Input and output.pptx
File Input  and output.pptxFile Input  and output.pptx
File Input and output.pptx
 
Java - Processing input and output
Java - Processing input and outputJava - Processing input and output
Java - Processing input and output
 
Jedi Slides Intro2 Chapter12 Advanced Io Streams
Jedi Slides Intro2 Chapter12 Advanced Io StreamsJedi Slides Intro2 Chapter12 Advanced Io Streams
Jedi Slides Intro2 Chapter12 Advanced Io Streams
 
Java
JavaJava
Java
 
5java Io
5java Io5java Io
5java Io
 
Java - File Input Output Concepts
Java - File Input Output ConceptsJava - File Input Output Concepts
Java - File Input Output Concepts
 
What is java input and output stream?
What is java input and output stream?What is java input and output stream?
What is java input and output stream?
 
What is java input and output stream?
What is java input and output stream?What is java input and output stream?
What is java input and output stream?
 
Basic i/o & file handling in java
Basic i/o & file handling in javaBasic i/o & file handling in java
Basic i/o & file handling in java
 
Unit No 5 Files and Database Connectivity.pptx
Unit No 5 Files and Database Connectivity.pptxUnit No 5 Files and Database Connectivity.pptx
Unit No 5 Files and Database Connectivity.pptx
 
Io Streams
Io StreamsIo Streams
Io Streams
 
Files and streams In Java
Files and streams In JavaFiles and streams In Java
Files and streams In Java
 
IOStream.pptx
IOStream.pptxIOStream.pptx
IOStream.pptx
 
Java I/O
Java I/OJava I/O
Java I/O
 

Más de NilaNila16

Basic Block Scheduling
Basic Block SchedulingBasic Block Scheduling
Basic Block SchedulingNilaNila16
 
Affine Array Indexes
Affine Array IndexesAffine Array Indexes
Affine Array IndexesNilaNila16
 
Software Engineering
Software EngineeringSoftware Engineering
Software EngineeringNilaNila16
 
Web Programming
Web ProgrammingWeb Programming
Web ProgrammingNilaNila16
 
MapReduce Paradigm
MapReduce ParadigmMapReduce Paradigm
MapReduce ParadigmNilaNila16
 
Hadoop Distributed File System
Hadoop Distributed File SystemHadoop Distributed File System
Hadoop Distributed File SystemNilaNila16
 
Operating system
Operating systemOperating system
Operating systemNilaNila16
 
Linear Block Codes
Linear Block CodesLinear Block Codes
Linear Block CodesNilaNila16
 
Applications of graph theory
                      Applications of graph theory                      Applications of graph theory
Applications of graph theoryNilaNila16
 
Recurrence Relation
Recurrence RelationRecurrence Relation
Recurrence RelationNilaNila16
 

Más de NilaNila16 (14)

Basic Block Scheduling
Basic Block SchedulingBasic Block Scheduling
Basic Block Scheduling
 
Affine Array Indexes
Affine Array IndexesAffine Array Indexes
Affine Array Indexes
 
Software Engineering
Software EngineeringSoftware Engineering
Software Engineering
 
Web Programming
Web ProgrammingWeb Programming
Web Programming
 
MapReduce Paradigm
MapReduce ParadigmMapReduce Paradigm
MapReduce Paradigm
 
Hadoop Distributed File System
Hadoop Distributed File SystemHadoop Distributed File System
Hadoop Distributed File System
 
Data Mining
Data MiningData Mining
Data Mining
 
Operating system
Operating systemOperating system
Operating system
 
RDBMS
RDBMSRDBMS
RDBMS
 
Linear Block Codes
Linear Block CodesLinear Block Codes
Linear Block Codes
 
Applications of graph theory
                      Applications of graph theory                      Applications of graph theory
Applications of graph theory
 
Hasse Diagram
Hasse DiagramHasse Diagram
Hasse Diagram
 
Fuzzy set
Fuzzy set Fuzzy set
Fuzzy set
 
Recurrence Relation
Recurrence RelationRecurrence Relation
Recurrence Relation
 

Último

Andrés Ramírez Gossler, Facundo Schinnea - eCommerce Day Chile 2024
Andrés Ramírez Gossler, Facundo Schinnea - eCommerce Day Chile 2024Andrés Ramírez Gossler, Facundo Schinnea - eCommerce Day Chile 2024
Andrés Ramírez Gossler, Facundo Schinnea - eCommerce Day Chile 2024eCommerce Institute
 
BDSM⚡Call Girls in Sector 93 Noida Escorts >༒8448380779 Escort Service
BDSM⚡Call Girls in Sector 93 Noida Escorts >༒8448380779 Escort ServiceBDSM⚡Call Girls in Sector 93 Noida Escorts >༒8448380779 Escort Service
BDSM⚡Call Girls in Sector 93 Noida Escorts >༒8448380779 Escort ServiceDelhi Call girls
 
Presentation on Engagement in Book Clubs
Presentation on Engagement in Book ClubsPresentation on Engagement in Book Clubs
Presentation on Engagement in Book Clubssamaasim06
 
CTAC 2024 Valencia - Henrik Hanke - Reduce to the max - slideshare.pdf
CTAC 2024 Valencia - Henrik Hanke - Reduce to the max - slideshare.pdfCTAC 2024 Valencia - Henrik Hanke - Reduce to the max - slideshare.pdf
CTAC 2024 Valencia - Henrik Hanke - Reduce to the max - slideshare.pdfhenrik385807
 
Thirunelveli call girls Tamil escorts 7877702510
Thirunelveli call girls Tamil escorts 7877702510Thirunelveli call girls Tamil escorts 7877702510
Thirunelveli call girls Tamil escorts 7877702510Vipesco
 
VVIP Call Girls Nalasopara : 9892124323, Call Girls in Nalasopara Services
VVIP Call Girls Nalasopara : 9892124323, Call Girls in Nalasopara ServicesVVIP Call Girls Nalasopara : 9892124323, Call Girls in Nalasopara Services
VVIP Call Girls Nalasopara : 9892124323, Call Girls in Nalasopara ServicesPooja Nehwal
 
ANCHORING SCRIPT FOR A CULTURAL EVENT.docx
ANCHORING SCRIPT FOR A CULTURAL EVENT.docxANCHORING SCRIPT FOR A CULTURAL EVENT.docx
ANCHORING SCRIPT FOR A CULTURAL EVENT.docxNikitaBankoti2
 
Introduction to Prompt Engineering (Focusing on ChatGPT)
Introduction to Prompt Engineering (Focusing on ChatGPT)Introduction to Prompt Engineering (Focusing on ChatGPT)
Introduction to Prompt Engineering (Focusing on ChatGPT)Chameera Dedduwage
 
No Advance 8868886958 Chandigarh Call Girls , Indian Call Girls For Full Nigh...
No Advance 8868886958 Chandigarh Call Girls , Indian Call Girls For Full Nigh...No Advance 8868886958 Chandigarh Call Girls , Indian Call Girls For Full Nigh...
No Advance 8868886958 Chandigarh Call Girls , Indian Call Girls For Full Nigh...Sheetaleventcompany
 
Chiulli_Aurora_Oman_Raffaele_Beowulf.pptx
Chiulli_Aurora_Oman_Raffaele_Beowulf.pptxChiulli_Aurora_Oman_Raffaele_Beowulf.pptx
Chiulli_Aurora_Oman_Raffaele_Beowulf.pptxraffaeleoman
 
CTAC 2024 Valencia - Sven Zoelle - Most Crucial Invest to Digitalisation_slid...
CTAC 2024 Valencia - Sven Zoelle - Most Crucial Invest to Digitalisation_slid...CTAC 2024 Valencia - Sven Zoelle - Most Crucial Invest to Digitalisation_slid...
CTAC 2024 Valencia - Sven Zoelle - Most Crucial Invest to Digitalisation_slid...henrik385807
 
Governance and Nation-Building in Nigeria: Some Reflections on Options for Po...
Governance and Nation-Building in Nigeria: Some Reflections on Options for Po...Governance and Nation-Building in Nigeria: Some Reflections on Options for Po...
Governance and Nation-Building in Nigeria: Some Reflections on Options for Po...Kayode Fayemi
 
Night 7k Call Girls Noida Sector 128 Call Me: 8448380779
Night 7k Call Girls Noida Sector 128 Call Me: 8448380779Night 7k Call Girls Noida Sector 128 Call Me: 8448380779
Night 7k Call Girls Noida Sector 128 Call Me: 8448380779Delhi Call girls
 
Re-membering the Bard: Revisiting The Compleat Wrks of Wllm Shkspr (Abridged)...
Re-membering the Bard: Revisiting The Compleat Wrks of Wllm Shkspr (Abridged)...Re-membering the Bard: Revisiting The Compleat Wrks of Wllm Shkspr (Abridged)...
Re-membering the Bard: Revisiting The Compleat Wrks of Wllm Shkspr (Abridged)...Hasting Chen
 
Mathematics of Finance Presentation.pptx
Mathematics of Finance Presentation.pptxMathematics of Finance Presentation.pptx
Mathematics of Finance Presentation.pptxMoumonDas2
 
Mohammad_Alnahdi_Oral_Presentation_Assignment.pptx
Mohammad_Alnahdi_Oral_Presentation_Assignment.pptxMohammad_Alnahdi_Oral_Presentation_Assignment.pptx
Mohammad_Alnahdi_Oral_Presentation_Assignment.pptxmohammadalnahdi22
 
Call Girl Number in Khar Mumbai📲 9892124323 💞 Full Night Enjoy
Call Girl Number in Khar Mumbai📲 9892124323 💞 Full Night EnjoyCall Girl Number in Khar Mumbai📲 9892124323 💞 Full Night Enjoy
Call Girl Number in Khar Mumbai📲 9892124323 💞 Full Night EnjoyPooja Nehwal
 
If this Giant Must Walk: A Manifesto for a New Nigeria
If this Giant Must Walk: A Manifesto for a New NigeriaIf this Giant Must Walk: A Manifesto for a New Nigeria
If this Giant Must Walk: A Manifesto for a New NigeriaKayode Fayemi
 
Navi Mumbai Call Girls Service Pooja 9892124323 Real Russian Girls Looking Mo...
Navi Mumbai Call Girls Service Pooja 9892124323 Real Russian Girls Looking Mo...Navi Mumbai Call Girls Service Pooja 9892124323 Real Russian Girls Looking Mo...
Navi Mumbai Call Girls Service Pooja 9892124323 Real Russian Girls Looking Mo...Pooja Nehwal
 
WhatsApp 📞 9892124323 ✅Call Girls In Juhu ( Mumbai )
WhatsApp 📞 9892124323 ✅Call Girls In Juhu ( Mumbai )WhatsApp 📞 9892124323 ✅Call Girls In Juhu ( Mumbai )
WhatsApp 📞 9892124323 ✅Call Girls In Juhu ( Mumbai )Pooja Nehwal
 

Último (20)

Andrés Ramírez Gossler, Facundo Schinnea - eCommerce Day Chile 2024
Andrés Ramírez Gossler, Facundo Schinnea - eCommerce Day Chile 2024Andrés Ramírez Gossler, Facundo Schinnea - eCommerce Day Chile 2024
Andrés Ramírez Gossler, Facundo Schinnea - eCommerce Day Chile 2024
 
BDSM⚡Call Girls in Sector 93 Noida Escorts >༒8448380779 Escort Service
BDSM⚡Call Girls in Sector 93 Noida Escorts >༒8448380779 Escort ServiceBDSM⚡Call Girls in Sector 93 Noida Escorts >༒8448380779 Escort Service
BDSM⚡Call Girls in Sector 93 Noida Escorts >༒8448380779 Escort Service
 
Presentation on Engagement in Book Clubs
Presentation on Engagement in Book ClubsPresentation on Engagement in Book Clubs
Presentation on Engagement in Book Clubs
 
CTAC 2024 Valencia - Henrik Hanke - Reduce to the max - slideshare.pdf
CTAC 2024 Valencia - Henrik Hanke - Reduce to the max - slideshare.pdfCTAC 2024 Valencia - Henrik Hanke - Reduce to the max - slideshare.pdf
CTAC 2024 Valencia - Henrik Hanke - Reduce to the max - slideshare.pdf
 
Thirunelveli call girls Tamil escorts 7877702510
Thirunelveli call girls Tamil escorts 7877702510Thirunelveli call girls Tamil escorts 7877702510
Thirunelveli call girls Tamil escorts 7877702510
 
VVIP Call Girls Nalasopara : 9892124323, Call Girls in Nalasopara Services
VVIP Call Girls Nalasopara : 9892124323, Call Girls in Nalasopara ServicesVVIP Call Girls Nalasopara : 9892124323, Call Girls in Nalasopara Services
VVIP Call Girls Nalasopara : 9892124323, Call Girls in Nalasopara Services
 
ANCHORING SCRIPT FOR A CULTURAL EVENT.docx
ANCHORING SCRIPT FOR A CULTURAL EVENT.docxANCHORING SCRIPT FOR A CULTURAL EVENT.docx
ANCHORING SCRIPT FOR A CULTURAL EVENT.docx
 
Introduction to Prompt Engineering (Focusing on ChatGPT)
Introduction to Prompt Engineering (Focusing on ChatGPT)Introduction to Prompt Engineering (Focusing on ChatGPT)
Introduction to Prompt Engineering (Focusing on ChatGPT)
 
No Advance 8868886958 Chandigarh Call Girls , Indian Call Girls For Full Nigh...
No Advance 8868886958 Chandigarh Call Girls , Indian Call Girls For Full Nigh...No Advance 8868886958 Chandigarh Call Girls , Indian Call Girls For Full Nigh...
No Advance 8868886958 Chandigarh Call Girls , Indian Call Girls For Full Nigh...
 
Chiulli_Aurora_Oman_Raffaele_Beowulf.pptx
Chiulli_Aurora_Oman_Raffaele_Beowulf.pptxChiulli_Aurora_Oman_Raffaele_Beowulf.pptx
Chiulli_Aurora_Oman_Raffaele_Beowulf.pptx
 
CTAC 2024 Valencia - Sven Zoelle - Most Crucial Invest to Digitalisation_slid...
CTAC 2024 Valencia - Sven Zoelle - Most Crucial Invest to Digitalisation_slid...CTAC 2024 Valencia - Sven Zoelle - Most Crucial Invest to Digitalisation_slid...
CTAC 2024 Valencia - Sven Zoelle - Most Crucial Invest to Digitalisation_slid...
 
Governance and Nation-Building in Nigeria: Some Reflections on Options for Po...
Governance and Nation-Building in Nigeria: Some Reflections on Options for Po...Governance and Nation-Building in Nigeria: Some Reflections on Options for Po...
Governance and Nation-Building in Nigeria: Some Reflections on Options for Po...
 
Night 7k Call Girls Noida Sector 128 Call Me: 8448380779
Night 7k Call Girls Noida Sector 128 Call Me: 8448380779Night 7k Call Girls Noida Sector 128 Call Me: 8448380779
Night 7k Call Girls Noida Sector 128 Call Me: 8448380779
 
Re-membering the Bard: Revisiting The Compleat Wrks of Wllm Shkspr (Abridged)...
Re-membering the Bard: Revisiting The Compleat Wrks of Wllm Shkspr (Abridged)...Re-membering the Bard: Revisiting The Compleat Wrks of Wllm Shkspr (Abridged)...
Re-membering the Bard: Revisiting The Compleat Wrks of Wllm Shkspr (Abridged)...
 
Mathematics of Finance Presentation.pptx
Mathematics of Finance Presentation.pptxMathematics of Finance Presentation.pptx
Mathematics of Finance Presentation.pptx
 
Mohammad_Alnahdi_Oral_Presentation_Assignment.pptx
Mohammad_Alnahdi_Oral_Presentation_Assignment.pptxMohammad_Alnahdi_Oral_Presentation_Assignment.pptx
Mohammad_Alnahdi_Oral_Presentation_Assignment.pptx
 
Call Girl Number in Khar Mumbai📲 9892124323 💞 Full Night Enjoy
Call Girl Number in Khar Mumbai📲 9892124323 💞 Full Night EnjoyCall Girl Number in Khar Mumbai📲 9892124323 💞 Full Night Enjoy
Call Girl Number in Khar Mumbai📲 9892124323 💞 Full Night Enjoy
 
If this Giant Must Walk: A Manifesto for a New Nigeria
If this Giant Must Walk: A Manifesto for a New NigeriaIf this Giant Must Walk: A Manifesto for a New Nigeria
If this Giant Must Walk: A Manifesto for a New Nigeria
 
Navi Mumbai Call Girls Service Pooja 9892124323 Real Russian Girls Looking Mo...
Navi Mumbai Call Girls Service Pooja 9892124323 Real Russian Girls Looking Mo...Navi Mumbai Call Girls Service Pooja 9892124323 Real Russian Girls Looking Mo...
Navi Mumbai Call Girls Service Pooja 9892124323 Real Russian Girls Looking Mo...
 
WhatsApp 📞 9892124323 ✅Call Girls In Juhu ( Mumbai )
WhatsApp 📞 9892124323 ✅Call Girls In Juhu ( Mumbai )WhatsApp 📞 9892124323 ✅Call Girls In Juhu ( Mumbai )
WhatsApp 📞 9892124323 ✅Call Girls In Juhu ( Mumbai )
 

Input/Output Exploring java.io

  • 1. Nadar Saraswathi College of Arts and Science Advanced Java Programming Input/Output Exploring java.io By Nagapandiyammal N
  • 3. INTRODUCTION  Data is retrieved from an input source. The results of a program are sent to an output destination.  In Java, these sources or destinations are defined very broadly.  For example, a network connection, memory buffer, or disk file can be manipulated by the Java I/O classes.
  • 4. The Java I/O Classes and InterfacesThe I/O classes defined by java.io are listed here BufferedInputStream FileWriter PipedOutputStream BufferedOutputStream FilterInputStream PipedReader BufferedReader FilterOutputStream PipedWriter BufferedWriter FilterReader PrintStream ByteArrayInputStream FilterWriter PrintWriter ByteArrayOutputStream InputStream PushbackInputStream CharArrayReader InputStreamReader PushbackReader CharArrayWriter LineNumberReader RandomAccessFile
  • 5. Console was added by Java SE 6.  The java.io package also contains two deprecated classes that are not shown in the preceding table: LineNumberInputStream and StringBufferInputStream. These classes should not be used for new code. The following interfaces are defined by java.io: Closeable FileFilter ObjectInputValidation DataInput FilenameFilter ObjectOutput DataOutput Flushable ObjectStreamConstant Externalizable ObjectInput Serializable
  • 6. File  File object is used to obtain or manipulate the information associated with a disk file, such as the permissions, time, date, and directory path, and to navigate subdirectory hierarchies.  A directory in Java is treated simply as a File with one additional property a list of filenames that can be examined by the list( ) method. The following constructors can be used to create File objects: File(String directoryPath) File(String directoryPath, String filename) File(File dirObj, String filename) File(URIuriObj)  Here, directoryPath is the path name of the file, filename is the name of the file or subdirectory, dirObj is a File object that specifies a directory, and uriObj is a URI object that describes a file.
  • 7. The following example demonstrates several of the File methods: // Demonstrate File. import java.io.File; class FileDemo { static void p(String s) { System.out.println(s); } public static void main(String args[]) { File f1 = new File("/java/COPYRIGHT"); p("File Name: " + f1.getName()); p("Path: " + f1.getPath()); p("Abs Path: " + f1.getAbsolutePath()); p("Parent: " + f1.getParent()); p(f1.exists() ? "exists" : "does not exist"); p(f1.canWrite() ? "is writeable" : "is not writeable"); p(f1.canRead() ? "is readable" : "is not readable"); p("is " + (f1.isDirectory() ? "" : "not" + " a directory")); p(f1.isFile() ? "is normal file" : "might be a named pipe"); p(f1.isAbsolute() ? "is absolute" : "is not absolute"); p("File last modified: " + f1.lastModified()); p("File size: " + f1.length() + " Bytes"); } }
  • 8. Directories A directory is a File that contains a list of other files and directories. When you create a File object and it is a directory, the isDirectory( ) method will return true. In this case, you can call list( ) on that object to extract the list of other files and directories inside. It has two forms. // Using directories. import java.io.File; class DirList { public static void main(String args[]) { String dirname = "/java"; File f1 = new File(dirname); if (f1.isDirectory()) { System.out.println("Directory of " + dirname); String s[] = f1.list(); for (int i=0; i < s.length; i++) { File f = new File(dirname + "/" + s[i]); if (f.isDirectory()) { System.out.println(s[i] + " is a directory"); } else { System.out.println(s[i] + " is a file"); } } } else { System.out.println(dirname + " is not a directory"); } } }
  • 9. Using FilenameFilter  You will often want to limit the number of files returned by the list() method to include only those files that match a certain filename pattern, or filter.  In this form, FFObj is an object of a class that implements the FilenameFilter interface.  FilenameFilter defines only a single method, accept( ), which is called once for each file in a list.  Its general form is given here: boolean accept(File directory, String filename)
  • 10. The listFiles( ) Alternative  There is a variation to the list( ) method, called listFiles( ), which you might find useful. The signatures for listFiles( ) are shown here: File[ ] listFiles( ) File[ ] listFiles(FilenameFilter FFObj) File[ ] listFiles(FileFilter FObj) • The first method returns all files, and the second returns those files that satisfy the specified FilenameFilter.  The third version of listFiles( ) returns those files with path names that satisfy the specified FileFilter.  FileFilter defines only a single method, accept( ), which is called once for each file in a list.
  • 11. Creating Directories  Another two useful File utility methods are mkdir( ) and mkdirs( ).  The mkdir( ) method creates a directory, returning true on success and false on failure.  Failure indicates that the path specified in the File object already exists, or that the directory cannot be created because the entire path does not exist yet.  To create a directory for which no path exists, use the mkdirs( ) method. It creates both a directory and all the parents of the directory.
  • 12. The Closeable and Flushable Interfaces  Recently (with the release of JDK 5), two interfaces were added to java.io: Closeable and Flushable.  The interfaces are implemented by several of the I/O classes. Their inclusion does not add new functionality to the stream classes.  They simply offer a uniform way of specifying that a stream can be closed or flushed. Objects of a class that implements Closeable can be closed. It defines the close( ) method, shown here: void close( ) throws IOException void flush( ) throws IOException  Flushing a stream typically causes buffered output to be physically written to the underlying device. This interface is implemented by all of the I/O classes that write to a stream.
  • 13. The Stream Classes  Java's stream-based I/O is built upon four abstract classes: InputStream, OutputStream, Reader, and Writer.  InputStream and OutputStream are designed for byte streams. Reader and Writer are designed for character streams.  In general, you should use the character stream classes when working with characters or strings, and use the byte stream classes when working with bytes or other binary objects.
  • 14. The Byte Streams  The byte stream classes provide a rich environment for handling byte- oriented I/O. A byte stream can be used with any type of object, including binary data.  Since the byte stream classes are topped by InputStream and OutputStream, our discussion will begin with them. InputStream :  InputStream is an abstract class that defines Java's model of streaming byte input. It implements the Closeable interface. Most of the methods in this class will throw an IOException on error conditions. OutputStream :  OutputStream is an abstract class that defines streaming byte output. It implements the Closeable and Flushable interfaces. Most of the methods in this class return void and throw an IOException in the case of errors.
  • 15. FileInputStream  The FileInputStream class creates an InputStream that you can use to read bytes from a file. Its two most common constructors are shown here: FileInputStream(String filepath) FileInputStream(File fileObj)  Either can throw a FileNotFoundException. Here, filepath is the full path name of a file, and fileObj is a File object that describes the file. FileOutputStream  FileOutputStream creates an OutputStream that you can use to write bytes to a file. Its most commonly used constructors are shown here: FileOutputStream(String filePath) FileOutputStream(File fileObj) FileOutputStream(String filePath, boolean append) FileOutputStream(File fileObj, boolean append)
  • 16. ByteArrayInputStream  ByteArrayInputStream is an implementation of an input stream that uses a byte array as the source. This class has two constructors, each of which requires a byte array to provide the data source: ByteArrayInputStream(byte array[ ]) ByteArrayInputStream(byte array[ ], int start, int numBytes)  Here, array is the input source. The second constructor creates an InputStream from a subset of your byte array that begins with the character at the index specified by start and is numBytes long.
  • 17. ByteArrayOutputStream  ByteArrayOutputStream is an implementation of an output stream that uses a byte array as the destination. ByteArrayOutputStream has two constructors, shown here: ByteArrayOutputStream( ) ByteArrayOutputStream(int numBytes)  In the first form, a buffer of 32 bytes is created. In the second, a buffer is created with a size equal to that specified by numBytes.  The buffer size will be increased automatically, if needed. The number of bytes held by the buffer is contained in the protected count field of ByteArrayOutputStream.
  • 18. Filtered Byte Streams  Filtered streams are simply wrappers around underlying input or output streams that transparently provide some extended level of functionality.  Typical extensions are buffering, character translation, and raw data translation. The filtered byte streams are FilterInputStream and FilterOutputStream. Their constructors are shown here: FilterOutputStream(OutputStream os) FilterInputStream(InputStream is)  The methods provided in these classes are identical to those in InputStream and OutputStream
  • 19. Buffered Byte Streams  For the byte-oriented streams, a buffered stream extends a filtered stream class by attaching a memory buffer to the I/O streams.  This buffer allows Java to do I/O operations on more than a byte at a time, hence increasing performance. Because the buffer is available, skipping, marking, and resetting of the stream become possible.  The buffered byte stream classes are BufferedInputStream and BufferedOutputStream. PushbackInputStream also implements a buffered stream.
  • 20. BufferedInputStream  Buffering I/O is a very common performance optimization. Java's BufferedInputStream class allows you to "wrap" any InputStream into a buffered stream and achieve this performance improvement. BufferedInputStream has two constructors: BufferedInputStream(InputStream inputStream) BufferedInputStream(InputStream , int bufSize)
  • 21. BufferedOutputStream  A BufferedOutputStream is similar to any OutputStream with the exception of an added flush( ) method that is used to ensure that data buffers are physically written to the actual output device. Here are the two available constructors:  BufferedOutputStream(OutputStream outputStream)  BufferedOutputStream(OutputStream , int bufSize)  The first form creates a buffered stream using the default buffer size. In the second form, the size of the buffer is passed in bufSize.
  • 22. The Character Streams  While the byte stream classes provide sufficient functionality to handle any type of I/O operation, they cannot work directly with Unicode characters.  Since one of the main purposes of Java is to support the "write once, run anywhere" philosophy, it was necessary to include direct I/O support for characters.  In this section, several of the character I/O classes are discussed. As explained earlier, at the top of the character stream hierarchies are the Reader and Writer abstract classes.
  • 23. Reader : Reader is an abstract class that defines Java's model of streaming character input. It implements the Closeable and Readable interfaces. All of the methods in this class (except for markSupported()) will throw an IOException on error conditions. Writer : Writer is an abstract class that defines streaming character output. It implements the Closeable, Flushable, and Appendable interfaces. All of the methods in this class throw an IOException in the case of errors.
  • 24. FileReader : The FileReader class creates a Reader that you can use to read the contents of a file. FileReader(String filePath) FileReader(File fileObj) Either can throw a FileNotFoundException. Here, filePath is the full path name of a file, and fileObj is a File object that describes the file. FileWriter : FileWriter creates a Writer that you can use to write to a file. FileWriter(String filePath) FileWriter(String filePath, boolean append) FileWriter(File fileObj) FileWriter(File fileObj, boolean append)
  • 25. Serialization Serializable :  Only an object that implements the Serializable interface can be saved and restored by the serialization facilities.  The Serializable interface defines no members. It is simply used to indicate that a class may be serialized. If a class is serializable, all of its subclasses are also serializable.
  • 26. Externalizable  The Java facilities for serialization and deserialization have been designed so that much of the work to save and restore the state of an object occurs automatically.  For example, it may be desirable to use compression or encryption techniques. The Externalizable interface is designed for these situations.  The Externalizable interface defines these two methods: void readExternal(ObjectInput inStream)throws IOException, ClassNotFoundException void writeExternal(ObjectOutput outStream)throws IOException
  • 27. Stream Benefits  Java programs written to adhere to the abstract, high-level InputStream, OutputStream, Reader, and Writer classes will function properly in the future even when new and improved concrete stream classes are invented.  As you will see in the next chapter, this model works very well when we switch from a file system based set of streams to the network and socket streams.  Finally, serialization of objects plays an important role in many types of Java programs.  Java's serialization I/O classes provide a portable solution to this sometimes tricky task.