SlideShare uma empresa Scribd logo
1 de 56
Input and Output in Java
By
Nilesh Dalvi
Lecturer, Patkar-Varde College.Lecturer, Patkar-Varde College.
http://www.slideshare.net/nileshdalvi01
Java and DataJava and Data
StructuresStructures
Introduction
• I/O is used to process the i/p and produce the o/p
based on the i/p
• Java uses the concept of stream to make operations
fast
• java.io package contains all the classes required for
input and output operations
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Stream
• Stream: sequence of data
• In java, it is composed of bytes.
• Three streams:
– System.out : standard output stream
– System.in : standard input stream
– System.err : standard error stream
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Reading Character from Console
• To read a character from console a
BufferedReader, used read().
• int read() throws IOException
• Each time that read() is called, it reads a
character from the input stream and returns it
as an integer value.
• Returns -1 when the end of the stream is
encounterd
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Reading Character from Console
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Reading Character from Console
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Reading String from Console
• String readLine() throws IOException
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Reading String from Console
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
File Class
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
• Represents file and directory pathnames
• Use for creating files and directories,
searching files, file deletion.
• A file object is used to manipulates the
information associated with a disk file, such as
the permissions, time, date and directory path
and to navigate subdirectory.
Constructor
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
• File (String directoryPath)
• File (String directoryPath, String filename)
• File (File obj, String filename)
Methods
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
• public String getName()
– Returns the name of the file or directory denoted by this abstract pathname.
• public String getParent()
– Returns the pathname string of this abstract pathname's parent, or null if this
pathname does not name a parent directory.
• public File getParentFile()
– Returns the abstract pathname of this abstract pathname's parent, or null if
this pathname does not name a parent directory.
• public String getPath()
– Converts this abstract pathname into a pathname string.
• public boolean isAbsolute()
– Tests whether this abstract pathname is absolute. Returns true if this abstract
pathname is absolute, false otherwise
Methods
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
• public String getAbsolutePath()
– Returns the absolute pathname string of this abstract pathname.
• public boolean canRead()
– Tests whether the application can read the file denoted by this abstract
pathname.
• public boolean canWrite()
– Tests whether the application can modify to the file denoted by this abstract
pathname
• public boolean exists()
– Tests whether the file or directory denoted by this abstract pathname exists.
• public boolean isDirectory()
– Tests whether the file denoted by this abstract pathname is a directory
• public boolean isFile()
– Tests whether the file denoted by this abstract pathname is a normal file.
Methods
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
• public long lastModified()
– Returns the time that the file denoted by this abstract pathname was last
modified.
• public boolean createNewFile() throws IOException
– Atomically creates a new, empty file named by this abstract pathname if and
only if a file with this name does not yet exist.
• public boolean delete()
– Deletes the file or directory denoted by this abstract pathname.
• public String[] list()
– Returns an array of strings naming the files and directories in the directory
denoted by this abstract pathname.
• public String[] list(FilenameFilter filter)
– Returns an array of strings naming the files and directories in the directory
denoted by this abstract pathname that satisfy the specified filter.
Methods
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
• public File[] listFiles()
– Returns an array of abstract pathnames denoting the files in the directory
denoted by this abstract pathname.
• public File[] listFiles(FileFilter filter)
– Returns an array of abstract pathnames denoting the files and directories in the
directory denoted by this abstract pathname that satisfy the specified filter.
• public boolean mkdir()
– Creates the directory named by this abstract pathname.
• public boolean mkdirs()
– Creates the directory named by this abstract pathname, including any necessary
but nonexistent parent directories.
• public boolean renameTo(File dest)
– Renames the file denoted by this abstract pathname.
Directories
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
• A Directory is a file that contains a list of other
files and directories.
Using FilenameFilter
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
String [] list (FilenameFilter fobj)
•Defines only single method, accept(), called
once for each file in a list.
boolean accept (File directory, String filename)
– True : for files included in list()
– False :for files excluded in list()
listFiles() Alternative
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
• File [] listFiles()
• File [] listFiles(FilenameFilter fobj)
• File [] listFiles(FileFilter fobj)
Stream Classes in Java
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
• Stream can be defined as a sequence of data.
– InputStream : read data from source
– OutputStream : write data to a destination
• This are basic stream classes in Java
Need of Stream Classes
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
• To perform read and write operation on
binary files we need a mechanism to read that
binary data on file/to write binary data.
• These classes are capable to read and write
one byte on binary files.
Stream Classes
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Byte Stream
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
• Handles byte-oriented I/O
• A byte stream can be used with any type of
objects, including binary data.
InputStream Class
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
• Abstract Class that defines Javas model of
streaming byte input
• Super class of all classes representing an input
stream of bytes
• All methods in this class will throw
IOException on error conditions.
• It is used to read data from a source, it may be
a file, an array peripheral device.
Commonly Used Methods
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Method Description
1) public abstract int read()throws
IOException:
reads the next byte of data from
the input stream.It returns -1 at the
end of file.
2) public int available()throws IOException:
returns an estimate of the number
of bytes that can be read from the
current input stream.
3) public void close()throws IOException:
is used to close the current input
stream.
OutputStream Class
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
• Abstract Class that defines Javas model of
streaming byte Output
• Super class of all classes representing an
output stream of bytes
• All methods in this class will throw
IOException on error conditions.
Commonly Used Methods
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Method Description
1) public void write(int)throws IOException:
is used to write a byte to the
current output stream.
2) public void write(byte[])throws IOException:
is used to write an array of byte to
the current output stream.
3) public void flush()throws IOException flushes the current output stream.
4) public void close()throws IOException
is used to close the current output
stream.
FileInputStream and FileOutputStream Class
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
• FileInputStream and FileOutputStream classes
are used to read and write data in file.
• In another words, they are used for file
handling in java.
FileOutputStream Class
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
• Java FileOutputStream is an output stream for
writing data to a file.
• Write primitive values then use FileOutputStream.
• For character-oriented data, prefer FileWriter.
• But we can write byte-oriented as well as
character-oriented data.
FileOutputStream Class
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
import java.io.*;
class Test{
public static void main(String args[]){
try{
FileOutputstream fout = new FileOutputStream("abc.txt");
String s="Sachin Tendulkar is my favourite player";
byte b[]=s.getBytes();//converting string into byte array
fout.write(b);
fout.close();
System.out.println("success...");
}
catch(Exception e){
System.out.println(e);
}
}
}
FileInputStream Class
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
• Java FileInputStream class obtains input bytes
from a file.
• It is used for reading streams of raw bytes such
as image data.
• For reading streams of characters, consider
using FileReader.
• It should be used to read byte-oriented data for
example to read image, audio, video etc.
FileInputStream Class
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
import java.io.*;
class SimpleRead{
public static void main(String args[]){
try{
FileInputStream fin = new FileInputStream("abc.txt");
int i=0;
while((i=fin.read())!=-1){
System.out.println((char)i);
}
fin.close();
}catch(Exception e){
System.out.println(e);
}
}
}
ByteArrayOutputStream Class
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
• Java ByteArrayOutputStream class is used to
write data into multiple files.
• In this stream, the data is written into a byte
array that can be written to multiple stream.
• ByteArrayOutputStream holds a copy of data
and forwards it to multiple streams.
• Buffer of ByteArrayOutputStream automatically
grows according to data.
Constructors
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
• ByteArrayOutputStream()-creates a new byte
array output stream with the initial capacity of
32 bytes, though its size increases if necessary.
• ByteArrayOutputStream(int size)-creates a new
byte array output stream, with a buffer
capacity of the specified size, in bytes.
Methods
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Method Description
public synchronized void
writeTo(OutputStream out) throws
IOException
writes the complete contents of this
byte array output stream to the
specified output stream.
public void write(byte b) throws IOException writes byte into this stream.
public void write(byte[] b) throws
IOException
writes byte array into this stream.
public void flush() flushes this stream.
ByteArrayOutputStream Class
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
import java.io.*;
class ByteArrayOutDemo{
public static void main(String args[])throws Exception{
FileOutputStream fout1=new FileOutputStream("f1.txt");
FileOutputStream fout2=new FileOutputStream("f2.txt");
ByteArrayOutputStream bout=new ByteArrayOutputStream();
bout.write(139);
bout.writeTo(fout1);
bout.writeTo(fout2);
bout.flush();
bout.close();
System.out.println("success...");
}
}
ByteArrayOutputStream Class
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
ByteArrayInputStream Class
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
• The ByteArrayInputStream class allows us to read data
from byte arrays as streams.
• ByteArrayInputStream can thus wrap the byte array,
and turn it into a stream.
byte[] bytes = "Hello".getBytes(); //get byte array from somewhere.
InputStream input = new ByteArrayInputStream(bytes);
int data = input.read();
while(data != -1) {
//do something with data
data = input.read();
}
input.close();
FilterInputStream and FilterOutputStream Class
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
• FilterInputStream and FilterOutputStream
are base classes for implementing our own
filtering output streams.
• Overrides all method of InputStream and
OutputStream Resp.
BufferedInputStream Class
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
• The BufferedInputStream class provides buffering to
our input streams.
• Buffering can speed up IO quite a bit.
• Rather than read one byte at a time from the network
or disk, the BufferedInputStream reads a larger block
at a time.
• This is typically much faster, especially for disk access
and larger data amounts.
Constructors
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
• BufferedInputStream(InputStream in)
• BufferedInputStream(InputStream in, int size)
Methods:
• Overrides all methods from FilterInputStream
class
BufferedInputStream Class
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
import java.io.*;
class BufferedInputStreamDemo{
public static void main(String args[]){
try
{
FileInputStream fin=new FileInputStream("f1.txt");
BufferedInputStream bin=new BufferedInputStream(fin);
int i;
while((i=bin.read())!=-1){
System.out.print((char)i);
}
bin.close();
fin.close();
}
catch(Exception e){System.out.println(e);}
}
}
BufferedOutputStream Class
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
• The BufferedOutputStream class provides buffering to
our output streams.
• Buffering can speed up IO quite a bit.
• Rather than write one byte at a time to the network or
disk, you write a larger block at a time.
• This is typically much faster, especially for disk access
and larger data amounts.
Constructors
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
• BufferedOutputStream(OutputStream out, int size)
• BufferedOutputStream(OutputStream out)
Methods:
• Overrides all methods from FilterOutputStream
class
BufferedOutputStream Class
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
import java.io.*;
class BufferedOutputStreamDemo
{
public static void main(String args[])
{
try
{
FileOutputStream fout=new FileOutputStream("f1.txt");
BufferedOutputStream bout=new BufferedOutputStream(fout);
String s="Hello World!";
byte b[]=s.getBytes();
bout.write(b);
bout.flush(); //flushes data from bout to fout
bout.close();
fout.close();
}
catch(Exception e){System.out.println(e);}
}
}
DataOutputStream Class
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
• DataOutputStream class enables you to write Java
primitives to OutputStream's instead of only bytes.
• We wrap an OutputStream in a DataOutputStream
and then we can write primitives to it.
• Example:
19.99 12 Java T-shirt
9.99 8 Java Mug
DataInputStream Class
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
• DataInputStream class enables us to read Java
primitives from InputStream's instead of only bytes.
• We wrap an InputStream in a DataInputStream and
then we can read primitives from it.
SequenceInputStream Class
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
• SequenceInputStream combines two or more other
InputStream's into one.
• First all bytes from the first input stream is iterated
and returned, then the bytes from the second input
stream.
PushbackInputStream Class
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
• PushbackInputStream is intendended to be used when
we parse data from an InputStream.
• Sometimes we need to read ahead a few bytes to see
what is coming, before we can determine how to
interprete the current byte.
• PushbackInputStream allows you to do that.
Character Streams
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
• ByteStream classes handle the data in binary form
which is efficient for data processing.
• They cannot work directly with Unicode characters
• CharacterStream deals with the character oriented
text
• Characters are stored and retrieved in a human
readable form
Character Streams
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
• Character Streams have two abstract classes
• They are
– Reader
– Writer
Reader Class
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
• Reader Class is an abstract class that defines Java’s model of
streaming character input
• All of the methods in this class will throw an IOException on
error conditions
Writer Class
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
• Writer Class is an abstract class that defines streaming
character output
• All of the methods in this class returns a void value and throw
an IOException in the case of errors
FileReader Class
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
• FileReader class creates a Reader that you can use to read the
contents of a file
• Two most commonly used constructors:
– FileReader(String filePath)
– FileReader(File fileObj)
FileWriter class
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
• FileWriter creates a Writer that you can use to write to a file
• Its most commonly used constructors are
– FileWriter(String filePath)
– FileWriter(String filePath, boolean append)
– FileWriter(File fileObj)
– FileWriter(File fileObj, boolean append)
CharArrayReader class
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
• CharArrayReader class is an implementation of an input stream
that uses a character array as the source
• Is used to read an array of characters from a memory buffers
• Constructors
• CharArrayReader(char array[ ])
• CharArrayReader(char array[ ], int start, int numChars)
CharArrayWriter class
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
• CharArrayWriter is an implementation of an output stream that
uses an array as the destination
• It is used for writing an array of characters to a memory buffer
• CharArrayWriter has two constructors
• CharArrayWriter( )
• CharArrayWriter(int numChars)
Q & A

