SlideShare a Scribd company logo
1 of 20
Sardar VallabhBhai Patel Institute
of Technology
 Name- RAJAN
SHAH
 Topic: Files In
JAVA
I/O Stream
 Input Stream: It is a stream which may be
connected with different i/p devices like
keyboard, file or network socket. It is used to
generate data.
 Output Stream: It is used to produce data and
may be connected with different output devices
like monitor, file, network socket.
Files and Streams
 File processing with classes in package java.io
 FileInputStream for
byte-by-byte input from a file
 FileOutputStream for
byte-by-byte output to a file
 FileReader for char-by-char
input from a file
 FileWriter for char-by-char
output to a file
Difference
byte Stream char Stream
1. It deals with byte.
2. Used to manipulate
data in binary
format.
3. Mainly uses audio,
video, img, txt files.
4. InputStream and
OutputStream are
top two abstract
classes of byte
Stream.
1. It deals with char or
Strings.
2. Used to manipulate
data in Unicode.
3. Can be used only
with txt files.
4. Reader & Writer are
top two abstract
classes of char
Stream.
byte-by-byte
Constructors, Methods Of
FileInputStream
 CONSTRUCTORS:
 FileInputStream(String fname);
 FileInputStream(File F);
 FileInputStream(String fold, String fname);
 METHODS:
 int read(); // n= fis.read(); // n is the byte read.
Constructors, Methods Of
FileOutputStream
 CONSTRUCTORS:
 FileOutputStream(String fname);
 FileOutputStream(File F);
 FileOutputStream(String fname, boolean app);
 METHODS:
 void write(int n); //write(n); //n is the byte to be
written
 void write(byte []b); // writes the array contents to
file.
Example: FileInputStream
 import java.io.*;
public class ReadStringFromFile {
public static void main(String[] args)
{
File file = new File("C://FileIO//ReadString.txt");
int ch;
StringBuffer strContent = new StringBuffer("");
FileInputStream fin = null;
try
{
fin = new FileInputStream(file);
Continue...
while( (ch = fin.read()) != -1)
strContent.append((char)ch);
fin.close();
}
catch(Exception e){
System.out.println(e);
}
System.out.println("File contents :");
System.out.println(strContent);
}
}
Example: FileOutputStream
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
public class FileOutputStreamDemo {
public static void main(String[] args) throws IOException
{
byte[] b = {65,66,67,68,69};
int i=0;
char c;
try{
FileOutputStream fos=new
FileOutputStream("C://test.txt");
fos.write(b);
Continue...
fos.flush();
FileInputStream fis = new FileInputStream("C://test.txt");
while((i=fis.read())!=-1) {
c=(char)i;
System.out.print(c); }
}catch(Exception ex) {ex.printStackTrace(); }
finally{
if(fos!=null)
fos.close();
if(fis!=null)
fis.close(); }
} }
char-by-char
Constructors, Methods Of FileReader
 CONSTRUCTORS:
 FileReader(String fname);
 FileReader(File F);
 FileReader(String fold, String fname);
 METHODS:
 int read(); // n= fis.read(); // n is the char read.
 int read(char [] c, int offset, int len); // reads
character into an array and returns the number of
chars read.
Constructors, Methods Of FileWriter
 CONSTRUCTORS:
 FileWriter(String fname);
 FileWriter(File F);
 FileWriter(String fold, String fname);
 METHODS:
 void write(String n); // write(n); //n is the String to be
written to file.
 public void write(char [] c, int offset, int len); // Writes a
portion of an array of characters starting from offset
and with a length of len.
 public void write(String s, int offset, int len);
Example: FileReader
 import java.io.*;
public class FileRead {
public static void main(String args[]) throws
IOException {
File file = new File("Hello1.txt");
file.createNewFile(); //returns true if file is created
FileWriter fw = new FileWriter(file);
fw.write("Thisn isn ann examplen");
fw.flush();
fw.close();
Continue...
FileReader fr = new FileReader(file);
char [] a = new char[50];
fr.read(a); //reads content to array
for(char c : a)
System.out.print(c);
fr.close(); }
}
Example: FileReader using
BufferedReader
 import java.io.*;
