0% found this document useful (0 votes)
68 views

Chapter-4 File I O

1. Streams allow data to be read from a source and written to a destination in Java. Input streams read from a source and output streams write to a destination like a file. 2. The File class represents files and directories on the file system and allows operations like creating, reading, and writing files. 3. Streams can be byte streams that handle single bytes of data or character streams that handle characters. InputStream and OutputStream are for byte streams while Reader and Writer are for character streams. 4. Common stream classes include FileInputStream, FileOutputStream, FileReader, and FileWriter which connect to files. Methods like read() and write() perform the actual input and output.

Uploaded by

Naoly Get
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views

Chapter-4 File I O

1. Streams allow data to be read from a source and written to a destination in Java. Input streams read from a source and output streams write to a destination like a file. 2. The File class represents files and directories on the file system and allows operations like creating, reading, and writing files. 3. Streams can be byte streams that handle single bytes of data or character streams that handle characters. InputStream and OutputStream are for byte streams while Reader and Writer are for character streams. 4. Common stream classes include FileInputStream, FileOutputStream, FileReader, and FileWriter which connect to files. Methods like read() and write() perform the actual input and output.

Uploaded by

Naoly Get
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

By: Naol G.

METTU UNIVERSITY
FACULTY OF ENGINEERING & TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE
Java programming course
Chapter-4
Java Streams and File I/O
By: Naol G. 2

Introduction
• What is Stream.
• Streams: is connection (logical) that allows a sequence of data moving from program
to file or vice versa.
• Why IO stream
• In java, when we store data inside variables, they kept inside RAM (i.e. lost when
power off).
• EX.
class TemporaryDataStoring{
public static void main(String[] args) { Both, 200.45 and 1234 data is used
double balance = 200.45; only @execution time.
int rolno = 1234; • Then the data will be removed after
} program execution completed.
}

• Advantage of IO stream:
• Permanent data storage (On the disk, not on RAM)
By: Naol G. 3

• In Java, streams are the sequence of data that are read from the
source and written to the destination.
• An input stream allow java program to read data from the source (ex. File).
• An output stream allow java program to write data to the destination (ex. File).

class HelloWorld { Note:


public static void main(String[] args) { In Hello World example, we have used
System.out.println("Hello, World!"); System.out to print a string.
}
} Here, the System.out is a type of output
stream.
By: Naol G. 4
By: Naol G. 5

Working with File Class


• Before doing operations (reading/writing) on file, we need to learn how
to create file first.
• The java.io.File is the central class that works with files and
directories.
• The instance of this class represents
• the name of a file or directory on the host file system.
• When a File object is created, the system doesn't check to the
existence of a corresponding file/directory.
• If the file exists, a program can examine its attributes and perform
various operations on the file, such as renaming it, deleting it, reading
from or writing to it.
By: Naol G. 6
By: Naol G. 7
By: Naol G. 8
By: Naol G. 9

More…
• getAbsolutePath() : String
• getAbsolutePath() returns the complete, non-relative path to the file.
• boolean f.delete() throws SecurityException
• Deletes the file.
• boolean f.renameTo(File f2) throws SecurityException
• Renames f to File f2. Returns true if successful.

By: Naol G. 10
By: Naol G. 11

EXAMPLE: CREATE NEW FILE


12

Using Path Names


• Path name—gives name of file and tells which directory the file is in
• Relative path name—gives the path starting with the directory that the program is
in.
• Absoulte path name—gives the path starting with the root directory (C:\) that the
program is in.

• Typical UNIX path name:


• /user/smith/home.work/java/FileClassDemo.java
• Typical Windows path name:
• D:\Work\Java\Programs\FileClassDemo.java
• When a backslash is used in a quoted string it must be written as two backslashes
since backslash is the escape character:
• "D:\\Work\\Java\\Programs\\FileClassDemo.java"
• Java will accept path names in UNIX or Windows format, regardless of which
operating system it is actually running on.
By: Naol G. 13

Types of Streams
• Depending upon the type of data flow within the stream, it (stream)
can be classified into:
• Byte Stream
• Character Stream

• Based on the direction of data flow, it can be classified into:


• InputStream and OutputStream (Byte Stream type) or
• Reader and Writer (Character Stream type)

• Note: both inputstream and Reader utilities for reading data from source to program.
• both outputstream and Writer utilities for writing data to file from program.
By: Naol G. 14

1) Byte Stream
• Byte stream is used to read and write a single byte (8 bits)
of data.
• All byte stream classes are derived from base abstract
classes called
• InputStream (to read) and OutputStream (to write).
By: Naol G. 15

Java InputStream Class

