SlideShare a Scribd company logo
Programming in Java
Topic: Managing Files & I/O
Handling
JAVA I/O
• Java I/O (Input and Output) is used to process the input and produce
the output.
• Java uses the concept of a stream to make I/O operation fast. The
java.io package contains all the classes required for input and output
operations.
• We can perform file handling in Java by Java I/O API.
19-05-2022 "Programming in JAVA" 1
Stream
A stream is a sequence of data. In Java, a stream is composed of bytes. It's
called a stream because it is like a stream of water that continues to flow.
In Java, 3 streams are created for us automatically. All these streams are
attached with the console.
1) System.out: standard output stream
2) System.in: standard input stream
3) System.err: standard error stream
19-05-2022 "Programming in JAVA" 2
The explanation of OutputStream and InputStream classes are given below:
OutputStream
Java application uses an output stream to write data to a destination; it may be a file, an array, peripheral device or socket.
InputStream
Java application uses an input stream to read data from a source; it may be a file, an array, peripheral device or socket.
Let's understand the working of Java OutputStream and InputStream by the figure given below.
19-05-2022 "Programming in JAVA" 3
OutputStream vs InputStream
Java FileOutputStream Class
• Java FileOutputStream is an output stream used for writing data to
a file.
• If you have to write primitive values into a file, use FileOutputStream
class. You can write byte-oriented as well as character-oriented data
through FileOutputStream class. But, for character-oriented data, it is
preferred to use FileWriter than FileOutputStream.
19-05-2022 "Programming in JAVA" 4
import java.io.FileOutputStream;
public class FileOutputStreamExample {
public static void main(String args[]){
try{
FileOutputStream fout=new FileOutputStream("D:testout.txt");
fout.write(65);
fout.close();
System.out.println("success...");
}catch(Exception e){System.out.println(e);}
}
}
19-05-2022 "Programming in JAVA" 5
Output:
Success...
The content of a text file testout.txt is set with
the data A.
testout.txt
A
import java.io.FileOutputStream;
public class FileOutputStreamExample {
public static void main(String args[]){
try{
FileOutputStream fout=new FileOutputStream("D:testout.txt");
String s="Welcome to javaTpoint.";
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);}
}
}
19-05-2022 "Programming in JAVA" 6
Output:
Success...
The content of a text file testout.txt is set with the
data Welcome to javaTpoint.
testout.txt
Welcome to javaTpoint.
Java FileInputStream Class
• Java FileInputStream class obtains input bytes from a file. It is used for
reading byte-oriented data (streams of raw bytes) such as image data,
audio, video etc.
• You can also read character-stream data. But, for reading streams of
characters, it is recommended to use FileReader class.
19-05-2022 "Programming in JAVA" 7
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);}
}
}
19-05-2022 "Programming in JAVA" 8
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);}
}
}
19-05-2022 "Programming in JAVA" 9
Demonstration
19-05-2022 "Programming in JAVA" 10

More Related Content

Similar to 1.26 File Input-Output in JAVA.pptx

Itp 120 Chapt 19 2009 Binary Input & Output
Itp 120 Chapt 19 2009 Binary Input & OutputItp 120 Chapt 19 2009 Binary Input & Output
Itp 120 Chapt 19 2009 Binary Input & Output
phanleson
 
Files io
Files ioFiles io
Files io
Narayana Swamy
 
CORE JAVA-1
CORE JAVA-1CORE JAVA-1
Java I/o streams
Java I/o streamsJava I/o streams
Java I/o streams
Hamid Ghorbani
 
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
Jyoti Verma
 
Core Java Programming Language (JSE) : Chapter XI - Console I/O and File I/O
Core Java Programming Language (JSE) : Chapter XI - Console I/O and File I/OCore Java Programming Language (JSE) : Chapter XI - Console I/O and File I/O
Core Java Programming Language (JSE) : Chapter XI - Console I/O and File I/O
WebStackAcademy
 
IOStream.pptx
IOStream.pptxIOStream.pptx
IOStream.pptx
HindAlmisbahi
 