Mais conteúdo relacionado

Mais procurados (20)

Java IO
Java IOJava IO
Java IO
 
Generics in java
Generics in javaGenerics in java
Generics in java
 
Constants, Variables and Data Types in Java
Constants, Variables and Data Types in JavaConstants, Variables and Data Types in Java
Constants, Variables and Data Types in Java
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Wrapper classes
Wrapper classes Wrapper classes
Wrapper classes
 
String, string builder, string buffer
String, string builder, string bufferString, string builder, string buffer
String, string builder, string buffer
 
L21 io streams
L21 io streamsL21 io streams
L21 io streams
 
Java constructors
Java constructorsJava constructors
Java constructors
 
Generics
GenericsGenerics
Generics
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
 
Java Streams
Java StreamsJava Streams
Java Streams
 
Applets in java
Applets in javaApplets in java
Applets in java
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Java static keyword
Java static keywordJava static keyword
Java static keyword
 
This keyword in java
This keyword in javaThis keyword in java
This keyword in java
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
Interface in java
Interface in javaInterface in java
Interface in java
 
Applets
AppletsApplets
Applets
 
Java program structure
Java program structureJava program structure
Java program structure
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVA
 

Semelhante a 9. Input Output in java

Semelhante a 9. Input Output in java (20)

Input & output
Input & outputInput & output
Input & output
 