public class Reader {
public static void main(String[]args) throws
IOException{
FileReader fr = new FileReader("C:/test.txt");
BufferedReader br = new BufferedReader(in);
while (br.readLine() != null)
{ System.out.println(br.readLine()); }
fr.close();
}
Example: FileWriter
• import java.io.*;
class Simple{
public static void main(String args[]){
try{
FileWriter fw=new FileWriter("abc.txt");
fw.write(“Hello World");
fw.close();
}
catch(Exception e) {System.out.println(e);}
System.out.println(“Success");
}
}
 import java.io.BufferedWriter;
 import java.io.File;
 import java.io.FileWriter;
 import java.io.IOException;
public class BufferedWriterDemo {
public static void main(String[] args) throws IOException
{
File f = new File("java2learn.txt");
System.out.println(f.exists());
BufferedWriter bw = new BufferedWriter(new
FileWriter(f));
char[] ch1 = { 'a', 'b', 'c', 'd' };
bw.write(ch1);
} }
THANK
YOU

More Related Content

What's hot (20)

Web Development Course: PHP lecture 1
Web Development Course: PHP lecture 1Web Development Course: PHP lecture 1
Web Development Course: PHP lecture 1
 
C++ Files and Streams
C++ Files and Streams C++ Files and Streams
C++ Files and Streams
 
JDBC – Java Database Connectivity
JDBC – Java Database ConnectivityJDBC – Java Database Connectivity
JDBC – Java Database Connectivity
 
Looping statement
Looping statementLooping statement
Looping statement
 
Oops concept on c#
Oops concept on c#Oops concept on c#
Oops concept on c#
 
Java I/O
Java I/OJava I/O
Java I/O
 
Introduction to php
Introduction to phpIntroduction to php
Introduction to php
 
Type casting
Type castingType casting
Type casting
 
C# String
C# StringC# String
C# String
 
L21 io streams
L21 io streamsL21 io streams
L21 io streams
 
Applets in java
Applets in javaApplets in java
Applets in java
 
Php
PhpPhp
Php
 
Data Types, Variables, and Operators
Data Types, Variables, and OperatorsData Types, Variables, and Operators
Data Types, Variables, and Operators
 
Io streams
Io streamsIo streams
Io streams
 
JDBC: java DataBase connectivity
JDBC: java DataBase connectivityJDBC: java DataBase connectivity
JDBC: java DataBase connectivity
 
Network programming in Java
Network programming in JavaNetwork programming in Java
Network programming in Java
 
File handling in Python
File handling in PythonFile handling in Python
File handling in Python
 
types of loops and what is loop
types of loops and what is looptypes of loops and what is loop
types of loops and what is loop
 
PHP - Introduction to File Handling with PHP
PHP -  Introduction to  File Handling with PHPPHP -  Introduction to  File Handling with PHP
PHP - Introduction to File Handling with PHP
 
OOP java
OOP javaOOP java
OOP java
 

Similar to Files and streams In Java

Similar to Files and streams In Java (20)

Input output files in java
Input output files in javaInput output files in java
Input output files in java
 
5java Io
5java Io5java Io
5java Io
 
Java I/o streams
Java I/o streamsJava I/o streams
Java I/o streams
 
26 io -ii file handling
26  io -ii  file handling26  io -ii  file handling
26 io -ii file handling
 
Input/Output Exploring java.io
Input/Output Exploring java.ioInput/Output Exploring java.io
Input/Output Exploring java.io
 
File Handling in Java.pdf
File Handling in Java.pdfFile Handling in Java.pdf
File Handling in Java.pdf
 
Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01
 
Files in c++
Files in c++Files in c++
Files in c++
 
CORE JAVA-1
CORE JAVA-1CORE JAVA-1
CORE JAVA-1
 
Io stream
Io streamIo stream
Io stream
 
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
 
Jedi Slides Intro2 Chapter12 Advanced Io Streams
Jedi Slides Intro2 Chapter12 Advanced Io StreamsJedi Slides Intro2 Chapter12 Advanced Io Streams
Jedi Slides Intro2 Chapter12 Advanced Io Streams
 
IOStream.pptx
IOStream.pptxIOStream.pptx
IOStream.pptx
 
Java program file I/O
Java program file I/OJava program file I/O
Java program file I/O
 
Understanding java streams
Understanding java streamsUnderstanding java streams
Understanding java streams
 
Java Day-6
Java Day-6Java Day-6
Java Day-6
 
Files in C++.pdf is the notes of cpp for reference
Files in C++.pdf is the notes of cpp for referenceFiles in C++.pdf is the notes of cpp for reference
Files in C++.pdf is the notes of cpp for reference
 
Java I/O
Java I/OJava I/O
Java I/O
 
COM1407: File Processing
COM1407: File Processing COM1407: File Processing
COM1407: File Processing
 
JAVA
JAVAJAVA
JAVA
 

More from Rajan Shah

Xml dtd- Document Type Definition- Web Technology
Xml dtd- Document Type Definition- Web TechnologyXml dtd- Document Type Definition- Web Technology
Xml dtd- Document Type Definition- Web TechnologyRajan Shah
 
Timing and control circuit
Timing and control circuitTiming and control circuit
Timing and control circuitRajan Shah
 
Rethrowing exception- JAVA
Rethrowing exception- JAVARethrowing exception- JAVA
Rethrowing exception- JAVARajan Shah
 
Np Completeness
Np CompletenessNp Completeness
Np CompletenessRajan Shah
 
Deadlock- Operating System
Deadlock- Operating SystemDeadlock- Operating System
Deadlock- Operating SystemRajan Shah
 
Data Reduction
Data ReductionData Reduction
Data ReductionRajan Shah
 
Cyclic Redundancy Check
Cyclic Redundancy CheckCyclic Redundancy Check
Cyclic Redundancy CheckRajan Shah
 
Client server s/w Engineering
Client server s/w EngineeringClient server s/w Engineering
Client server s/w EngineeringRajan Shah
 
Bluetooth protocol
Bluetooth protocolBluetooth protocol
Bluetooth protocolRajan Shah
 

More from Rajan Shah (10)

Xml dtd- Document Type Definition- Web Technology
Xml dtd- Document Type Definition- Web TechnologyXml dtd- Document Type Definition- Web Technology
Xml dtd- Document Type Definition- Web Technology
 
Timing and control circuit
Timing and control circuitTiming and control circuit
Timing and control circuit
 
Rethrowing exception- JAVA
Rethrowing exception- JAVARethrowing exception- JAVA
Rethrowing exception- JAVA
 
Np Completeness
Np CompletenessNp Completeness
Np Completeness
 
Lex Tool
Lex ToolLex Tool
Lex Tool
 
Deadlock- Operating System
Deadlock- Operating SystemDeadlock- Operating System
Deadlock- Operating System
 
Data Reduction
Data ReductionData Reduction
Data Reduction
 
Cyclic Redundancy Check
Cyclic Redundancy CheckCyclic Redundancy Check
Cyclic Redundancy Check
 
Client server s/w Engineering
Client server s/w EngineeringClient server s/w Engineering
Client server s/w Engineering
 
Bluetooth protocol
Bluetooth protocolBluetooth protocol
Bluetooth protocol
 

Recently uploaded

(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineeringmalavadedarshan25
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZTE
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxvipinkmenon1
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxPoojaBan
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and usesDevarapalliHaritha
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 

Recently uploaded (20)

(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineering
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptx
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptx
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and uses
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 

Files and streams In Java

  • 1. Sardar VallabhBhai Patel Institute of Technology  Name- RAJAN SHAH  Topic: Files In JAVA
  • 2. I/O Stream  Input Stream: It is a stream which may be connected with different i/p devices like keyboard, file or network socket. It is used to generate data.  Output Stream: It is used to produce data and may be connected with different output devices like monitor, file, network socket.
  • 3. Files and Streams  File processing with classes in package java.io  FileInputStream for byte-by-byte input from a file  FileOutputStream for byte-by-byte output to a file  FileReader for char-by-char input from a file  FileWriter for char-by-char output to a file
  • 4. Difference byte Stream char Stream 1. It deals with byte. 2. Used to manipulate data in binary format. 3. Mainly uses audio, video, img, txt files. 4. InputStream and OutputStream are top two abstract classes of byte Stream. 1. It deals with char or Strings. 2. Used to manipulate data in Unicode. 3. Can be used only with txt files. 4. Reader & Writer are top two abstract classes of char Stream.
  • 6. Constructors, Methods Of FileInputStream  CONSTRUCTORS:  FileInputStream(String fname);  FileInputStream(File F);  FileInputStream(String fold, String fname);  METHODS:  int read(); // n= fis.read(); // n is the byte read.
  • 7. Constructors, Methods Of FileOutputStream  CONSTRUCTORS:  FileOutputStream(String fname);  FileOutputStream(File F);  FileOutputStream(String fname, boolean app);  METHODS:  void write(int n); //write(n); //n is the byte to be written  void write(byte []b); // writes the array contents to file.
  • 8. Example: FileInputStream  import java.io.*; public class ReadStringFromFile { public static void main(String[] args) { File file = new File("C://FileIO//ReadString.txt"); int ch; StringBuffer strContent = new StringBuffer(""); FileInputStream fin = null; try { fin = new FileInputStream(file);
  • 9. Continue... while( (ch = fin.read()) != -1) strContent.append((char)ch); fin.close(); } catch(Exception e){ System.out.println(e); } System.out.println("File contents :"); System.out.println(strContent); } }
  • 10. Example: FileOutputStream  import java.io.FileInputStream;  import java.io.FileOutputStream;  import java.io.IOException; public class FileOutputStreamDemo { public static void main(String[] args) throws IOException { byte[] b = {65,66,67,68,69}; int i=0; char c; try{ FileOutputStream fos=new FileOutputStream("C://test.txt"); fos.write(b);
  • 11. Continue... fos.flush(); FileInputStream fis = new FileInputStream("C://test.txt"); while((i=fis.read())!=-1) { c=(char)i; System.out.print(c); } }catch(Exception ex) {ex.printStackTrace(); } finally{ if(fos!=null) fos.close(); if(fis!=null) fis.close(); } } }
  • 13. Constructors, Methods Of FileReader  CONSTRUCTORS:  FileReader(String fname);  FileReader(File F);  FileReader(String fold, String fname);  METHODS:  int read(); // n= fis.read(); // n is the char read.  int read(char [] c, int offset, int len); // reads character into an array and returns the number of chars read.
  • 14. Constructors, Methods Of FileWriter  CONSTRUCTORS:  FileWriter(String fname);  FileWriter(File F);  FileWriter(String fold, String fname);  METHODS:  void write(String n); // write(n); //n is the String to be written to file.  public void write(char [] c, int offset, int len); // Writes a portion of an array of characters starting from offset and with a length of len.  public void write(String s, int offset, int len);
  • 15. Example: FileReader  import java.io.*; public class FileRead { public static void main(String args[]) throws IOException { File file = new File("Hello1.txt"); file.createNewFile(); //returns true if file is created FileWriter fw = new FileWriter(file); fw.write("Thisn isn ann examplen"); fw.flush(); fw.close();
  • 16. Continue... FileReader fr = new FileReader(file); char [] a = new char[50]; fr.read(a); //reads content to array for(char c : a) System.out.print(c); fr.close(); } }
  • 17. Example: FileReader using BufferedReader  import java.io.*; public class Reader { public static void main(String[]args) throws IOException{ FileReader fr = new FileReader("C:/test.txt"); BufferedReader br = new BufferedReader(in); while (br.readLine() != null) { System.out.println(br.readLine()); } fr.close(); }
  • 18. Example: FileWriter • import java.io.*; class Simple{ public static void main(String args[]){ try{ FileWriter fw=new FileWriter("abc.txt"); fw.write(“Hello World"); fw.close(); } catch(Exception e) {System.out.println(e);} System.out.println(“Success"); } }
  • 19.  import java.io.BufferedWriter;  import java.io.File;  import java.io.FileWriter;  import java.io.IOException; public class BufferedWriterDemo { public static void main(String[] args) throws IOException { File f = new File("java2learn.txt"); System.out.println(f.exists()); BufferedWriter bw = new BufferedWriter(new FileWriter(f)); char[] ch1 = { 'a', 'b', 'c', 'd' }; bw.write(ch1); } }