Buffer and scanner
Buffer and scannerBuffer and scanner
Buffer and scanner
Arif Ullah
 
File Handling.pptx
File Handling.pptxFile Handling.pptx
File Handling.pptx
PragatiSutar4
 
Java Wrapper Classes and I/O Mechanisms
Java Wrapper Classes and I/O MechanismsJava Wrapper Classes and I/O Mechanisms
Java Wrapper Classes and I/O Mechanisms
Subhadra Sundar Chakraborty
 
Java I/O
Java I/OJava I/O
CHAPTER 5 mechanical engineeringasaaa.pptx
CHAPTER 5 mechanical engineeringasaaa.pptxCHAPTER 5 mechanical engineeringasaaa.pptx
CHAPTER 5 mechanical engineeringasaaa.pptx
SadhilAggarwal
 
Io streams
Io streamsIo streams
Understanding java streams
Understanding java streamsUnderstanding java streams
Understanding java streams
Shahjahan Samoon
 
Java Notes by C. Sreedhar, GPREC
Java Notes by C. Sreedhar, GPRECJava Notes by C. Sreedhar, GPREC
Java Notes by C. Sreedhar, GPREC
Sreedhar Chowdam
 
Java Notes
Java Notes Java Notes
Java Notes
Sreedhar Chowdam
 
CSE3146-ADV JAVA M2.pdf
CSE3146-ADV JAVA M2.pdfCSE3146-ADV JAVA M2.pdf
CSE3146-ADV JAVA M2.pdf
VithalReddy3
 
Io stream
Io streamIo stream
Io stream
Parthipan Parthi
 
Java platform
Java platformJava platform
Java platform
BG Java EE Course
 
Computer science input and output BASICS.pptx
Computer science input and output BASICS.pptxComputer science input and output BASICS.pptx
Computer science input and output BASICS.pptx
RathanMB
 

Similar to 1.26 File Input-Output in JAVA.pptx (20)

Itp 120 Chapt 19 2009 Binary Input & Output
Itp 120 Chapt 19 2009 Binary Input & OutputItp 120 Chapt 19 2009 Binary Input & Output
Itp 120 Chapt 19 2009 Binary Input & Output
 
Files io
Files ioFiles io
Files io
 
CORE JAVA-1
CORE JAVA-1CORE JAVA-1
CORE JAVA-1
 
Java I/o streams
Java I/o streamsJava I/o streams
Java I/o streams
 
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
 
Core Java Programming Language (JSE) : Chapter XI - Console I/O and File I/O
Core Java Programming Language (JSE) : Chapter XI - Console I/O and File I/OCore Java Programming Language (JSE) : Chapter XI - Console I/O and File I/O
Core Java Programming Language (JSE) : Chapter XI - Console I/O and File I/O
 
IOStream.pptx
IOStream.pptxIOStream.pptx
IOStream.pptx
 
Buffer and scanner
Buffer and scannerBuffer and scanner
Buffer and scanner
 
File Handling.pptx
File Handling.pptxFile Handling.pptx
File Handling.pptx
 
Java Wrapper Classes and I/O Mechanisms
Java Wrapper Classes and I/O MechanismsJava Wrapper Classes and I/O Mechanisms
Java Wrapper Classes and I/O Mechanisms
 
Java I/O
Java I/OJava I/O
Java I/O
 
CHAPTER 5 mechanical engineeringasaaa.pptx
CHAPTER 5 mechanical engineeringasaaa.pptxCHAPTER 5 mechanical engineeringasaaa.pptx
CHAPTER 5 mechanical engineeringasaaa.pptx
 
Io streams
Io streamsIo streams
Io streams
 
Understanding java streams
Understanding java streamsUnderstanding java streams
Understanding java streams
 
Java Notes by C. Sreedhar, GPREC
Java Notes by C. Sreedhar, GPRECJava Notes by C. Sreedhar, GPREC
Java Notes by C. Sreedhar, GPREC
 
