SlideShare a Scribd company logo
File IO in Java

       Prof. AshishSingh Bhatia, ast.bhatia@gmail.com,
     ashish@asbspace.in, Web: asbspace.in, M:9879009551


                                 September 18, 2012




Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   1
AGENDA
Understanding Streams
Streams
Files
File class methods
File class methods -Cont
Programs using File class
Program to rename a file.
Program to delete a file.
Types of Streams
Stream Base IO
Overview of IO Classes
OutputStream of IO
InputStream of IO
Writer of IO
Reader of IO
   Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   2
Understanding Streams




   Input Stream




   Output Stream

     Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   3
Streams




      Sequence of bytes.
      Input Stream : Data is received by some device to program.
      Output Stream : Data is sent by the program to device.
      Stream provides a sort of abstraction.
      Recall System.out and System.in
      System.in object of InputStream and System.out and
      System.err is object of PrintStream class.




    Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   4
Streams




      Sequence of bytes.
      Input Stream : Data is received by some device to program.
      Output Stream : Data is sent by the program to device.
      Stream provides a sort of abstraction.
      Recall System.out and System.in
      System.in object of InputStream and System.out and
      System.err is object of PrintStream class.




    Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   5
Streams




      Sequence of bytes.
      Input Stream : Data is received by some device to program.
      Output Stream : Data is sent by the program to device.
      Stream provides a sort of abstraction.
      Recall System.out and System.in
      System.in object of InputStream and System.out and
      System.err is object of PrintStream class.




    Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   6
Streams




      Sequence of bytes.
      Input Stream : Data is received by some device to program.
      Output Stream : Data is sent by the program to device.
      Stream provides a sort of abstraction.
      Recall System.out and System.in
      System.in object of InputStream and System.out and
      System.err is object of PrintStream class.




    Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   7
Streams




      Sequence of bytes.
      Input Stream : Data is received by some device to program.
      Output Stream : Data is sent by the program to device.
      Stream provides a sort of abstraction.
      Recall System.out and System.in
      System.in object of InputStream and System.out and
      System.err is object of PrintStream class.




    Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   8
Streams




      Sequence of bytes.
      Input Stream : Data is received by some device to program.
      Output Stream : Data is sent by the program to device.
      Stream provides a sort of abstraction.
      Recall System.out and System.in
      System.in object of InputStream and System.out and
      System.err is object of PrintStream class.




    Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   9
