SlideShare a Scribd company logo
Dr.Ranjitha M
Department of Computer Science[PG]
Kristu Jayanti College, Bengaluru
1
Kristu Jayanti College
Files and operations in Java
2
Kristu Jayanti College
java.io package
• java.io package contains almost every class you need to perform
input and output (I/O) in Java.
• Java programs perform I/O through streams.
• A stream is an abstraction that either produces or consumes
information.
• A stream is linked to a physical device by the Java I/O system.
• All streams behave in the same manner, even if the actual physical
devices to which they are linked differ.
3
Kristu Jayanti College
java.io package
• Same I/O classes and methods can be applied to different types of
devices.
• input stream can abstract many different kinds of input: from a disk file, a
keyboard, or a network socket.
• Streams are a clean way to deal with input/ output without having
every part of your code understand the difference between a
keyboard and a network.
4
Kristu Jayanti College
File operations
• Two of the most often-used stream classes are FileInputStream and
FileOutputStream
• Toopen a file, you simply create an object of one of these classes,
specifying the name of the file as an argument to the constructor.
FileInputStream(String fileName) throws FileNotFoundException
FileOutputStream(String fileName) throws
FileNotFoundException
• FileNotFoundException is a subclass of IOException.
• When an output file is opened, any preexisting file by the same name
is destroyed.
5
Kristu Jayanti College
File operations
• When you are done with a file, you must close it.
• This is done by calling the close( ) method, which is implemented by
both FileInputStream and FileOutputStream.
void close( ) throws IOException
• Closing a file releases the system resources allocated to the file,
allowing them to be used by another file.
• Failure to close a file can result in “memory leaks” because of unused
resources remaining allocated.
6
Kristu Jayanti College
File operations
• Toread from a file, you can use a version of read( ) that is defined
within FileInputStream.
int read( ) throws IOException
• Each time that it is called, it reads a single byte from the file and
returns the byte as an integer value.
• read( ) returns –1 when the end of the file is encountered.
• It can throw an IOException.
7
Kristu Jayanti College
File operation in Java
import java.io.*;
class ShowFile {
public static void main(String args[]) throws IOException {
int i;
FileInputStream fin;
if(args.length != 1) {// First, confirm that a filename has been specified.
System.out.println("Usage: ShowFile filename");
return;
}
try { // Attempt to open the file.
fin = new FileInputStream(args[0]);
} catch(FileNotFoundException e) {
System.out.println("Cannot Open File");
return;
}
// At this point, the file is open and can be read.
try { // The following reads characters until EOF is encountered.
do {
i = fin.read();
if(i != -1)
System.out.print((char) i);
} while(i != -1);
} catch(IOException e) {
System.out.println("Error Reading File");
}
try { // Close the file
fin.close();
} catch(IOException e) {
System.out.println("Error Closing File");
}
}
}
8
Kristu Jayanti College
File operation in Java
import java.io.*;
class ShowFile {
public static void main(String args[]) throws IOException {
int i;
FileInputStream fin = null;
if(args.length != 1) {// First, confirm that a filename has been specified.
System.out.println("Usage: ShowFile filename");
return;
}
try { // Attempt to open the file.
fin = new FileInputStream(args[0]);
do {
i = fin.read();
if(i != -1)
System.out.print((char) i);
} while(i != -1);
}
catch(FileNotFoundException e)
{ System.out.println("Cannot Open File");
return;
} catch(IOException e) {
System.out.println("Error Reading File");
}
finally {
// Close file in allcases.
try {
if(fin != null) fin.close();
} catch(IOException e) {
System.out.println("Error Closing File");
}
}// End finally
}//End main
}//End class
9
Kristu Jayanti College
File operations
• Towrite to a file, you can use the write( ) method defined by
FileOutputStream.
void write(int byteval) throws IOException
• This method writes the byte specified by byteval to the file.
• Although byteval is declared as an integer, only the low-order eight
bits are written to the file.
• If an error occurs during writing, an IOException is thrown.
1
0
Kristu Jayanti College
File operation in Java
import java.io.*; class
CopyFile {
public static void main(String args[]) throws IOException {
int i;
FileInputStream fin = null;
FileOutputStream fout = null;
// First, confirm that both files have been specified.
if(args.length != 2) {
System.out.println("Usage: CopyFile from to");
return; }
try { // Attempt to open the files.
fin = new FileInputStream(args[0]);
fout = new FileOutputStream(args[1]);
do {
i = fin.read();
if(i != -1) fout.write(i);
} while(i != -1);
} Indian Institute of Information TechnologyKottayam 10
catch(IOException e) {
System.out.println("I/O Error: " + e);
} finally{
try {
if(fin != null) fin.close();
} catch(IOException e2) {
System.out.println("Error Closing Input File");
}
try {
if(fout != null) fout.close();
} catch(IOException e2) {
System.out.println("Error Closing Output File");
}
}
}
}
1
1
Kristu Jayanti College

