SlideShare a Scribd company logo
1 of 24
File Handling
System class
□ System class has three attributes
  □ in (System.in)
    □ Object of class InputStream
  □ out (System.out)
    □ Object of OutputStream
  □ err (Error Stream)
    □ Object of ErrorStream
Introduction

□ We need to have some method of
  storing data permanently – even when
  the computer is switched off and
  program has been terminated
□ We need to store multiple records,
  each consist of multiple fields in a file
File Handling
□ Java provide File streams – Input and
  Output streams that handle
  communication between main
  memory and named file on a disk
□ We can write data to a file in the form
  of Strings, lines of text or basic types
  such as integers or characters.
□ Java even allows us to store and
  retrieve whole objects
A file on a disk or tape can be a text
         file or a binary file.
The standard input and output behave
           like text files.
The standard error behaves like
          a text file.
StreamS
File streams are created, connected to
 files, and disconnected from files by
            the programmer.
Input and Output
□ Input
   □ Allowing information to come in from outside
      world
   □ Transfer of data from some external device to
      main memory
□ Output
   □ Display or storage of processed information
   □ Transfer of data from main memory to an
      external device
□ In order to have Input and Output , a channel of
  communication is required referred to as a stream
□ We have standard input and output stream which
  is keyboard and screen
□ We have standard error stream which is also set to
  screen
One of the most frequently used
task in programming is writing to
 and reading from a file. To do
   this in Java there are more
            possibilities.
Encoding
□ Java supports three different ways of
  encoding data
  □ Text
    □ Data on disk is stored as characters in the form used by
      external system
    □ ASCII normally but as java uses UNICODE so internally
      some transformation do happen
    □ Readable by text editor
  □ Binary
    □ Data is stored in same format as the internal
      representation of the data used by the program to
      store data so number 107 will be stored as 1101011.
    □ Cannot be properly read by text editor
  □ Object
    □ Whole object can be written
Reading and writing to text files
     Writing to a File           Reading from a File
FileWriter name = new        Filereader name = new
   FileWriter(“Filename”);      FileReader(“Filename”);


PrintWriter printname = new BufferedReader buffername
   PrintWriter(name);         = new
                              BufferedReader(name);
printname.println(data );
                             String str =
                                 buffername.readLine();
Printname.close();
                             If(str == null) => End of File

                             Buffername.close();
Filename handling
□ To write anything to a file first of all we
  need a file name we want to use.
□ The file name is a simple string like:
  □ String fileName = "test.txt";
□ If you want to write in a file which is
  located elsewhere you need to define
  the
  complete file name and path in your
  fileName variable:
  □ String fileName = "c:filedemotest.txt";
Open a file
□ To open a file for writing use the FileWriter class
  and create an instance from it.
  The file name is passed in the constructor like
  this:
   □ FileWriter writer = new FileWriter(fileName);
□ This code opens the file in overwrite mode. If
  you want to append to the file then
  you need to use an other constructor like this:   
   □ FileWriter writer = new
     FileWriter(fileName,true);
□ Besides this the constructor can throw an
  IOException so we put all of the code inside
  a try-catch block.
Write to a File
□ At this point we have a writer object and we can
  send real content to the file.
□ Do this using the write() method, which has more
  variant but the most commonly used requires a
  string as input parameter.
□ Calling the write() method doesn't mean that it
  immediately writes the data into the file.
   □ The output is maybe cached so if you want to
      send your data immediately to the file you need
      to call the flush() method.
□ As last step you should close the file with the close()
  method and you are done
Reading from a File
□ reading from a file is very similar to writing.
□ We only need to use *Reader objects
  instead of *Writer objects.
□ It means that you can use FileReader or
  BufferedReader.
  □ A simple FileReader can handle only a single
    character or a character array it is more
    convenient to use the BufferedReader which can
    read a complete line from a file as a string.
  □ So using a BufferedReader we can read a text
    file line by line with the readln() method
return fileContent.toString();
Reading and Writing to Binary
              Files
□ FileOutputStream and
  DataOutputStream
□ FileInputStream and DataInputStream
□ If will try to access something after end
  of file, it will throw and EOFException
Reading and writing to Binary files
     Writing to a File           Reading from a File