Java Notes
Java Notes Java Notes
Java Notes
 
CSE3146-ADV JAVA M2.pdf
CSE3146-ADV JAVA M2.pdfCSE3146-ADV JAVA M2.pdf
CSE3146-ADV JAVA M2.pdf
 
Io stream
Io streamIo stream
Io stream
 
Java platform
Java platformJava platform
Java platform
 
Computer science input and output BASICS.pptx
Computer science input and output BASICS.pptxComputer science input and output BASICS.pptx
Computer science input and output BASICS.pptx
 

Recently uploaded

Independent Call Girls In Kolkata ✔ 7014168258 ✔ Hi I Am Divya Vip Call Girl ...
Independent Call Girls In Kolkata ✔ 7014168258 ✔ Hi I Am Divya Vip Call Girl ...Independent Call Girls In Kolkata ✔ 7014168258 ✔ Hi I Am Divya Vip Call Girl ...
Independent Call Girls In Kolkata ✔ 7014168258 ✔ Hi I Am Divya Vip Call Girl ...
simmi singh
 
Extreme DDD Modelling Patterns - 2024 Devoxx Poland
Extreme DDD Modelling Patterns - 2024 Devoxx PolandExtreme DDD Modelling Patterns - 2024 Devoxx Poland
Extreme DDD Modelling Patterns - 2024 Devoxx Poland
Alberto Brandolini
 
Orca: Nocode Graphical Editor for Container Orchestration
Orca: Nocode Graphical Editor for Container OrchestrationOrca: Nocode Graphical Editor for Container Orchestration
Orca: Nocode Graphical Editor for Container Orchestration
Pedro J. Molina
 
TheFutureIsDynamic-BoxLang-CFCamp2024.pdf
TheFutureIsDynamic-BoxLang-CFCamp2024.pdfTheFutureIsDynamic-BoxLang-CFCamp2024.pdf
TheFutureIsDynamic-BoxLang-CFCamp2024.pdf
Ortus Solutions, Corp
 
Ensuring Efficiency and Speed with Practical Solutions for Clinical Operations
Ensuring Efficiency and Speed with Practical Solutions for Clinical OperationsEnsuring Efficiency and Speed with Practical Solutions for Clinical Operations
Ensuring Efficiency and Speed with Practical Solutions for Clinical Operations
OnePlan Solutions
 
🏎️Tech Transformation: DevOps Insights from the Experts 👩‍💻
🏎️Tech Transformation: DevOps Insights from the Experts 👩‍💻🏎️Tech Transformation: DevOps Insights from the Experts 👩‍💻
🏎️Tech Transformation: DevOps Insights from the Experts 👩‍💻
campbellclarkson
 
Microsoft-Power-Platform-Adoption-Planning.pptx
Microsoft-Power-Platform-Adoption-Planning.pptxMicrosoft-Power-Platform-Adoption-Planning.pptx
Microsoft-Power-Platform-Adoption-Planning.pptx
jrodriguezq3110
 
Top Call Girls Lucknow ✔ 9352988975 ✔ Hi I Am Divya Vip Call Girl Services Pr...
Top Call Girls Lucknow ✔ 9352988975 ✔ Hi I Am Divya Vip Call Girl Services Pr...Top Call Girls Lucknow ✔ 9352988975 ✔ Hi I Am Divya Vip Call Girl Services Pr...
Top Call Girls Lucknow ✔ 9352988975 ✔ Hi I Am Divya Vip Call Girl Services Pr...
simmi singh
 
Cost-Effective Strategies For iOS App Development
Cost-Effective Strategies For iOS App DevelopmentCost-Effective Strategies For iOS App Development
Cost-Effective Strategies For iOS App Development
Softradix Technologies
 
OpenChain Webinar - Open Source Due Diligence for M&A - 2024-06-17
OpenChain Webinar - Open Source Due Diligence for M&A - 2024-06-17OpenChain Webinar - Open Source Due Diligence for M&A - 2024-06-17
OpenChain Webinar - Open Source Due Diligence for M&A - 2024-06-17
Shane Coughlan
 