• First, inputstream the way of reading data from source by java


program.
• The InputStream class is part of the java.io package
• It is an abstract superclass that represents an input stream of bytes.
• Since InputStream is an abstract class, it is not useful by itself.
• However, its subclasses can be used to read data.
By: Naol G. 16

• Subclasses of InputStream
• In order to use the functionality of InputStream, we can use its subclasses.
• Some of them are:
• FileInputStream
• ByteArrayInputStream
• ObjectInputStream
By: Naol G. 17

• Create an InputStream
• In order to create an InputStream, we must import the java.io.InputStream
package first.
• Once we import the package, here is how we can create the input stream.

• Here, we have created an input stream using FileInputStream. It is


because InputStream is an abstract class.
• Hence we cannot create an object of InputStream.
• Note: We can also create an input stream from other subclasses of InputStream.
By: Naol G. 18

Methods of InputStream
• The InputStream class provides different methods that are
implemented by its subclasses.
• Here are some of the commonly used methods:
By: Naol G. 19

Example: InputStream Using FileInputStream


Suppose we have a file named input.txt
with the following content.
“This is a line of text inside the input.txt file.”

Output
Available bytes in the file is: 49
Data read from the file:
This is a line of text inside the input.txt file.
By: Naol G. 20

Java OutputStream Class


• First, outputstream the way of writing data to destination from java
program.
• The OutputStream class is part of the java.io package
• It is an abstract superclass that represents an output stream of bytes.
• Since OutputStream is an abstract class, it is not useful by itself.
• However, its subclasses can be used to write data.
By: Naol G. 21

• Subclasses of OutputStream
• In order to use the functionality of OutputStream, we can use its subclasses.
Some of them are:
• FileOutputStream
• ByteArrayOutputStream
• ObjectOutputStream
By: Naol G. 22

• Create an OutputStream
• In order to create OutputStream, we must import java.io.OutputStream package
first.
• Once we import the package, here is how we can create the output stream.

• Here, we have created an object of output stream using FileOutputStream.


• Because OutputStream is an abstract class, we cannot create an object of
OutputStream.
By: Naol G. 23

• Methods of OutputStream
• The OutputStream class provides different methods that are
implemented by its subclasses. Here are some of the methods:
By: Naol G. 24

Example: OutputStream Using FileOutputStream

Here, we have created an output stream


using the FileOutputStream class.

The output stream is now linked with the file


output.txt.
By: Naol G. 25

2) Character Stream
• Character stream is used to read and write a single character of data.
• All the character stream classes are derived from base abstract
classes:
• Writer and
• Reader
By: Naol G. 26

Java Writer Class


• The Writer class of the java.io package.
• It is an abstract superclass that represents a stream of characters.
• Since Writer is an abstract class, it is not useful by itself.
• However, its subclasses can be used to write data.
By: Naol G. 27

• Subclasses of Writer
• In order to use the functionality of the Writer, we can use its subclasses.
Some of them are:
• BufferedWriter
• OutputStreamWriter
• FileWriter
• StringWriter

Output
By: Naol G. 28

• Create a Writer
• In order to create a Writer, we must import the java.io.Writer package first.
• Once we import the package, here is how we can create the writer.

• Here, we have created a writer named output using the FileWriter class.
• It is because the Writer is an abstract class.
• Hence we cannot create an object of Writer.
By: Naol G. 29
By: Naol G. 30
By: Naol G. 31
By: Naol G. 32

Example2: Writer Using FileWriter


By: Naol G. 33

Java Reader Class


• The Reader class is part of the java.io package.
• It is an abstract superclass that represents a stream of characters.
• Since Reader is an abstract class, it is not useful by itself.
• However, its subclasses can be used to read data.

• Subclasses of Reader
• In order to use the functionality of Reader, we can use its subclasses.
• Some of them are:
• BufferedReader
• InputStreamReader
• FileReader
• StringReader
By: Naol G. 34
By: Naol G. 35

• Create a Reader
• In order to create a Reader, we must import the java.io.Reader package first.
• Once we import the package, here is how we can create the reader.

• Here, we have created a reader using the FileReader class.


• It is because Reader is an abstract class.
• Hence we cannot create an object of Reader.
By: Naol G. 36
By: Naol G. 37

Example: Reader Using FileReader


Suppose we have a file named
input.txt with the following content.

“This is a line of text inside the file.”

Output

Is there data in the stream? true


Data in the stream:
This is a line of text inside the file.
By: Naol G. 38

• Read more on:


• Subclasses of InputStream/OutputStream
• Subclasses of Writer/Reader
• FilterClass

End

You might also like