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

JAVA-Notes-Unit-3_Part 1

This document provides an overview of file I/O operations in Java, focusing on I/O streams, including byte and character streams. It explains the classes available in the java.io package for reading and writing data, as well as basic file operations such as creating, deleting, and checking file properties. Additionally, it includes example programs demonstrating the use of byte and character streams for file handling.

Uploaded by

kishore0331
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

JAVA-Notes-Unit-3_Part 1

This document provides an overview of file I/O operations in Java, focusing on I/O streams, including byte and character streams. It explains the classes available in the java.io package for reading and writing data, as well as basic file operations such as creating, deleting, and checking file properties. Additionally, it includes example programs demonstrating the use of byte and character streams for file handling.

Uploaded by

kishore0331
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

OBJECT ORIENTED PROGRAMMING THROUGH JAVA UNIT-III

Files
Introduction to I/O Streams: Byte Streams, Character Streams. File I/O.

Introduction to I/O Streams in Java


I/O (Input/Output) streams are an essential part of Java programming that allows
the program to communicate with the outside world, whether it's reading data from
files, writing data to files, or interacting with other input/output devices like keyboards,
networks, or consoles.

In Java, the java.io package provides the classes needed to work with I/O
operations. The primary classes in this package allow reading from and writing to files,
consoles, memory, and other data sources.

Stream: A stream in Java is an abstraction that allows you to read or write data in a
continuous flow. It represents the flow of data between your program and some
external source (like a file, network connection, or keyboard).

Types of Streams in Java:

1. Byte Streams

2. Character Streams

B.Tech (CSE-DS)-II-II Sem Page 1


OBJECT ORIENTED PROGRAMMING THROUGH JAVA UNIT-III

1. Byte Streams:
o Byte Streams works with raw binary data and this is used to process data byte by
byte (8 bits).
o Used for handling raw binary data (e.g., image, audio, or video files).
o The basic classes for byte I/O are InputStream and OutputStream.
 InputStream: Used to read bytes from an input source.
 OutputStream: Used to write bytes to an output destination.

Stream class Description

BufferedInputStream It is used for Buffered Input Stream.

DataInputStream It contains method for reading java standard datatypes.

FileInputStream This is used to reads from a file

InputStream This is an abstract class that describes stream input.

PrintStream This contains the most used print() and println() method

BufferedOutputStream This is used for Buffered Output Stream.

DataOutputStream This contains method for writing java standard data types.

FileOutputStream This is used to write to a file.

OutputStream This is an abstract class that describe stream output.

B.Tech (CSE-DS)-II-II Sem Page 2


OBJECT ORIENTED PROGRAMMING THROUGH JAVA UNIT-III

//Program to demonstrate on ByteStreams


import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class ByteStreams


{
public static void main(String args[]) throws IOException
{
FileInputStream in = null;
FileOutputStream out = null;

try
{
in = new FileInputStream("input.txt");
out = new FileOutputStream("output.txt");
int c;
while ((c = in.read()) != -1)
{
out.write(c);
}
System.out.print("Data Transferred successfully");
}
finally
{
if (in != null)
{
in.close();
}
if (out != null)
{
out.close();
}
}
}
}

B.Tech (CSE-DS)-II-II Sem Page 3


OBJECT ORIENTED PROGRAMMING THROUGH JAVA UNIT-III

B.Tech (CSE-DS)-II-II Sem Page 4


OBJECT ORIENTED PROGRAMMING THROUGH JAVA UNIT-III

2. Character Streams:

Characters are stored using Unicode conventions. Character stream


automatically allows us to read/write data character by character. For example,
FileReader and FileWriter are character streams used to read from the source and
write to the destination.

o Used for handling text data (characters).


o The basic classes for character I/O are Reader and Writer.
 Reader: Used to read characters from an input source.
 Writer: Used to write characters to an output destination.

Stream class Description

BufferedReader It is used to handle buffered input stream.

FileReader This is an input stream that reads from file.

InputStreamReader This input stream is used to translate byte to character.

OutputStreamReader This output stream is used to translate character to byte.

Reader This is an abstract class that define character stream input.

PrintWriter This contains the most used print() and println() method

Writer This is an abstract class that define character stream output.

BufferedWriter This is used to handle buffered output stream.

FileWriter This is used to output stream that writes to fil//Program to


demonstrate on CharacterStreams

import java.io.FileReader;
B.Tech (CSE-DS)-II-II Sem Page 5
import java.io.FileWriter;
OBJECT ORIENTED PROGRAMMING THROUGH JAVA UNIT-III

//Program to demonstrate on CharacterStreams


import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class CharacterStreams


{
public static void main(String args[]) throws IOException
{
FileReader in = null;
FileWriter out = null;

try
{
in = new FileReader("input.txt");
out = new FileWriter("output.txt");
int c;
while ((c = in.read()) != -1)
{
out.write(c);
}
System.out.print("Data Transferred successfully");
}
finally
{
if (in != null)
{
in.close();
}
if (out != null)
{
out.close();
}
}
}
}

B.Tech (CSE-DS)-II-II Sem Page 6


OBJECT ORIENTED PROGRAMMING THROUGH JAVA UNIT-III

Difference Between Byte Stream and Character Stream

Byte Stream Character Stream


Byte stream is used to perform input and Character stream is used to perform input
output operations of 8-bit bytes. and output operations of 16-bit Unicode.
It processes data byte by byte. It processes data character by character.
Common classes for Byte stream are Common classes for Character streams are
FileInputStream and FileOutputStream. FileReader and FileWriter.

B.Tech (CSE-DS)-II-II Sem Page 7


OBJECT ORIENTED PROGRAMMING THROUGH JAVA UNIT-III

File I/O
File I/O (Input/Output) operations in Java are crucial for reading from and
writing to files. Java provides a comprehensive set of classes in the java.io package
that enable file handling. These classes are capable of working with both text files and
binary files.

The Java File class is an abstract representation of file.

Basic File Operations

The primary operations related to file handling include:

 Reading from a file


 Writing to a file
 Creating, deleting, or renaming files
 Checking file properties (exists, isDirectory, etc.)

//program to perform File I/O Operations


import java.io.File;
import java.io.IOException;
public class FileOperations
{
public static void main(String[] args)
{
// Create a File object for the file
File file = new File("new.txt");
// Check if file exists
if (file.exists())
{
System.out.println("File exists: " + file.getName());
System.out.println("File path: " + file.getAbsolutePath());
System.out.println("File size: " + file.length() + " bytes");
}
else
{
System.out.println("File does not exist.");
}

B.Tech (CSE-DS)-II-II Sem Page 8


OBJECT ORIENTED PROGRAMMING THROUGH JAVA UNIT-III

// Create a new file (if not already existing)


try
{
if (file.createNewFile())
{
System.out.println("File created: " + file.getName());
}
else
{
System.out.println("File already exists.");
}
}
catch (IOException e)
{
e.printStackTrace();
}
// Delete a file
if (file.delete())
{
System.out.println("File deleted: " + file.getName());
}
else
{
System.out.println("Failed to delete the file.");
}
}
}

B.Tech (CSE-DS)-II-II Sem Page 9

You might also like