Files


           File class is provided by java.io package.
           An abstract reprsentation of file and directory pathnames.
           Note File object is not for reading / writing files.
           Used for obtaining information associated with file like
           permission, time and date, path.
           Directory too is treated as file and have additional method
           list() to list the filename in directory.
           public        File(String pathname)
           public        File(String parent, String child)
           public        File(File parent, String child)
           public        File(URI uri



        Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   10
Files


           File class is provided by java.io package.
           An abstract reprsentation of file and directory pathnames.
           Note File object is not for reading / writing files.
           Used for obtaining information associated with file like
           permission, time and date, path.
           Directory too is treated as file and have additional method
           list() to list the filename in directory.
           public        File(String pathname)
           public        File(String parent, String child)
           public        File(File parent, String child)
           public        File(URI uri



        Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   11
Files


           File class is provided by java.io package.
           An abstract reprsentation of file and directory pathnames.
           Note File object is not for reading / writing files.
           Used for obtaining information associated with file like
           permission, time and date, path.
           Directory too is treated as file and have additional method
           list() to list the filename in directory.
           public        File(String pathname)
           public        File(String parent, String child)
           public        File(File parent, String child)
           public        File(URI uri



        Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   12
Files


           File class is provided by java.io package.
           An abstract reprsentation of file and directory pathnames.
           Note File object is not for reading / writing files.
           Used for obtaining information associated with file like
           permission, time and date, path.
           Directory too is treated as file and have additional method
           list() to list the filename in directory.
           public        File(String pathname)
           public        File(String parent, String child)
           public        File(File parent, String child)
           public        File(URI uri



        Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   13
Files


           File class is provided by java.io package.
           An abstract reprsentation of file and directory pathnames.
           Note File object is not for reading / writing files.
           Used for obtaining information associated with file like
           permission, time and date, path.
           Directory too is treated as file and have additional method
           list() to list the filename in directory.
           public        File(String pathname)
           public        File(String parent, String child)
           public        File(File parent, String child)
           public        File(URI uri



        Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   14
Files


           File class is provided by java.io package.
           An abstract reprsentation of file and directory pathnames.
           Note File object is not for reading / writing files.
           Used for obtaining information associated with file like
           permission, time and date, path.
           Directory too is treated as file and have additional method
           list() to list the filename in directory.
           public        File(String pathname)
           public        File(String parent, String child)
           public        File(File parent, String child)
           public        File(URI uri



        Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   15
File class methods
        boolean canRead()
        boolean canWrite()
        boolean cancreateNewFile()
        boolean delete()
        boolean exists()
        String getAbsolutePath()
        String getName()
        String getParent()
        String getPath()
        boolean isAbsolute()
        boolean isDirectory()
        boolean isFile()
        boolean isHidden()
        long lastModified()
        long length()
     Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   16
File class methods -Cont


        String[] list()
        File[] listFiles()
        boolean mkdir()
        boolean renameTo(File d)
        boolean setLastModifiedDate()
        boolean setReadOnly()
        String toString()
        String getFreeSpace()
        String getTotalSpace()
        String getUsableSpace()



     Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   17
Programs using File class




        Program to rename a file.




     Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   18
Programs using File class




        Program to rename a file.
        Program to delete a file.




     Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   19
Programs using File class




        Program to rename a file.
        Program to delete a file.
        Creating a directory.




     Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   20
Programs using File class




        Program to rename a file.
        Program to delete a file.
        Creating a directory.
        Traversing a Directory.




     Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   21
Programs using File class




        Program to rename a file.
        Program to delete a file.
        Creating a directory.
        Traversing a Directory.
        Using FilenameFilter Interface.




     Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   22
Program to rename a file.
   import java.io.*;
   import java.util.Scanner;
   public class Rename
   {
   public static void main(String args[]) {
     File f1, f2;
     Scanner s = new Scanner(System.in);
     System.out.println("Enter the file name : ");
     String name old = s.nextLine();
     System.out.println("Enter the new name : ");
     String name new = s.nextLine();
     f1 = new File(name old);
     f2 = new File(name new);
     if(f1.renameTo(f2))
       System.out.println("File renamed");
     else
       System.out.println("Error Occured");
     Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   23
Program to delete a file.
   import java.io.*;
   import java.util.Scanner;
   public class Rename
   {
   public static void main(String args[]) {
     File f1;
     Scanner s = new Scanner(System.in);
     System.out.println("Enter the file name :                                              ");
     String name = s.nextLine();
     f1 = new File(name);
     if(f1.exists())
     {
       if(f1.delete())
         System.out.println("File deleted");
       else
         System.out.println("Error Occured");
     }
     Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   24
Types of Streams




        Byte Stream / Binary Stream
                Only Binary data / bytes.
                Read and Write [8 bytes].
        Character Stream
                Character Data.
                Unicode Character.




     Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   25
Types of Streams




        Byte Stream / Binary Stream
                Only Binary data / bytes.
                Read and Write [8 bytes].
        Character Stream
                Character Data.
                Unicode Character.




     Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   26
Stream Base IO




       Four Abstract Classes.
       Byte Stream / Binary Stream
               InputStream.
               OutputStream.
       Character Stream
               Reader.
               Writer.




    Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   27
Stream Base IO




       Four Abstract Classes.
       Byte Stream / Binary Stream
               InputStream.
               OutputStream.
       Character Stream
               Reader.
               Writer.




    Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   28
Overview of IO Classes




     Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   29
OutputStream of IO




    Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   30
InputStream of IO




     Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   31
Writer of IO




     Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   32
Reader of IO




     Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   33

More Related Content

PPT
TagFS — Tag Semantics for Hierarchical File Systems
PDF
Intake 37 11
PPTX
Data De-duplication (Spring 2014)
PPTX
File system interface
PDF
Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java
PPTX
Chap 5 php files part 1
PDF
Machine Learning Applications
PPTX
Linked Data:Libraries and Beyond
TagFS — Tag Semantics for Hierarchical File Systems
Intake 37 11
Data De-duplication (Spring 2014)
File system interface
Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java
Chap 5 php files part 1
Machine Learning Applications
Linked Data:Libraries and Beyond

What's hot (19)

PDF
10 File System
PPTX
Domain names
PPT
File Management
PDF
Bio ontologies and semantic technologies
PDF
Uplift – Generating RDF datasets from non-RDF data with R2RML
PPTX
SWT Lecture Session 6 - RDFS semantics, inference techniques, sesame rdfs
PPT
PDF
Changing Data: Implementing Primo for the Tri University Group of Libraries (...
PPTX
Democratizing Big Semantic Data management
PDF
Local Persistent data with ActionScript 3 and AIR
PPTX
Chapter 04
PPT
Mba admission in india
DOCX
Switching & Multiplexing
PPTX
Strings in c plus plus
PPT
14 file handling
 
PPTX
Chapter 10.1
PPT
File Management in Operating Systems
PPTX
Python files / directories part15
PDF
Query-Load aware partitioning of RDF data
10 File System
Domain names
File Management
Bio ontologies and semantic technologies
Uplift – Generating RDF datasets from non-RDF data with R2RML
SWT Lecture Session 6 - RDFS semantics, inference techniques, sesame rdfs
Changing Data: Implementing Primo for the Tri University Group of Libraries (...
Democratizing Big Semantic Data management
Local Persistent data with ActionScript 3 and AIR
Chapter 04
Mba admission in india
Switching & Multiplexing
Strings in c plus plus
14 file handling
 
Chapter 10.1
File Management in Operating Systems
Python files / directories part15
Query-Load aware partitioning of RDF data
Ad

Viewers also liked (7)

PPT
JavaScript State of the Union - Jan 2013
PDF
HTTP Basic and Servlet Basic
PDF
Java I/O Part 2
PDF
I/O In Java Part 2
PDF
Dom Basics
PDF
Nested and Enum in Java
PDF
Servlet Event framework
JavaScript State of the Union - Jan 2013
HTTP Basic and Servlet Basic
Java I/O Part 2
I/O In Java Part 2
Dom Basics
Nested and Enum in Java
Servlet Event framework
Ad

Similar to Java I/O Part 1 (20)

PPTX
chapter 2(IO and stream)/chapter 2, IO and stream
PDF
55 new things in Java 7 - Devoxx France
PDF
Basic i/o & file handling in java
PPTX
IO Programming.pptx all informatiyon ppt
PPTX
Java 3 Computer Science.pptx
PPTX
IO and threads Java
DOCX
FileHandling.docx
PDF
Javase7 1641812
PDF
CSE3146-ADV JAVA M2.pdf
PPTX
Input/Output Exploring java.io
PDF
File Handling in Java.pdf
PDF
Advanced programming ch2
PDF
JAVA 4.pdfdhfvksfvhsjfbjhdjhbjshjshjvcjdbh
PPTX
Files that are designed to be read by human beings
PPTX
File Handlingb in java. A brief presentation on file handling
PPT
Java stream
PPTX
File Handling in Java Oop presentation
PDF
Monhocvecaujahetvagiuplaptunhhayhonha.pdf
PDF
My History
PPT
9. Input Output in java
chapter 2(IO and stream)/chapter 2, IO and stream
55 new things in Java 7 - Devoxx France
Basic i/o & file handling in java
IO Programming.pptx all informatiyon ppt
Java 3 Computer Science.pptx
IO and threads Java
FileHandling.docx
Javase7 1641812
CSE3146-ADV JAVA M2.pdf
Input/Output Exploring java.io
File Handling in Java.pdf
Advanced programming ch2
JAVA 4.pdfdhfvksfvhsjfbjhdjhbjshjshjvcjdbh
Files that are designed to be read by human beings
File Handlingb in java. A brief presentation on file handling
Java stream
File Handling in Java Oop presentation
Monhocvecaujahetvagiuplaptunhhayhonha.pdf
My History
9. Input Output in java

Recently uploaded (20)

PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Complications of Minimal Access Surgery at WLH
PPTX
GDM (1) (1).pptx small presentation for students
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Insiders guide to clinical Medicine.pdf
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
Institutional Correction lecture only . . .
PDF
Sports Quiz easy sports quiz sports quiz
PDF
01-Introduction-to-Information-Management.pdf
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
Lesson notes of climatology university.
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
Computing-Curriculum for Schools in Ghana
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
O7-L3 Supply Chain Operations - ICLT Program
Complications of Minimal Access Surgery at WLH
GDM (1) (1).pptx small presentation for students
Microbial disease of the cardiovascular and lymphatic systems
Insiders guide to clinical Medicine.pdf
Renaissance Architecture: A Journey from Faith to Humanism
Institutional Correction lecture only . . .
Sports Quiz easy sports quiz sports quiz
01-Introduction-to-Information-Management.pdf
Pharmacology of Heart Failure /Pharmacotherapy of CHF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Lesson notes of climatology university.
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Computing-Curriculum for Schools in Ghana
Final Presentation General Medicine 03-08-2024.pptx
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Anesthesia in Laparoscopic Surgery in India
FourierSeries-QuestionsWithAnswers(Part-A).pdf

Java I/O Part 1

  • 1. File IO in Java Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 September 18, 2012 Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 1
  • 2. AGENDA Understanding Streams Streams Files File class methods File class methods -Cont Programs using File class Program to rename a file. Program to delete a file. Types of Streams Stream Base IO Overview of IO Classes OutputStream of IO InputStream of IO Writer of IO Reader of IO Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 2
  • 3. Understanding Streams Input Stream Output Stream Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 3
  • 4. Streams Sequence of bytes. Input Stream : Data is received by some device to program. Output Stream : Data is sent by the program to device. Stream provides a sort of abstraction. Recall System.out and System.in System.in object of InputStream and System.out and System.err is object of PrintStream class. Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 4
  • 5. Streams Sequence of bytes. Input Stream : Data is received by some device to program. Output Stream : Data is sent by the program to device. Stream provides a sort of abstraction. Recall System.out and System.in System.in object of InputStream and System.out and System.err is object of PrintStream class. Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 5
  • 6. Streams Sequence of bytes. Input Stream : Data is received by some device to program. Output Stream : Data is sent by the program to device. Stream provides a sort of abstraction. Recall System.out and System.in System.in object of InputStream and System.out and System.err is object of PrintStream class. Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 6
  • 7. Streams Sequence of bytes. Input Stream : Data is received by some device to program. Output Stream : Data is sent by the program to device. Stream provides a sort of abstraction. Recall System.out and System.in System.in object of InputStream and System.out and System.err is object of PrintStream class. Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 7
  • 8. Streams Sequence of bytes. Input Stream : Data is received by some device to program. Output Stream : Data is sent by the program to device. Stream provides a sort of abstraction. Recall System.out and System.in System.in object of InputStream and System.out and System.err is object of PrintStream class. Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 8
  • 9. Streams Sequence of bytes. Input Stream : Data is received by some device to program. Output Stream : Data is sent by the program to device. Stream provides a sort of abstraction. Recall System.out and System.in System.in object of InputStream and System.out and System.err is object of PrintStream class. Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 9
  • 10. Files File class is provided by java.io package. An abstract reprsentation of file and directory pathnames. Note File object is not for reading / writing files. Used for obtaining information associated with file like permission, time and date, path. Directory too is treated as file and have additional method list() to list the filename in directory. public File(String pathname) public File(String parent, String child) public File(File parent, String child) public File(URI uri Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 10
  • 11. Files File class is provided by java.io package. An abstract reprsentation of file and directory pathnames. Note File object is not for reading / writing files. Used for obtaining information associated with file like permission, time and date, path. Directory too is treated as file and have additional method list() to list the filename in directory. public File(String pathname) public File(String parent, String child) public File(File parent, String child) public File(URI uri Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 11
  • 12. Files File class is provided by java.io package. An abstract reprsentation of file and directory pathnames. Note File object is not for reading / writing files. Used for obtaining information associated with file like permission, time and date, path. Directory too is treated as file and have additional method list() to list the filename in directory. public File(String pathname) public File(String parent, String child) public File(File parent, String child) public File(URI uri Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 12
  • 13. Files File class is provided by java.io package. An abstract reprsentation of file and directory pathnames. Note File object is not for reading / writing files. Used for obtaining information associated with file like permission, time and date, path. Directory too is treated as file and have additional method list() to list the filename in directory. public File(String pathname) public File(String parent, String child) public File(File parent, String child) public File(URI uri Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 13
  • 14. Files File class is provided by java.io package. An abstract reprsentation of file and directory pathnames. Note File object is not for reading / writing files. Used for obtaining information associated with file like permission, time and date, path. Directory too is treated as file and have additional method list() to list the filename in directory. public File(String pathname) public File(String parent, String child) public File(File parent, String child) public File(URI uri Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 14
  • 15. Files File class is provided by java.io package. An abstract reprsentation of file and directory pathnames. Note File object is not for reading / writing files. Used for obtaining information associated with file like permission, time and date, path. Directory too is treated as file and have additional method list() to list the filename in directory. public File(String pathname) public File(String parent, String child) public File(File parent, String child) public File(URI uri Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 15
  • 16. File class methods boolean canRead() boolean canWrite() boolean cancreateNewFile() boolean delete() boolean exists() String getAbsolutePath() String getName() String getParent() String getPath() boolean isAbsolute() boolean isDirectory() boolean isFile() boolean isHidden() long lastModified() long length() Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 16
  • 17. File class methods -Cont String[] list() File[] listFiles() boolean mkdir() boolean renameTo(File d) boolean setLastModifiedDate() boolean setReadOnly() String toString() String getFreeSpace() String getTotalSpace() String getUsableSpace() Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 17
  • 18. Programs using File class Program to rename a file. Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 18
  • 19. Programs using File class Program to rename a file. Program to delete a file. Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 19
  • 20. Programs using File class Program to rename a file. Program to delete a file. Creating a directory. Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 20
  • 21. Programs using File class Program to rename a file. Program to delete a file. Creating a directory. Traversing a Directory. Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 21
  • 22. Programs using File class Program to rename a file. Program to delete a file. Creating a directory. Traversing a Directory. Using FilenameFilter Interface. Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 22
  • 23. Program to rename a file. import java.io.*; import java.util.Scanner; public class Rename { public static void main(String args[]) { File f1, f2; Scanner s = new Scanner(System.in); System.out.println("Enter the file name : "); String name old = s.nextLine(); System.out.println("Enter the new name : "); String name new = s.nextLine(); f1 = new File(name old); f2 = new File(name new); if(f1.renameTo(f2)) System.out.println("File renamed"); else System.out.println("Error Occured"); Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 23
  • 24. Program to delete a file. import java.io.*; import java.util.Scanner; public class Rename { public static void main(String args[]) { File f1; Scanner s = new Scanner(System.in); System.out.println("Enter the file name : "); String name = s.nextLine(); f1 = new File(name); if(f1.exists()) { if(f1.delete()) System.out.println("File deleted"); else System.out.println("Error Occured"); } Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 24
  • 25. Types of Streams Byte Stream / Binary Stream Only Binary data / bytes. Read and Write [8 bytes]. Character Stream Character Data. Unicode Character. Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 25
  • 26. Types of Streams Byte Stream / Binary Stream Only Binary data / bytes. Read and Write [8 bytes]. Character Stream Character Data. Unicode Character. Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 26
  • 27. Stream Base IO Four Abstract Classes. Byte Stream / Binary Stream InputStream. OutputStream. Character Stream Reader. Writer. Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 27
  • 28. Stream Base IO Four Abstract Classes. Byte Stream / Binary Stream InputStream. OutputStream. Character Stream Reader. Writer. Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 28
  • 29. Overview of IO Classes Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 29
  • 30. OutputStream of IO Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 30
  • 31. InputStream of IO Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 31
  • 32. Writer of IO Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 32
  • 33. Reader of IO Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 33