Beginner's Guide to Observability@Devoxx PL 2024
Beginner's  Guide to Observability@Devoxx PL 2024Beginner's  Guide to Observability@Devoxx PL 2024
Beginner's Guide to Observability@Devoxx PL 2024
michniczscribd
 
Erotic Call Girls Bangalore🫱9079923931🫲 High Quality Call Girl Service Right ...
Erotic Call Girls Bangalore🫱9079923931🫲 High Quality Call Girl Service Right ...Erotic Call Girls Bangalore🫱9079923931🫲 High Quality Call Girl Service Right ...
Erotic Call Girls Bangalore🫱9079923931🫲 High Quality Call Girl Service Right ...
meenusingh4354543
 
Building API data products on top of your real-time data infrastructure
Building API data products on top of your real-time data infrastructureBuilding API data products on top of your real-time data infrastructure
Building API data products on top of your real-time data infrastructure
confluent
 
Strengthening Web Development with CommandBox 6: Seamless Transition and Scal...
Strengthening Web Development with CommandBox 6: Seamless Transition and Scal...Strengthening Web Development with CommandBox 6: Seamless Transition and Scal...
Strengthening Web Development with CommandBox 6: Seamless Transition and Scal...
Ortus Solutions, Corp
 
42 Ways to Generate Real Estate Leads - Sellxpert
42 Ways to Generate Real Estate Leads - Sellxpert42 Ways to Generate Real Estate Leads - Sellxpert
42 Ways to Generate Real Estate Leads - Sellxpert
vaishalijagtap12
 
美洲杯赔率投注网【​网址​🎉3977·EE​🎉】
美洲杯赔率投注网【​网址​🎉3977·EE​🎉】美洲杯赔率投注网【​网址​🎉3977·EE​🎉】
美洲杯赔率投注网【​网址​🎉3977·EE​🎉】
widenerjobeyrl638
 
The Power of Visual Regression Testing_ Why It Is Critical for Enterprise App...
The Power of Visual Regression Testing_ Why It Is Critical for Enterprise App...The Power of Visual Regression Testing_ Why It Is Critical for Enterprise App...
The Power of Visual Regression Testing_ Why It Is Critical for Enterprise App...
kalichargn70th171
 
Call Girls in Varanasi || 7426014248 || Quick Booking at Affordable Price
Call Girls in Varanasi || 7426014248 || Quick Booking at Affordable PriceCall Girls in Varanasi || 7426014248 || Quick Booking at Affordable Price
Call Girls in Varanasi || 7426014248 || Quick Booking at Affordable Price
vickythakur209464
 
Trailhead Talks_ Journey of an All-Star Ranger .pptx
Trailhead Talks_ Journey of an All-Star Ranger .pptxTrailhead Talks_ Journey of an All-Star Ranger .pptx
Trailhead Talks_ Journey of an All-Star Ranger .pptx
ImtiazBinMohiuddin
 

Recently uploaded (20)

Independent Call Girls In Kolkata ✔ 7014168258 ✔ Hi I Am Divya Vip Call Girl ...
Independent Call Girls In Kolkata ✔ 7014168258 ✔ Hi I Am Divya Vip Call Girl ...Independent Call Girls In Kolkata ✔ 7014168258 ✔ Hi I Am Divya Vip Call Girl ...
Independent Call Girls In Kolkata ✔ 7014168258 ✔ Hi I Am Divya Vip Call Girl ...
 
Extreme DDD Modelling Patterns - 2024 Devoxx Poland
Extreme DDD Modelling Patterns - 2024 Devoxx PolandExtreme DDD Modelling Patterns - 2024 Devoxx Poland
Extreme DDD Modelling Patterns - 2024 Devoxx Poland
 
Orca: Nocode Graphical Editor for Container Orchestration
Orca: Nocode Graphical Editor for Container OrchestrationOrca: Nocode Graphical Editor for Container Orchestration
Orca: Nocode Graphical Editor for Container Orchestration
 
