SlideShare a Scribd company logo
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

Learn Java with Dr. Rifat Shahriyar
Learn Java with Dr. Rifat ShahriyarLearn Java with Dr. Rifat Shahriyar
Learn Java with Dr. Rifat Shahriyar
Abir Mohammad
 
Data file handling in c++
Data file handling in c++Data file handling in c++
Data file handling in c++
Vineeta Garg
 
Python programming : Abstract classes interfaces
Python programming : Abstract classes interfacesPython programming : Abstract classes interfaces
Python programming : Abstract classes interfaces
Emertxe Information Technologies Pvt Ltd
 
Abstract class
Abstract classAbstract class
Abstract class
Tony Nguyen
 
Java Notes
Java NotesJava Notes
Java Notes
Abhishek Khune
 
Java.util
Java.utilJava.util
Java.util
Ramakrishna kapa
 
Introduction to package in java
Introduction to package in javaIntroduction to package in java
Introduction to package in java
Prognoz Technologies Pvt. Ltd.
 
Python-02| Input, Output & Import
Python-02| Input, Output & ImportPython-02| Input, Output & Import
Python-02| Input, Output & Import
Mohd Sajjad
 
Introduction to Java Strings, By Kavita Ganesan
Introduction to Java Strings, By Kavita GanesanIntroduction to Java Strings, By Kavita Ganesan
Introduction to Java Strings, By Kavita Ganesan
Kavita Ganesan
 
Java string handling
Java string handlingJava string handling
Java string handling
Salman Khan
 
Java String
Java StringJava String
Java String
Java2Blog
 
Visibility control in java
Visibility control in javaVisibility control in java
Visibility control in javaTech_MX
 
Java threading
Java threadingJava threading
Java threading
Chinh Ngo Nguyen
 
Enums in c
Enums in cEnums in c
Stream classes in C++
Stream classes in C++Stream classes in C++
Stream classes in C++
Shyam Gupta
 

What's hot (20)

Packages in java
Packages in javaPackages in java
Packages in java
 
Learn Java with Dr. Rifat Shahriyar
Learn Java with Dr. Rifat ShahriyarLearn Java with Dr. Rifat Shahriyar
Learn Java with Dr. Rifat Shahriyar
 
Data file handling in c++
Data file handling in c++Data file handling in c++
Data file handling in c++
 
Python programming : Abstract classes interfaces
Python programming : Abstract classes interfacesPython programming : Abstract classes interfaces
Python programming : Abstract classes interfaces
 
Abstract class
Abstract classAbstract class
Abstract class
 
Java Notes
Java NotesJava Notes
Java Notes
 
Java.util
Java.utilJava.util
Java.util
 
Java keywords
Java keywordsJava keywords
Java keywords
 
Introduction to package in java
Introduction to package in javaIntroduction to package in java
Introduction to package in java
 
Python-02| Input, Output & Import
Python-02| Input, Output & ImportPython-02| Input, Output & Import
Python-02| Input, Output & Import
 
Java I/O
Java I/OJava I/O
Java I/O
 
Introduction to Java Strings, By Kavita Ganesan
Introduction to Java Strings, By Kavita GanesanIntroduction to Java Strings, By Kavita Ganesan
Introduction to Java Strings, By Kavita Ganesan
 
Java string handling
Java string handlingJava string handling
Java string handling
 
Input output streams
Input output streamsInput output streams
Input output streams
 
Java String
Java StringJava String
Java String
 
Visibility control in java
Visibility control in javaVisibility control in java
Visibility control in java
 
Strings in Java
Strings in JavaStrings in Java
Strings in Java
 
Java threading
Java threadingJava threading
Java threading
 
Enums in c
Enums in cEnums in c
Enums in c
 
Stream classes in C++
Stream classes in C++Stream classes in C++
Stream classes in C++
 

Similar to Files and streams In Java

Input output files in java
Input output files in javaInput output files in java
Input output files in java
Kavitha713564
 
Java I/o streams
Java I/o streamsJava I/o streams
Java I/o streams
Hamid Ghorbani
 
Input/Output Exploring java.io
Input/Output Exploring java.ioInput/Output Exploring java.io
Input/Output Exploring java.io
NilaNila16
 
File Handling in Java.pdf
File Handling in Java.pdfFile Handling in Java.pdf
File Handling in Java.pdf
SudhanshiBakre1
 
Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01
Rex Joe
 
Files in c++
Files in c++Files in c++
Files in c++
Selvin Josy Bai Somu
 
