Professional Documents
Culture Documents
Java Chap 2 - Merged
Java Chap 2 - Merged
DEPARTMENT : CSE
Bachelor of Engineering (Computer Science & Engineering)
Java Programing(CST-205)
TOPIC OF PRESENTATION:
2
PipedWriter
The PipedWriter class is used to write java pipe as a stream of characters.
This class is used generally for writing text. Generally PipedWriter is connected to
a PipedReader and used by different threads.
Constructor:
Methods
PipedReader
The PipedReader class is used to read the contents of a pipe as a stream of
characters. This class is used generally to read text.
PipedReader class must be connected to the same PipedWriter and are used by
different threads.
Constructor:
Example
import java.io.PipedReader;
import java.io.PipedWriter;
a. Writer
b. Reader
c. FileReader
d. FileInputStream
Reference Links:
https://www.geeksforgeeks.org/java-io-pipedreader-class-java/
https://www.geeksforgeeks.org/java-io-pipedwriter-class-java/
https://docs.oracle.com/javase/7/docs/api/java/io/PipedReader.html
https://docs.oracle.com/javase/7/docs/api/java/io/PipedWriter.html
https://www.javatpoint.com/java-pipedwriter-class
https://www.javatpoint.com/java-pipedreader-class
THANK YOU
INSTITUTE : UIE
DEPARTMENT : CSE
Bachelor of Engineering (Computer Science & Engineering)
Java Programing(CST-205)
TOPIC OF PRESENTATION:
2
Introduction to Streams
• Java programs perform I/O through streams. A stream is:
– an abstraction that either produces or consumes information
– linked to a physical device by the Java I/O system
• All streams behave similarly, even if the actual physical devices to which they are
linked differ.
• Thus the same I/O classes can be applied to any kind of device as they abstract the
difference between different I/O devices.
• Java’s stream classes are defined in the java.io package.
• Java 2 defines two types of streams:
– byte streams
– character streams
• Byte streams:
– provide a convenient means for handling input and output of bytes
– are used for reading or writing binary data
• Character streams:
– provide a convenient means for handling input and output of
characters
– use Unicode, and, therefore, can be internationalized
Predefined Streams
Column A Column B
13
FileOutputStream class methods
14
Example
Java FileOutputStream Example 1: write byte
import java.io.FileOutputStream;
public class FileOutputStreamExample {
public static void main(String args[]){
try{
FileOutputStream fout=new FileOutputStream("D:\\testout.txt");
Output:
fout.write(65);
Success...
fout.close();
The content of a text file testout.txt is set
System.out.println("success...");
with the data A.
}catch(Exception e){System.out.println(e);}
testout.txt
}
A
}
15
Summary:
Video Lectures :
https://youtu.be/lYuj9nn0PBU
Reference Links:
https://docs.oracle.com/javase/7/docs/api/java/io/FileOutputStream.html
https://www.tutorialspoint.com/java/io/index.htm
https://www.tutorialspoint.com/java/io/java_io_fileoutputstream.htm
https://www.javatpoint.com/java-io
https://www.javatpoint.com/java-fileoutputstream-class
THANK YOU
INSTITUTE : UIE
DEPARTMENT : CSE
Bachelor of Engineering (Computer Science & Engineering)
Java Programing(CST-205)
TOPIC OF PRESENTATION:
2
Java FileInputStream Class
The FileInputStream class creates an InputStream that you can use to read bytes from a file.
FileInputStream(String filepath)
FileInputStream(File fileObj) throw a FileNotFoundException
Eg::
The mark( ) and reset( ) methods are not overridden, and any attempt to use reset( ) on a
FileInputStream will generate an IOException.
Java FileInputStream class methods
Java FileInputStream example 1: read single character
import java.io.FileInputStream;
public class DataStreamExample {
public static void main(String args[]){
try{ FileInputStream fin=new FileInputStream("D:\\testout.txt");
int i=fin.read();
System.out.print((char)i);
fin.close();
}catch(Exception e){System.out.println(e);}
}
}
Note: Before running the code, a text file named "testout.txt" is required to be
created. In this file, we are having the following content:
Welcome to javatpoint.
After executing the above program, you will get a single character from the file
which is 87 (in byte form). To see the text, you need to convert it into character.
Output:
W
Java FileInputStream example 2: read all characters
import java.io.FileInputStream;
public class DataStreamExample {
public static void main(String args[]){
try{
FileInputStream fin=new FileInputStream("D:\\testout.txt");
int i=0;
while((i=fin.read())!=-1){
System.out.print((char)i);
}
fin.close();
}catch(Exception e){
System.out.println(e);
}
}
}
Output:
Welcome to javaTpoint
QUIZ:
Which of these exception is thrown in cases when the file specified for
writing is not found?
a) IOException
b) FileException
c) FileNotFoundException
d) FileInputException
ByteArrayOutputStream
System.out.println("Buffer as a string");
System.out.println(f.toString());
System.out.println("Into array");
byte b[] = f.toByteArray();
for (int i=0; i<b.length; i++) {
System.out.print((char) b[i]);
}
System.out.println("\nTo an OutputStream()");
OutputStream f2 = new FileOutputStream("test.txt");
f.writeTo(f2);
f2.close();
System.out.println("Doing a reset");
f.reset();
for (int i=0; i<3; i++)
f.write('X');
System.out.println(f.toString());
System.out.println("\nTo an OutputStream()");
OutputStream f3 = new FileOutputStream("test1.txt");
f.writeTo(f3);
f3.close();
Additional:: this example uses the writeTo( ) convenience
} method to write the contents of f to test.txt.
}
ByteArrayInputStream
ByteArrayInputStream(byte array[ ])
ByteArrayInputStream(byte array[ ], int start, int numBytes)
Video Lectures :
https://youtu.be/fnFQWtZZE-4
https://youtu.be/5s1FQMoWuJs
Reference Links:
https://docs.oracle.com/javase/7/docs/api/java/io/FileInputStream.html
https://docs.oracle.com/javase/7/docs/api/java/io/ByteArrayInputStream.html
https://docs.oracle.com/javase/7/docs/api/java/io/ByteArrayOutputStream.html
https://www.javatpoint.com/java-fileinputstream-class
https://www.javatpoint.com/java-bytearrayoutputstream-class
https://www.javatpoint.com/java-bytearrayinputstream-class
THANK YOU
INSTITUTE : UIE
DEPARTMENT : CSE
Bachelor of Engineering (Computer Science & Engineering)
Java Programing(CST-205)
TOPIC OF PRESENTATION:
2
Java Writer
Writer is an abstract class that defines streaming character output.
Constructor
Modifier:: protected
Constructor:: Writer()
Description:: It creates a new character-stream writer whose critical sections will
synchronize on the writer itself.
Methods
Reader
Reader is an abstract class that defines Java’s model of streaming character input.
Constructor
Modifier:: protected
Constructor:: Reader()
Description:: It creates a new character-stream reader whose critical sections will
synchronize on the reader itself.
Methods
FileWriter
FileWriter(String filePath)
FileWriter(String filePath, boolean append)
FileWriter(File fileObj)
FileWriter(File fileObj, boolean append)
throw an IOException.
FileWriter methods
FileWriter Example
import java.io.*;
class FileWriterDemo {
public static void main(String args[]) throws IOException {
The FileReader class creates a Reader that you can use to read the
contents of a file.
FileReader(String filePath)
FileReader(File fileObj)
throw a FileNotFoundException.
FileReader methods
FileReader Example
import java.io.*;
class FileReaderDemo {
public static void main(String args[]) throws IOException {
FileReader fr = new FileReader("file112.txt");
BufferedReader br = new BufferedReader(fr);
String s;
System.out.println(br.readLine());
//while((s = br.readLine()) != null) {
//System.out.println(s);
//}
fr.close();
br.close(); Shows how to read lines from a file and print
} these to the standard output stream.
}
QUIZ:
• FileNotFoundException
• ClassNotFoundException
• StringOutOfBoundsException
• NullPointerException
Summary:
Video Lectures :
https://youtu.be/QzL6rGxqMcs
https://youtu.be/6I5bxsXcTug
Reference Links:
https://docs.oracle.com/javase/7/docs/api/java/io/Writer.html
https://docs.oracle.com/javase/7/docs/api/java/io/Reader.html
https://docs.oracle.com/javase/7/docs/api/java/io/FileWriter.html
https://docs.oracle.com/javase/7/docs/api/java/io/FileReader.html
https://www.javatpoint.com/java-writer-class
https://www.javatpoint.com/java-reader-class
https://www.javatpoint.com/java-filewriter-class
https://www.javatpoint.com/java-filereader-class
THANK YOU
INSTITUTE : UIE
DEPARTMENT : CSE
Bachelor of Engineering (Computer Science & Engineering)
Java Programing(CST-205)
TOPIC OF PRESENTATION:
2
CharArrayReader
CharArrayReader is an implementation of an input stream that uses a character array
as the source.
CharArrayReader(char array[ ])
CharArrayReader(char array[ ], int start, int numChars)
Java CharArrayReader class methods
Example
import java.io.*;
public class CharArrayReaderDemo {
public static void main(String args[]) throws IOException {
String tmp = "abcdefghijklmnopqrstuvwxyz";
int length = tmp.length();
char c[] = new char[length];
tmp.getChars(0, length, c, 0); //public void getChars(int srcBegin, int srcEnd,
// char[] dst, int dstBegin)
CharArrayReader input1 = new CharArrayReader(c);
CharArrayReader input2 = new CharArrayReader(c, 0, 5);
int i;
System.out.println("input1 is:");
while((i = input1.read()) != -1) {
System.out.print((char)i);
}
System.out.println();
System.out.println("input2 is:");
while((i = input2.read()) != -1) {
System.out.print((char)i); } input1 object is constructed using the entire lowercase
} alphabet,
CharArrayWriter( )
CharArrayWriter(int numChars)
Java CharArrayWriter class methods
Java CharArrayWriter class methods
Example
import java.io.*;
class CharArrayWriterDemo {
public static void main(String args[]) throws IOException {
System.out.println("Buffer as a string");
System.out.println(f.toString());
System.out.println("Into array");
char c[] = f.toCharArray();
for (int i=0; i<c.length; i++) {
System.out.print(c[i]);
}
System.out.println("\nTo a FileWriter()");
FileWriter f2 = new FileWriter("test.txt");
f.writeTo(f2);
f2.close();
System.out.println("Doing a reset"); Example demonstrates
f.reset(); CharArrayWriter by reworking the
for (int i=0; i<3; i++) sample program shown earlier for
f.write('X'); ByteArrayOutputStream.
System.out.println(f.toString()); } }
Object serialization
• You can only serialize the objects of a class that implements Serializable
interface
Object serialization
After a serialized object is written to a file, it can be read from the file and
deserialized (that is we can recreate the object in memory)
By default, when you serialize an object, all its fields are serialized
except for static variables. When you construct this object back from
its persistent state, you will get the values of all the fields that are
serialized(except static variables).
Video Lectures :
https://youtu.be/L2g2dnGu93Y
https://youtu.be/6B6vp0jZnb0
Reference Links:
https://javadoc.scijava.org/Java7/java/io/CharArrayReader.html
https://javadoc.scijava.org/Java7/java/io/CharArrayWriter.html
https://docs.oracle.com/javase/7/docs/api/java/io/CharArrayReader.html
https://docs.oracle.com/javase/7/docs/api/java/io/CharArrayWriter.html
https://www.geeksforgeeks.org/java-io-chararrayreader-class-java/
https://www.geeksforgeeks.org/java-io-chararraywriter-class-java-set-
1/?ref=lbp
THANK YOU
INSTITUTE : UIE
DEPARTMENT : CSE
Bachelor of Engineering (Computer Science & Engineering)
Java Programing(CST-205)
TOPIC OF PRESENTATION:
2
FilterWriter
Java FilterWriter class is an abstract class which is used to write filtered character
streams.
The sub class of the FilterWriter should override some of its methods and it may
provide additional methods and fields also.
Constructors
Modifier: protected
Constructor: FilterWriter(Writer out)
Description: It creates InputStream class Object
Methods
Example
class FilterWriterDemo {
public static void main(String[] args) throws Exception {
FilterWriter fr = null;
Writer wr = null;
wr = new StringWriter();
fr = new FilterWriter(wr) {} ;
String str = "Geeksfor";
char c[] = {'G','e','e','k'};
//illustrating write(String str,int off,int len)
fr.write(str);
//illustrating flush()
fr.flush();
//illustrating write(char[] cff,int off,int len)
fr.write(c);
//illustrating write(int c)
fr.write('s');
System.out.print(wr.toString());
//close the stream
fr.close();
}
}
Java.io.FilterReader Class
Introduction
The Java.io.FilterReader class is for reading filtered character streams.
Class constructors
//illustrating markSupported()
if(fr.markSupported()) {
//illustrating mark() method
System.out.println("mark method is supported");
fr.mark(100);
}
Example
//illustrating skip() method
fr.skip(5);
//illustrating ready()
if(fr.ready()) {
//illustrating read(char[] cbuff,int off,int len)
fr.read(ch);
for (int i = 0; i < 8; i++) {
System.out.print(ch[i]);
}
//illustrating reset()
fr.reset();
for (int i = 0; i <5 ; i++) {
//illustrating read()
System.out.print((char)fr.read());
} }
//closing the stream
fr.close(); } }
QUIZ:
a. FilterOutputStream
b. BufferedOutputStream
c. ByteArrayOutputStream
Summary:
Reference Links:
https://docs.oracle.com/javase/7/docs/api/java/io/FilterWriter.html
https://docs.oracle.com/javase/7/docs/api/java/io/FilterReader.html
https://www.javatpoint.com/java-filterreader-class
https://www.javatpoint.com/java-filterwriter-class
https://www.geeksforgeeks.org/java-io-filterwriter-class-java/
https://www.geeksforgeeks.org/java-io-filterreader-class-java/
THANK YOU