FileOutputStream out = new
   FileOutputStream("c:test.
   txt",true);

DataOutputStream dataOut =
  new
  DataOutputStream(out);

String str ="Saira Anwar";

dataOut.writeBytes(str);
dataOut.close();

More Related Content

What's hot (20)

Files in c++
Files in c++Files in c++
Files in c++
 
File Handling in C++
File Handling in C++File Handling in C++
File Handling in C++
 
Filehadnling
FilehadnlingFilehadnling
Filehadnling
 
Unit5 C
Unit5 C Unit5 C
Unit5 C
 
Filehandlinging cp2
Filehandlinging cp2Filehandlinging cp2
Filehandlinging cp2
 
LSMW - Multiple Line of Material Master Long Text
LSMW - Multiple Line of Material Master Long TextLSMW - Multiple Line of Material Master Long Text
LSMW - Multiple Line of Material Master Long Text
 
Cpp file-handling
Cpp file-handlingCpp file-handling
Cpp file-handling
 
File in cpp 2016
File in cpp 2016 File in cpp 2016
File in cpp 2016
 
Files in c++
Files in c++Files in c++
Files in c++
 
Reading and Writing Files
Reading and Writing FilesReading and Writing Files
Reading and Writing Files
 
Filepointers1 1215104829397318-9
Filepointers1 1215104829397318-9Filepointers1 1215104829397318-9
Filepointers1 1215104829397318-9
 
Python files / directories part16
Python files / directories  part16Python files / directories  part16
Python files / directories part16
 
File handling in_c
File handling in_cFile handling in_c
File handling in_c
 
File handling in vb.net
File handling in vb.netFile handling in vb.net
File handling in vb.net
 
C++ files and streams
C++ files and streamsC++ files and streams
C++ files and streams
 
C files
C filesC files
C files
 
Files and file objects (in Python)
Files and file objects (in Python)Files and file objects (in Python)
Files and file objects (in Python)
 
File handling in Python
File handling in PythonFile handling in Python
File handling in Python
 
File handling
File handlingFile handling
File handling
 
File handling and Dictionaries in python
File handling and Dictionaries in pythonFile handling and Dictionaries in python
File handling and Dictionaries in python
 

Viewers also liked

Case studys of RockSound and Kerrang!
Case studys of RockSound and Kerrang!Case studys of RockSound and Kerrang!
Case studys of RockSound and Kerrang!Chloe Main
 
Magazine Print scenes
Magazine Print scenesMagazine Print scenes
Magazine Print scenesChloe Main
 
Magazine Print Scenes
Magazine Print ScenesMagazine Print Scenes
Magazine Print ScenesChloe Main
 
Documentary Research
Documentary ResearchDocumentary Research
Documentary ResearchChloe Main
 
Conclusion and Result
Conclusion and ResultConclusion and Result
Conclusion and ResultChloe Main
 
Formal proposal
Formal proposalFormal proposal
Formal proposalChloe Main
 
Images Presentation by John Mueller
Images Presentation by John MuellerImages Presentation by John Mueller
Images Presentation by John MuellerKarol Dziedzic
 
Seebloggers - Google Analytics
Seebloggers - Google AnalyticsSeebloggers - Google Analytics
Seebloggers - Google AnalyticsKarol Dziedzic
 

Viewers also liked (17)

Case studys of RockSound and Kerrang!
Case studys of RockSound and Kerrang!Case studys of RockSound and Kerrang!
Case studys of RockSound and Kerrang!
 
Magazine Print scenes
Magazine Print scenesMagazine Print scenes
Magazine Print scenes
 
Comp102 lec 3
Comp102   lec 3Comp102   lec 3
Comp102 lec 3
 
Magazine Print Scenes
Magazine Print ScenesMagazine Print Scenes
Magazine Print Scenes
 
Documentary Research
Documentary ResearchDocumentary Research
Documentary Research
 
Conclusion and Result
Conclusion and ResultConclusion and Result
Conclusion and Result
 
Comp102 lec 1
Comp102   lec 1Comp102   lec 1
Comp102 lec 1
 
Comp102 lec 5.0
Comp102   lec 5.0Comp102   lec 5.0
Comp102 lec 5.0
 
Comp102 lec 10
Comp102   lec 10Comp102   lec 10
Comp102 lec 10
 
Comp102 lec 9
Comp102   lec 9Comp102   lec 9
Comp102 lec 9
 
Comp102 lec 5.1
Comp102   lec 5.1Comp102   lec 5.1
Comp102 lec 5.1
 
Comp102 lec 8
Comp102   lec 8Comp102   lec 8
Comp102 lec 8
 
Comp102 lec 4
Comp102   lec 4Comp102   lec 4
Comp102 lec 4
 
Formal proposal
Formal proposalFormal proposal
Formal proposal
 
Images Presentation by John Mueller
Images Presentation by John MuellerImages Presentation by John Mueller
Images Presentation by John Mueller
 
Comp102 lec 7
Comp102   lec 7Comp102   lec 7
Comp102 lec 7
 
Seebloggers - Google Analytics
Seebloggers - Google AnalyticsSeebloggers - Google Analytics
Seebloggers - Google Analytics
 

Similar to Comp102 lec 11 (20)

Java file
Java fileJava file
Java file
 
File Handling
File HandlingFile Handling
File Handling
 
File Handling
File HandlingFile Handling
File Handling
 
File handling & regular expressions in python programming
File handling & regular expressions in python programmingFile handling & regular expressions in python programming
File handling & regular expressions in python programming
 
file handling.pdf
file handling.pdffile handling.pdf
file handling.pdf
 
File_Handling in C.ppt
File_Handling in C.pptFile_Handling in C.ppt
File_Handling in C.ppt
 
File_Handling in C.ppt
File_Handling in C.pptFile_Handling in C.ppt
File_Handling in C.ppt
 
Chapter 11
Chapter 11Chapter 11
Chapter 11
 
COM1407: File Processing
COM1407: File Processing COM1407: File Processing
COM1407: File Processing
 
ASP.NET Session 7
ASP.NET Session 7ASP.NET Session 7
ASP.NET Session 7
 
Data Structure Using C - FILES
Data Structure Using C - FILESData Structure Using C - FILES
Data Structure Using C - FILES
 
file_c.pdf
file_c.pdffile_c.pdf
file_c.pdf
 
File handling in c
File handling in cFile handling in c
File handling in c
 
Input File dalam C++
Input File dalam C++Input File dalam C++
Input File dalam C++
 
Presentation of file handling in C language
Presentation of file handling in C languagePresentation of file handling in C language
Presentation of file handling in C language
 
Chapter4.pptx
Chapter4.pptxChapter4.pptx
Chapter4.pptx
 
IOStream.pptx
IOStream.pptxIOStream.pptx
IOStream.pptx
 
Lecture 11.pdf
Lecture 11.pdfLecture 11.pdf
Lecture 11.pdf
 
Module2-Files.pdf
Module2-Files.pdfModule2-Files.pdf
Module2-Files.pdf
 
Data file handling in c++
Data file handling in c++Data file handling in c++
Data file handling in c++
 

Comp102 lec 11

  • 2. System class □ System class has three attributes □ in (System.in) □ Object of class InputStream □ out (System.out) □ Object of OutputStream □ err (Error Stream) □ Object of ErrorStream
  • 3. Introduction □ We need to have some method of storing data permanently – even when the computer is switched off and program has been terminated □ We need to store multiple records, each consist of multiple fields in a file
  • 4. File Handling □ Java provide File streams – Input and Output streams that handle communication between main memory and named file on a disk □ We can write data to a file in the form of Strings, lines of text or basic types such as integers or characters. □ Java even allows us to store and retrieve whole objects
  • 5. A file on a disk or tape can be a text file or a binary file.
  • 6. The standard input and output behave like text files.
  • 7. The standard error behaves like a text file.
  • 9.
  • 10.
  • 11. File streams are created, connected to files, and disconnected from files by the programmer.
  • 12. Input and Output □ Input □ Allowing information to come in from outside world □ Transfer of data from some external device to main memory □ Output □ Display or storage of processed information □ Transfer of data from main memory to an external device □ In order to have Input and Output , a channel of communication is required referred to as a stream □ We have standard input and output stream which is keyboard and screen □ We have standard error stream which is also set to screen
  • 13. One of the most frequently used task in programming is writing to and reading from a file. To do this in Java there are more possibilities.
  • 14. Encoding □ Java supports three different ways of encoding data □ Text □ Data on disk is stored as characters in the form used by external system □ ASCII normally but as java uses UNICODE so internally some transformation do happen □ Readable by text editor □ Binary □ Data is stored in same format as the internal representation of the data used by the program to store data so number 107 will be stored as 1101011. □ Cannot be properly read by text editor □ Object □ Whole object can be written
  • 15. Reading and writing to text files Writing to a File Reading from a File FileWriter name = new Filereader name = new FileWriter(“Filename”); FileReader(“Filename”); PrintWriter printname = new BufferedReader buffername PrintWriter(name); = new BufferedReader(name); printname.println(data ); String str = buffername.readLine(); Printname.close(); If(str == null) => End of File Buffername.close();
  • 16. Filename handling □ To write anything to a file first of all we need a file name we want to use. □ The file name is a simple string like: □ String fileName = "test.txt"; □ If you want to write in a file which is located elsewhere you need to define the complete file name and path in your fileName variable: □ String fileName = "c:filedemotest.txt";
  • 17. Open a file □ To open a file for writing use the FileWriter class and create an instance from it. The file name is passed in the constructor like this: □ FileWriter writer = new FileWriter(fileName); □ This code opens the file in overwrite mode. If you want to append to the file then you need to use an other constructor like this:    □ FileWriter writer = new FileWriter(fileName,true); □ Besides this the constructor can throw an IOException so we put all of the code inside a try-catch block.
  • 18. Write to a File □ At this point we have a writer object and we can send real content to the file. □ Do this using the write() method, which has more variant but the most commonly used requires a string as input parameter. □ Calling the write() method doesn't mean that it immediately writes the data into the file. □ The output is maybe cached so if you want to send your data immediately to the file you need to call the flush() method. □ As last step you should close the file with the close() method and you are done
  • 19.
  • 20.
  • 21. Reading from a File □ reading from a file is very similar to writing. □ We only need to use *Reader objects instead of *Writer objects. □ It means that you can use FileReader or BufferedReader. □ A simple FileReader can handle only a single character or a character array it is more convenient to use the BufferedReader which can read a complete line from a file as a string. □ So using a BufferedReader we can read a text file line by line with the readln() method
  • 23. Reading and Writing to Binary Files □ FileOutputStream and DataOutputStream □ FileInputStream and DataInputStream □ If will try to access something after end of file, it will throw and EOFException
  • 24. Reading and writing to Binary files Writing to a File Reading from a File FileOutputStream out = new FileOutputStream("c:test. txt",true); DataOutputStream dataOut = new DataOutputStream(out); String str ="Saira Anwar"; dataOut.writeBytes(str); dataOut.close();