new pdfrdfzdfzdzzzzzzzzzzzzzzzzzzzzzzzzzzgggggggggggggggggggggggggggggggggggg...
new pdfrdfzdfzdzzzzzzzzzzzzzzzzzzzzzzzzzzgggggggggggggggggggggggggggggggggggg...new pdfrdfzdfzdzzzzzzzzzzzzzzzzzzzzzzzzzzgggggggggggggggggggggggggggggggggggg...
new pdfrdfzdfzdzzzzzzzzzzzzzzzzzzzzzzzzzzgggggggggggggggggggggggggggggggggggg...
AzanMehdi
 
Io stream
Io streamIo stream
Io stream
Parthipan Parthi
 
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 & Outputphanleson
 
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 StreamsDon Bosco BSIT
 
IOStream.pptx
IOStream.pptxIOStream.pptx
IOStream.pptx
HindAlmisbahi
 
Java program file I/O
Java program file I/OJava program file I/O
Java program file I/O
Nem Sothea
 
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
anuvayalil5525
 
Java I/O
Java I/OJava I/O
Java I/O
Jayant Dalvi
 
COM1407: File Processing
COM1407: File Processing COM1407: File Processing
COM1407: File Processing
Hemantha Kulathilake
 

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
 
new pdfrdfzdfzdzzzzzzzzzzzzzzzzzzzzzzzzzzgggggggggggggggggggggggggggggggggggg...
new pdfrdfzdfzdzzzzzzzzzzzzzzzzzzzzzzzzzzgggggggggggggggggggggggggggggggggggg...new pdfrdfzdfzdzzzzzzzzzzzzzzzzzzzzzzzzzzgggggggggggggggggggggggggggggggggggg...
new pdfrdfzdfzdzzzzzzzzzzzzzzzzzzzzzzzzzzgggggggggggggggggggggggggggggggggggg...
 
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
 
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 Technology
Rajan Shah
 
Timing and control circuit
Timing and control circuitTiming and control circuit
Timing and control circuit
Rajan Shah
 
Rethrowing exception- JAVA
Rethrowing exception- JAVARethrowing exception- JAVA
Rethrowing exception- JAVA
Rajan Shah
 
Np Completeness
Np CompletenessNp Completeness
Np Completeness
Rajan Shah
 
Lex Tool
Lex ToolLex Tool
Lex Tool
Rajan Shah
 
Deadlock- Operating System
Deadlock- Operating SystemDeadlock- Operating System
Deadlock- Operating System
Rajan Shah
 
Data Reduction
Data ReductionData Reduction
Data Reduction
Rajan Shah
 
Cyclic Redundancy Check
Cyclic Redundancy CheckCyclic Redundancy Check
Cyclic Redundancy Check
Rajan Shah
 
Client server s/w Engineering
Client server s/w EngineeringClient server s/w Engineering
Client server s/w Engineering
Rajan Shah
 
Bluetooth protocol
Bluetooth protocolBluetooth protocol
Bluetooth protocol
Rajan 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

在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
obonagu
 
Vaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdfVaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdf
Kamal Acharya
 
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang,  ICLR 2024, MLILAB, KAIST AI.pdfJ.Yang,  ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
MLILAB
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
gdsczhcet
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation & Control
 
The role of big data in decision making.
The role of big data in decision making.The role of big data in decision making.
The role of big data in decision making.
ankuprajapati0525
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
JoytuBarua2
 
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
MLILAB
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
SamSarthak3
 
Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.
PrashantGoswami42
 
Event Management System Vb Net Project Report.pdf
Event Management System Vb Net  Project Report.pdfEvent Management System Vb Net  Project Report.pdf
Event Management System Vb Net Project Report.pdf
Kamal Acharya
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
Osamah Alsalih
 
Forklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella PartsForklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella Parts
Intella Parts
 
ethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.pptethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.ppt
Jayaprasanna4
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
Robbie Edward Sayers
 
Democratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek AryaDemocratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek Arya
abh.arya
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
Kamal Acharya
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
VENKATESHvenky89705
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
fxintegritypublishin
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
TeeVichai
 

Recently uploaded (20)

在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
 
Vaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdfVaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdf
 
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang,  ICLR 2024, MLILAB, KAIST AI.pdfJ.Yang,  ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
 
The role of big data in decision making.
The role of big data in decision making.The role of big data in decision making.
The role of big data in decision making.
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
 
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
 
Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.
 
Event Management System Vb Net Project Report.pdf
Event Management System Vb Net  Project Report.pdfEvent Management System Vb Net  Project Report.pdf
Event Management System Vb Net Project Report.pdf
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
 
Forklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella PartsForklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella Parts
 
ethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.pptethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.ppt
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
 
Democratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek AryaDemocratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek Arya
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
 

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); } }