TheFutureIsDynamic-BoxLang-CFCamp2024.pdf
TheFutureIsDynamic-BoxLang-CFCamp2024.pdfTheFutureIsDynamic-BoxLang-CFCamp2024.pdf
TheFutureIsDynamic-BoxLang-CFCamp2024.pdf
 
bgiolcb
bgiolcbbgiolcb
bgiolcb
 
Ensuring Efficiency and Speed with Practical Solutions for Clinical Operations
Ensuring Efficiency and Speed with Practical Solutions for Clinical OperationsEnsuring Efficiency and Speed with Practical Solutions for Clinical Operations
Ensuring Efficiency and Speed with Practical Solutions for Clinical Operations
 
🏎️Tech Transformation: DevOps Insights from the Experts 👩‍💻
🏎️Tech Transformation: DevOps Insights from the Experts 👩‍💻🏎️Tech Transformation: DevOps Insights from the Experts 👩‍💻
🏎️Tech Transformation: DevOps Insights from the Experts 👩‍💻
 
Microsoft-Power-Platform-Adoption-Planning.pptx
Microsoft-Power-Platform-Adoption-Planning.pptxMicrosoft-Power-Platform-Adoption-Planning.pptx
Microsoft-Power-Platform-Adoption-Planning.pptx
 
Top Call Girls Lucknow ✔ 9352988975 ✔ Hi I Am Divya Vip Call Girl Services Pr...
Top Call Girls Lucknow ✔ 9352988975 ✔ Hi I Am Divya Vip Call Girl Services Pr...Top Call Girls Lucknow ✔ 9352988975 ✔ Hi I Am Divya Vip Call Girl Services Pr...
Top Call Girls Lucknow ✔ 9352988975 ✔ Hi I Am Divya Vip Call Girl Services Pr...
 
Cost-Effective Strategies For iOS App Development
Cost-Effective Strategies For iOS App DevelopmentCost-Effective Strategies For iOS App Development
Cost-Effective Strategies For iOS App Development
 
OpenChain Webinar - Open Source Due Diligence for M&A - 2024-06-17
OpenChain Webinar - Open Source Due Diligence for M&A - 2024-06-17OpenChain Webinar - Open Source Due Diligence for M&A - 2024-06-17
OpenChain Webinar - Open Source Due Diligence for M&A - 2024-06-17
 
Beginner's Guide to Observability@Devoxx PL 2024
Beginner's  Guide to Observability@Devoxx PL 2024Beginner's  Guide to Observability@Devoxx PL 2024
Beginner's Guide to Observability@Devoxx PL 2024
 
Erotic Call Girls Bangalore🫱9079923931🫲 High Quality Call Girl Service Right ...
Erotic Call Girls Bangalore🫱9079923931🫲 High Quality Call Girl Service Right ...Erotic Call Girls Bangalore🫱9079923931🫲 High Quality Call Girl Service Right ...
Erotic Call Girls Bangalore🫱9079923931🫲 High Quality Call Girl Service Right ...
 
Building API data products on top of your real-time data infrastructure
Building API data products on top of your real-time data infrastructureBuilding API data products on top of your real-time data infrastructure
Building API data products on top of your real-time data infrastructure
 
Strengthening Web Development with CommandBox 6: Seamless Transition and Scal...
Strengthening Web Development with CommandBox 6: Seamless Transition and Scal...Strengthening Web Development with CommandBox 6: Seamless Transition and Scal...
Strengthening Web Development with CommandBox 6: Seamless Transition and Scal...
 
42 Ways to Generate Real Estate Leads - Sellxpert
42 Ways to Generate Real Estate Leads - Sellxpert42 Ways to Generate Real Estate Leads - Sellxpert
42 Ways to Generate Real Estate Leads - Sellxpert
 