JAVA
JAVAJAVA
JAVA
 
Input/Output Exploring java.io
Input/Output Exploring java.ioInput/Output Exploring java.io
Input/Output Exploring java.io
 
Java program file I/O
Java program file I/OJava program file I/O
Java program file I/O
 
7. Multithreading
7. Multithreading7. Multithreading
7. Multithreading
 
File handling
File handlingFile handling
File handling
 
Java I/O
Java I/OJava I/O
Java I/O
 
L22 multi-threading-introduction
L22 multi-threading-introductionL22 multi-threading-introduction
L22 multi-threading-introduction
 
Java - File Input Output Concepts
Java - File Input Output ConceptsJava - File Input Output Concepts
Java - File Input Output Concepts
 
Multi threading
Multi threadingMulti threading
Multi threading
 
CSE3146-ADV JAVA M2.pdf
CSE3146-ADV JAVA M2.pdfCSE3146-ADV JAVA M2.pdf
CSE3146-ADV JAVA M2.pdf
 
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
 
Basic IO
Basic IOBasic IO
Basic IO
 
9 Inputs & Outputs
9 Inputs & Outputs9 Inputs & Outputs
9 Inputs & Outputs
 
7 streams and error handling in java
7 streams and error handling in java7 streams and error handling in java
7 streams and error handling in java
 