More Related Content

PPTX
Input output files in java
PPTX
File Handling.pptx
PPTX
Session 4 Try with Resources and Custom Exception.pptx
PDF
File Handling in Java.pdf
PPTX
UNIT 5 PY.pptx - FILE HANDLING CONCEPTS
PPTX
IO Programming.pptx all informatiyon ppt
Input output files in java
File Handling.pptx
Session 4 Try with Resources and Custom Exception.pptx
File Handling in Java.pdf
UNIT 5 PY.pptx - FILE HANDLING CONCEPTS
IO Programming.pptx all informatiyon ppt

Similar to Various types of File Operations in Java (20)

PPTX
Introduction to files management systems
PPTX
Java I/O
PPTX
file.pptx 43dcsddsafgdewdvvbghghsdwweffr
PPTX
File Handling
PPTX
File Handling
PDF
OOPs with Java_unit2.pdf. sarthak bookkk
PPTX
chapter 2(IO and stream)/chapter 2, IO and stream
PDF
DOC-20241121-WA0004bwushshusjssjuwh..pdf
PPTX
Unit3 part3-packages and interfaces
PPTX
Unit3 packages & interfaces
PPT
04_1.Exceptionssssssssssssssssssss..pptt
PPTX
Working with files in c++. file handling
PPT
Data file handling
PDF
File operations
PDF
What is java input and output stream?
PDF
What is java input and output stream?
PPT
csc1201_lecture13.ppt
PPTX
Chapter4.pptx
Introduction to files management systems
Java I/O
file.pptx 43dcsddsafgdewdvvbghghsdwweffr
File Handling
File Handling
OOPs with Java_unit2.pdf. sarthak bookkk
chapter 2(IO and stream)/chapter 2, IO and stream
DOC-20241121-WA0004bwushshusjssjuwh..pdf
Unit3 part3-packages and interfaces
Unit3 packages & interfaces
04_1.Exceptionssssssssssssssssssss..pptt
Working with files in c++. file handling
Data file handling
File operations
What is java input and output stream?
What is java input and output stream?
csc1201_lecture13.ppt
Chapter4.pptx
Ad

More from RanjithaM32 (14)

PPTX
Gives an overview of intelligent storage system
PPT
svm.ppt
PPTX
Unit 4 Ethics in health research.pptx
PPTX
Unit 1 NoSQL commands.pptx
PPTX
Java Multithreading.pptx
PPT
Java Exception.ppt
PPTX
ARtificial Intelligence Knowledge Representation.pptx
PPTX
Lisp.pptx
PPT
Java interfaces
PDF
Html introduction
PPT
Threads
PDF
Types of learning
PDF
Ml introduction
PPTX
Knowledge base system
Gives an overview of intelligent storage system
svm.ppt
Unit 4 Ethics in health research.pptx
Unit 1 NoSQL commands.pptx
Java Multithreading.pptx
Java Exception.ppt
ARtificial Intelligence Knowledge Representation.pptx
Lisp.pptx
Java interfaces
Html introduction
Threads
Types of learning
Ml introduction
Knowledge base system
Ad

Recently uploaded (20)

PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
A comparative analysis of optical character recognition models for extracting...
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
sap open course for s4hana steps from ECC to s4
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
Big Data Technologies - Introduction.pptx
PPTX
A Presentation on Artificial Intelligence
PDF
Approach and Philosophy of On baking technology
PDF
Network Security Unit 5.pdf for BCA BBA.
PPT
Teaching material agriculture food technology
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Spectral efficient network and resource selection model in 5G networks
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
A comparative analysis of optical character recognition models for extracting...
20250228 LYD VKU AI Blended-Learning.pptx
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
sap open course for s4hana steps from ECC to s4
MIND Revenue Release Quarter 2 2025 Press Release
Mobile App Security Testing_ A Comprehensive Guide.pdf
Diabetes mellitus diagnosis method based random forest with bat algorithm
Chapter 3 Spatial Domain Image Processing.pdf
Big Data Technologies - Introduction.pptx
A Presentation on Artificial Intelligence
Approach and Philosophy of On baking technology
Network Security Unit 5.pdf for BCA BBA.
Teaching material agriculture food technology
MYSQL Presentation for SQL database connectivity
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...