美洲杯赔率投注网【​网址​🎉3977·EE​🎉】
美洲杯赔率投注网【​网址​🎉3977·EE​🎉】美洲杯赔率投注网【​网址​🎉3977·EE​🎉】
美洲杯赔率投注网【​网址​🎉3977·EE​🎉】
 
The Power of Visual Regression Testing_ Why It Is Critical for Enterprise App...
The Power of Visual Regression Testing_ Why It Is Critical for Enterprise App...The Power of Visual Regression Testing_ Why It Is Critical for Enterprise App...
The Power of Visual Regression Testing_ Why It Is Critical for Enterprise App...
 
Call Girls in Varanasi || 7426014248 || Quick Booking at Affordable Price
Call Girls in Varanasi || 7426014248 || Quick Booking at Affordable PriceCall Girls in Varanasi || 7426014248 || Quick Booking at Affordable Price
Call Girls in Varanasi || 7426014248 || Quick Booking at Affordable Price
 
Trailhead Talks_ Journey of an All-Star Ranger .pptx
Trailhead Talks_ Journey of an All-Star Ranger .pptxTrailhead Talks_ Journey of an All-Star Ranger .pptx
Trailhead Talks_ Journey of an All-Star Ranger .pptx
 

1.26 File Input-Output in JAVA.pptx

  • 1. Programming in Java Topic: Managing Files & I/O Handling
  • 2. JAVA I/O • Java I/O (Input and Output) is used to process the input and produce the output. • Java uses the concept of a stream to make I/O operation fast. The java.io package contains all the classes required for input and output operations. • We can perform file handling in Java by Java I/O API. 19-05-2022 "Programming in JAVA" 1
  • 3. Stream A stream is a sequence of data. In Java, a stream is composed of bytes. It's called a stream because it is like a stream of water that continues to flow. In Java, 3 streams are created for us automatically. All these streams are attached with the console. 1) System.out: standard output stream 2) System.in: standard input stream 3) System.err: standard error stream 19-05-2022 "Programming in JAVA" 2
  • 4. The explanation of OutputStream and InputStream classes are given below: OutputStream Java application uses an output stream to write data to a destination; it may be a file, an array, peripheral device or socket. InputStream Java application uses an input stream to read data from a source; it may be a file, an array, peripheral device or socket. Let's understand the working of Java OutputStream and InputStream by the figure given below. 19-05-2022 "Programming in JAVA" 3 OutputStream vs InputStream
  • 5. Java FileOutputStream Class • Java FileOutputStream is an output stream used for writing data to a file. • If you have to write primitive values into a file, use FileOutputStream class. You can write byte-oriented as well as character-oriented data through FileOutputStream class. But, for character-oriented data, it is preferred to use FileWriter than FileOutputStream. 19-05-2022 "Programming in JAVA" 4
  • 6. import java.io.FileOutputStream; public class FileOutputStreamExample { public static void main(String args[]){ try{ FileOutputStream fout=new FileOutputStream("D:testout.txt"); fout.write(65); fout.close(); System.out.println("success..."); }catch(Exception e){System.out.println(e);} } } 19-05-2022 "Programming in JAVA" 5 Output: Success... The content of a text file testout.txt is set with the data A. testout.txt A
  • 7. import java.io.FileOutputStream; public class FileOutputStreamExample { public static void main(String args[]){ try{ FileOutputStream fout=new FileOutputStream("D:testout.txt"); String s="Welcome to javaTpoint."; 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);} } } 19-05-2022 "Programming in JAVA" 6 Output: Success... The content of a text file testout.txt is set with the data Welcome to javaTpoint. testout.txt Welcome to javaTpoint.
  • 8. Java FileInputStream Class • Java FileInputStream class obtains input bytes from a file. It is used for reading byte-oriented data (streams of raw bytes) such as image data, audio, video etc. • You can also read character-stream data. But, for reading streams of characters, it is recommended to use FileReader class. 19-05-2022 "Programming in JAVA" 7
  • 9. 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);} } } 19-05-2022 "Programming in JAVA" 8
  • 10. 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);} } } 19-05-2022 "Programming in JAVA" 9