Unit3 packages & interfaces
Unit3 packages & interfacesUnit3 packages & interfaces
Unit3 packages & interfaces
 
Input output files in java
Input output files in javaInput output files in java
Input output files in java
 
IOStream.pptx
IOStream.pptxIOStream.pptx
IOStream.pptx
 
Unit3 part3-packages and interfaces
Unit3 part3-packages and interfacesUnit3 part3-packages and interfaces
Unit3 part3-packages and interfaces
 
Module 1.pptx
Module 1.pptxModule 1.pptx
Module 1.pptx
 

Mais de Nilesh Dalvi

10. Introduction to Datastructure
10. Introduction to Datastructure10. Introduction to Datastructure
10. Introduction to DatastructureNilesh Dalvi
 
6. Exception Handling
6. Exception Handling6. Exception Handling
6. Exception HandlingNilesh Dalvi
 
5. Inheritances, Packages and Intefaces
5. Inheritances, Packages and Intefaces5. Inheritances, Packages and Intefaces
5. Inheritances, Packages and IntefacesNilesh Dalvi
 
4. Classes and Methods
4. Classes and Methods4. Classes and Methods
4. Classes and MethodsNilesh Dalvi
 
3. Data types and Variables
3. Data types and Variables3. Data types and Variables
3. Data types and VariablesNilesh Dalvi
 