Various types of File Operations in Java

  • 1. Dr.Ranjitha M Department of Computer Science[PG] Kristu Jayanti College, Bengaluru 1 Kristu Jayanti College
  • 2. Files and operations in Java 2 Kristu Jayanti College
  • 3. java.io package • java.io package contains almost every class you need to perform input and output (I/O) in Java. • Java programs perform I/O through streams. • A stream is an abstraction that either produces or consumes information. • A stream is linked to a physical device by the Java I/O system. • All streams behave in the same manner, even if the actual physical devices to which they are linked differ. 3 Kristu Jayanti College
  • 4. java.io package • Same I/O classes and methods can be applied to different types of devices. • input stream can abstract many different kinds of input: from a disk file, a keyboard, or a network socket. • Streams are a clean way to deal with input/ output without having every part of your code understand the difference between a keyboard and a network. 4 Kristu Jayanti College
  • 5. File operations • Two of the most often-used stream classes are FileInputStream and FileOutputStream • Toopen a file, you simply create an object of one of these classes, specifying the name of the file as an argument to the constructor. FileInputStream(String fileName) throws FileNotFoundException FileOutputStream(String fileName) throws FileNotFoundException • FileNotFoundException is a subclass of IOException. • When an output file is opened, any preexisting file by the same name is destroyed. 5 Kristu Jayanti College
  • 6. File operations • When you are done with a file, you must close it. • This is done by calling the close( ) method, which is implemented by both FileInputStream and FileOutputStream. void close( ) throws IOException • Closing a file releases the system resources allocated to the file, allowing them to be used by another file. • Failure to close a file can result in “memory leaks” because of unused resources remaining allocated. 6 Kristu Jayanti College
  • 7. File operations • Toread from a file, you can use a version of read( ) that is defined within FileInputStream. int read( ) throws IOException • Each time that it is called, it reads a single byte from the file and returns the byte as an integer value. • read( ) returns –1 when the end of the file is encountered. • It can throw an IOException. 7 Kristu Jayanti College
  • 8. File operation in Java import java.io.*; class ShowFile { public static void main(String args[]) throws IOException { int i; FileInputStream fin; if(args.length != 1) {// First, confirm that a filename has been specified. System.out.println("Usage: ShowFile filename"); return; } try { // Attempt to open the file. fin = new FileInputStream(args[0]); } catch(FileNotFoundException e) { System.out.println("Cannot Open File"); return; } // At this point, the file is open and can be read. try { // The following reads characters until EOF is encountered. do { i = fin.read(); if(i != -1) System.out.print((char) i); } while(i != -1); } catch(IOException e) { System.out.println("Error Reading File"); } try { // Close the file fin.close(); } catch(IOException e) { System.out.println("Error Closing File"); } } } 8 Kristu Jayanti College
  • 9. File operation in Java import java.io.*; class ShowFile { public static void main(String args[]) throws IOException { int i; FileInputStream fin = null; if(args.length != 1) {// First, confirm that a filename has been specified. System.out.println("Usage: ShowFile filename"); return; } try { // Attempt to open the file. fin = new FileInputStream(args[0]); do { i = fin.read(); if(i != -1) System.out.print((char) i); } while(i != -1); } catch(FileNotFoundException e) { System.out.println("Cannot Open File"); return; } catch(IOException e) { System.out.println("Error Reading File"); } finally { // Close file in allcases. try { if(fin != null) fin.close(); } catch(IOException e) { System.out.println("Error Closing File"); } }// End finally }//End main }//End class 9 Kristu Jayanti College
  • 10. File operations • Towrite to a file, you can use the write( ) method defined by FileOutputStream. void write(int byteval) throws IOException • This method writes the byte specified by byteval to the file. • Although byteval is declared as an integer, only the low-order eight bits are written to the file. • If an error occurs during writing, an IOException is thrown. 1 0 Kristu Jayanti College
  • 11. File operation in Java import java.io.*; class CopyFile { public static void main(String args[]) throws IOException { int i; FileInputStream fin = null; FileOutputStream fout = null; // First, confirm that both files have been specified. if(args.length != 2) { System.out.println("Usage: CopyFile from to"); return; } try { // Attempt to open the files. fin = new FileInputStream(args[0]); fout = new FileOutputStream(args[1]); do { i = fin.read(); if(i != -1) fout.write(i); } while(i != -1); } Indian Institute of Information TechnologyKottayam 10 catch(IOException e) { System.out.println("I/O Error: " + e); } finally{ try { if(fin != null) fin.close(); } catch(IOException e2) { System.out.println("Error Closing Input File"); } try { if(fout != null) fout.close(); } catch(IOException e2) { System.out.println("Error Closing Output File"); } } } } 1 1 Kristu Jayanti College