1. Overview of Java
1. Overview of Java1. Overview of Java
1. Overview of JavaNilesh Dalvi
 
Standard Template Library
Standard Template LibraryStandard Template Library
Standard Template LibraryNilesh Dalvi
 
Input and output in C++
Input and output in C++Input and output in C++
Input and output in C++Nilesh Dalvi
 
Inheritance : Extending Classes
Inheritance : Extending ClassesInheritance : Extending Classes
Inheritance : Extending ClassesNilesh Dalvi
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator OverloadingNilesh Dalvi
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructorsNilesh Dalvi
 

Mais de Nilesh Dalvi (20)

14. Linked List
14. Linked List14. Linked List
14. Linked List
 
13. Queue
13. Queue13. Queue
13. Queue
 
12. Stack
12. Stack12. Stack
12. Stack
 
11. Arrays
11. Arrays11. Arrays
11. Arrays
 
10. Introduction to Datastructure
10. Introduction to Datastructure10. Introduction to Datastructure
10. Introduction to Datastructure
 
8. String
8. String8. String
8. String
 
6. Exception Handling
6. Exception Handling6. Exception Handling
6. Exception Handling
 
5. Inheritances, Packages and Intefaces
5. Inheritances, Packages and Intefaces5. Inheritances, Packages and Intefaces
5. Inheritances, Packages and Intefaces
 
4. Classes and Methods
4. Classes and Methods4. Classes and Methods
4. Classes and Methods
 
3. Data types and Variables
3. Data types and Variables3. Data types and Variables
3. Data types and Variables
 
2. Basics of Java
2. Basics of Java2. Basics of Java
2. Basics of Java
 
1. Overview of Java
1. Overview of Java1. Overview of Java
1. Overview of Java
 
Standard Template Library
Standard Template LibraryStandard Template Library
Standard Template Library
 
Templates
TemplatesTemplates
Templates
 
Input and output in C++
Input and output in C++Input and output in C++
Input and output in C++
 
Strings
StringsStrings
Strings
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Inheritance : Extending Classes
Inheritance : Extending ClassesInheritance : Extending Classes
Inheritance : Extending Classes
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 

Último

Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptxPoojaSen20
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 

Último (20)

Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptx
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 

9. Input Output in java

  • 1. Input and Output in Java By Nilesh Dalvi Lecturer, Patkar-Varde College.Lecturer, Patkar-Varde College. http://www.slideshare.net/nileshdalvi01 Java and DataJava and Data StructuresStructures
  • 2. Introduction • I/O is used to process the i/p and produce the o/p based on the i/p • Java uses the concept of stream to make operations fast • java.io package contains all the classes required for input and output operations Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 3. Stream • Stream: sequence of data • In java, it is composed of bytes. • Three streams: – System.out : standard output stream – System.in : standard input stream – System.err : standard error stream Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 4. Reading Character from Console • To read a character from console a BufferedReader, used read(). • int read() throws IOException • Each time that read() is called, it reads a character from the input stream and returns it as an integer value. • Returns -1 when the end of the stream is encounterd Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 5. Reading Character from Console Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 6. Reading Character from Console Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 7. Reading String from Console • String readLine() throws IOException Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 8. Reading String from Console Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 9. File Class Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). • Represents file and directory pathnames • Use for creating files and directories, searching files, file deletion. • A file object is used to manipulates the information associated with a disk file, such as the permissions, time, date and directory path and to navigate subdirectory.
  • 10. Constructor Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). • File (String directoryPath) • File (String directoryPath, String filename) • File (File obj, String filename)
  • 11. Methods Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). • public String getName() – Returns the name of the file or directory denoted by this abstract pathname. • public String getParent() – Returns the pathname string of this abstract pathname's parent, or null if this pathname does not name a parent directory. • public File getParentFile() – Returns the abstract pathname of this abstract pathname's parent, or null if this pathname does not name a parent directory. • public String getPath() – Converts this abstract pathname into a pathname string. • public boolean isAbsolute() – Tests whether this abstract pathname is absolute. Returns true if this abstract pathname is absolute, false otherwise
  • 12. Methods Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). • public String getAbsolutePath() – Returns the absolute pathname string of this abstract pathname. • public boolean canRead() – Tests whether the application can read the file denoted by this abstract pathname. • public boolean canWrite() – Tests whether the application can modify to the file denoted by this abstract pathname • public boolean exists() – Tests whether the file or directory denoted by this abstract pathname exists. • public boolean isDirectory() – Tests whether the file denoted by this abstract pathname is a directory • public boolean isFile() – Tests whether the file denoted by this abstract pathname is a normal file.
  • 13. Methods Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). • public long lastModified() – Returns the time that the file denoted by this abstract pathname was last modified. • public boolean createNewFile() throws IOException – Atomically creates a new, empty file named by this abstract pathname if and only if a file with this name does not yet exist. • public boolean delete() – Deletes the file or directory denoted by this abstract pathname. • public String[] list() – Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname. • public String[] list(FilenameFilter filter) – Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname that satisfy the specified filter.
  • 14. Methods Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). • public File[] listFiles() – Returns an array of abstract pathnames denoting the files in the directory denoted by this abstract pathname. • public File[] listFiles(FileFilter filter) – Returns an array of abstract pathnames denoting the files and directories in the directory denoted by this abstract pathname that satisfy the specified filter. • public boolean mkdir() – Creates the directory named by this abstract pathname. • public boolean mkdirs() – Creates the directory named by this abstract pathname, including any necessary but nonexistent parent directories. • public boolean renameTo(File dest) – Renames the file denoted by this abstract pathname.
  • 15. Directories Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). • A Directory is a file that contains a list of other files and directories.
  • 16. Using FilenameFilter Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). String [] list (FilenameFilter fobj) •Defines only single method, accept(), called once for each file in a list. boolean accept (File directory, String filename) – True : for files included in list() – False :for files excluded in list()
  • 17. listFiles() Alternative Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). • File [] listFiles() • File [] listFiles(FilenameFilter fobj) • File [] listFiles(FileFilter fobj)
  • 18. Stream Classes in Java Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). • Stream can be defined as a sequence of data. – InputStream : read data from source – OutputStream : write data to a destination • This are basic stream classes in Java
  • 19. Need of Stream Classes Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). • To perform read and write operation on binary files we need a mechanism to read that binary data on file/to write binary data. • These classes are capable to read and write one byte on binary files.
  • 20. Stream Classes Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 21. Byte Stream Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). • Handles byte-oriented I/O • A byte stream can be used with any type of objects, including binary data.
  • 22. InputStream Class Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). • Abstract Class that defines Javas model of streaming byte input • Super class of all classes representing an input stream of bytes • All methods in this class will throw IOException on error conditions. • It is used to read data from a source, it may be a file, an array peripheral device.
  • 23. Commonly Used Methods Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Method Description 1) public abstract int read()throws IOException: reads the next byte of data from the input stream.It returns -1 at the end of file. 2) public int available()throws IOException: returns an estimate of the number of bytes that can be read from the current input stream. 3) public void close()throws IOException: is used to close the current input stream.
  • 24. OutputStream Class Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). • Abstract Class that defines Javas model of streaming byte Output • Super class of all classes representing an output stream of bytes • All methods in this class will throw IOException on error conditions.
  • 25. Commonly Used Methods Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Method Description 1) public void write(int)throws IOException: is used to write a byte to the current output stream. 2) public void write(byte[])throws IOException: is used to write an array of byte to the current output stream. 3) public void flush()throws IOException flushes the current output stream. 4) public void close()throws IOException is used to close the current output stream.
  • 26. FileInputStream and FileOutputStream Class Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). • FileInputStream and FileOutputStream classes are used to read and write data in file. • In another words, they are used for file handling in java.
  • 27. FileOutputStream Class Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). • Java FileOutputStream is an output stream for writing data to a file. • Write primitive values then use FileOutputStream. • For character-oriented data, prefer FileWriter. • But we can write byte-oriented as well as character-oriented data.
  • 28. FileOutputStream Class Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). import java.io.*; class Test{ public static void main(String args[]){ try{ FileOutputstream fout = new FileOutputStream("abc.txt"); String s="Sachin Tendulkar is my favourite player"; byte b[]=s.getBytes();//converting string into byte array fout.write(b); fout.close(); System.out.println("success..."); } catch(Exception e){ System.out.println(e); } } }
  • 29. FileInputStream Class Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). • Java FileInputStream class obtains input bytes from a file. • It is used for reading streams of raw bytes such as image data. • For reading streams of characters, consider using FileReader. • It should be used to read byte-oriented data for example to read image, audio, video etc.
  • 30. FileInputStream Class Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). import java.io.*; class SimpleRead{ public static void main(String args[]){ try{ FileInputStream fin = new FileInputStream("abc.txt"); int i=0; while((i=fin.read())!=-1){ System.out.println((char)i); } fin.close(); }catch(Exception e){ System.out.println(e); } } }
  • 31. ByteArrayOutputStream Class Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). • Java ByteArrayOutputStream class is used to write data into multiple files. • In this stream, the data is written into a byte array that can be written to multiple stream. • ByteArrayOutputStream holds a copy of data and forwards it to multiple streams. • Buffer of ByteArrayOutputStream automatically grows according to data.
  • 32. Constructors Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). • ByteArrayOutputStream()-creates a new byte array output stream with the initial capacity of 32 bytes, though its size increases if necessary. • ByteArrayOutputStream(int size)-creates a new byte array output stream, with a buffer capacity of the specified size, in bytes.
  • 33. Methods Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Method Description public synchronized void writeTo(OutputStream out) throws IOException writes the complete contents of this byte array output stream to the specified output stream. public void write(byte b) throws IOException writes byte into this stream. public void write(byte[] b) throws IOException writes byte array into this stream. public void flush() flushes this stream.
  • 34. ByteArrayOutputStream Class Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). import java.io.*; class ByteArrayOutDemo{ public static void main(String args[])throws Exception{ FileOutputStream fout1=new FileOutputStream("f1.txt"); FileOutputStream fout2=new FileOutputStream("f2.txt"); ByteArrayOutputStream bout=new ByteArrayOutputStream(); bout.write(139); bout.writeTo(fout1); bout.writeTo(fout2); bout.flush(); bout.close(); System.out.println("success..."); } }
  • 35. ByteArrayOutputStream Class Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 36. ByteArrayInputStream Class Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). • The ByteArrayInputStream class allows us to read data from byte arrays as streams. • ByteArrayInputStream can thus wrap the byte array, and turn it into a stream. byte[] bytes = "Hello".getBytes(); //get byte array from somewhere. InputStream input = new ByteArrayInputStream(bytes); int data = input.read(); while(data != -1) { //do something with data data = input.read(); } input.close();
  • 37. FilterInputStream and FilterOutputStream Class Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). • FilterInputStream and FilterOutputStream are base classes for implementing our own filtering output streams. • Overrides all method of InputStream and OutputStream Resp.
  • 38. BufferedInputStream Class Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). • The BufferedInputStream class provides buffering to our input streams. • Buffering can speed up IO quite a bit. • Rather than read one byte at a time from the network or disk, the BufferedInputStream reads a larger block at a time. • This is typically much faster, especially for disk access and larger data amounts.
  • 39. Constructors Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). • BufferedInputStream(InputStream in) • BufferedInputStream(InputStream in, int size) Methods: • Overrides all methods from FilterInputStream class
  • 40. BufferedInputStream Class Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). import java.io.*; class BufferedInputStreamDemo{ public static void main(String args[]){ try { FileInputStream fin=new FileInputStream("f1.txt"); BufferedInputStream bin=new BufferedInputStream(fin); int i; while((i=bin.read())!=-1){ System.out.print((char)i); } bin.close(); fin.close(); } catch(Exception e){System.out.println(e);} } }
  • 41. BufferedOutputStream Class Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). • The BufferedOutputStream class provides buffering to our output streams. • Buffering can speed up IO quite a bit. • Rather than write one byte at a time to the network or disk, you write a larger block at a time. • This is typically much faster, especially for disk access and larger data amounts.
  • 42. Constructors Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). • BufferedOutputStream(OutputStream out, int size) • BufferedOutputStream(OutputStream out) Methods: • Overrides all methods from FilterOutputStream class
  • 43. BufferedOutputStream Class Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). import java.io.*; class BufferedOutputStreamDemo { public static void main(String args[]) { try { FileOutputStream fout=new FileOutputStream("f1.txt"); BufferedOutputStream bout=new BufferedOutputStream(fout); String s="Hello World!"; byte b[]=s.getBytes(); bout.write(b); bout.flush(); //flushes data from bout to fout bout.close(); fout.close(); } catch(Exception e){System.out.println(e);} } }
  • 44. DataOutputStream Class Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). • DataOutputStream class enables you to write Java primitives to OutputStream's instead of only bytes. • We wrap an OutputStream in a DataOutputStream and then we can write primitives to it. • Example: 19.99 12 Java T-shirt 9.99 8 Java Mug
  • 45. DataInputStream Class Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). • DataInputStream class enables us to read Java primitives from InputStream's instead of only bytes. • We wrap an InputStream in a DataInputStream and then we can read primitives from it.
  • 46. SequenceInputStream Class Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). • SequenceInputStream combines two or more other InputStream's into one. • First all bytes from the first input stream is iterated and returned, then the bytes from the second input stream.
  • 47. PushbackInputStream Class Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). • PushbackInputStream is intendended to be used when we parse data from an InputStream. • Sometimes we need to read ahead a few bytes to see what is coming, before we can determine how to interprete the current byte. • PushbackInputStream allows you to do that.
  • 48. Character Streams Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). • ByteStream classes handle the data in binary form which is efficient for data processing. • They cannot work directly with Unicode characters • CharacterStream deals with the character oriented text • Characters are stored and retrieved in a human readable form
  • 49. Character Streams Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). • Character Streams have two abstract classes • They are – Reader – Writer
  • 50. Reader Class Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). • Reader Class is an abstract class that defines Java’s model of streaming character input • All of the methods in this class will throw an IOException on error conditions
  • 51. Writer Class Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). • Writer Class is an abstract class that defines streaming character output • All of the methods in this class returns a void value and throw an IOException in the case of errors
  • 52. FileReader Class Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). • FileReader class creates a Reader that you can use to read the contents of a file • Two most commonly used constructors: – FileReader(String filePath) – FileReader(File fileObj)
  • 53. FileWriter class Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). • FileWriter creates a Writer that you can use to write to a file • Its most commonly used constructors are – FileWriter(String filePath) – FileWriter(String filePath, boolean append) – FileWriter(File fileObj) – FileWriter(File fileObj, boolean append)
  • 54. CharArrayReader class Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). • CharArrayReader class is an implementation of an input stream that uses a character array as the source • Is used to read an array of characters from a memory buffers • Constructors • CharArrayReader(char array[ ]) • CharArrayReader(char array[ ], int start, int numChars)
  • 55. CharArrayWriter class Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). • CharArrayWriter is an implementation of an output stream that uses an array as the destination • It is used for writing an array of characters to a memory buffer • CharArrayWriter has two constructors • CharArrayWriter( ) • CharArrayWriter(int numChars)
  • 56. Q & A