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
Java Input Output and File Handling
PDF
PDF
Java - File Input Output Concepts
PPT
Java File I/O
PPTX
Java file
PPT
File Input & Output
PPTX
[Java] #7 - Input & Output Stream
PPT
Chapter 12 - File Input and Output
Java Input Output and File Handling
Java - File Input Output Concepts
Java File I/O
Java file
File Input & Output
[Java] #7 - Input & Output Stream
Chapter 12 - File Input and Output

What's hot (20)

PPTX
Handling I/O in Java
PPTX
basics of file handling
PDF
Java IO
PPT
17 files and streams
PDF
Java Course 8: I/O, Files and Streams
PPTX
Data file handling
PPT
File in cpp 2016
PPT
File handling in_c
PPS
Files & IO in Java
PDF
C++ Files and Streams
PPTX
Understanding java streams
PPTX
Java Input Output (java.io.*)
PDF
Java I/o streams
PPT
File handling
PPT
7 streams and error handling in java
PPT
File handling in C++
PPTX
L21 io streams
PPT
Cpp file-handling
PPSX
Files in c++
Handling I/O in Java
basics of file handling
Java IO
17 files and streams
Java Course 8: I/O, Files and Streams
Data file handling
File in cpp 2016
File handling in_c
Files & IO in Java
C++ Files and Streams
Understanding java streams
Java Input Output (java.io.*)
Java I/o streams
File handling
7 streams and error handling in java
File handling in C++
L21 io streams
Cpp file-handling
Files in c++
Ad

Viewers also liked (16)

PPT
Unit v
PPT
Jedi Slides Intro2 Chapter12 Advanced Io Streams
PPTX
Dynamic Memory Allocation(DMA)
PPT
Java API, Exceptions and IO
PPT
Various io stream classes .47
PPTX
Learn advanced java programming
PPT
Java IO Package and Streams
PPTX
Java Applets
PPT
Memory allocation in c
PPTX
Dynamic memory allocation(memory,allocation,memory allocatin,calloc,malloc,re...
PPT
File handling in c
PPS
Advance Java
PPT
Itp 120 Chapt 19 2009 Binary Input & Output
PPS
PPT
Core java slides
PPTX
Data mining
Unit v
Jedi Slides Intro2 Chapter12 Advanced Io Streams
Dynamic Memory Allocation(DMA)
Java API, Exceptions and IO
Various io stream classes .47
Learn advanced java programming
Java IO Package and Streams
Java Applets
Memory allocation in c
Dynamic memory allocation(memory,allocation,memory allocatin,calloc,malloc,re...
File handling in c
Advance Java
Itp 120 Chapt 19 2009 Binary Input & Output
Core java slides
Data mining
Ad

Similar to I/O in java 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
PPTX
Chapter 10.1
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
PPT
14 file handling
 
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
Chapter 10.1
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
14 file handling
 

More from ashishspace (7)

PDF
I/O In Java Part 2
PDF
Remote Method Invocation
PDF
Understanding Date Handling and Collection Framework
PDF
Nested and Enum in Java
PDF
HTTP Basic and Servlet Basic
PDF
Java packages and access specifiers
PDF
Data types and operators and statements
I/O In Java Part 2
Remote Method Invocation
Understanding Date Handling and Collection Framework
Nested and Enum in Java
HTTP Basic and Servlet Basic
Java packages and access specifiers
Data types and operators and statements

Recently uploaded (20)

PDF
Computing-Curriculum for Schools in Ghana
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
Cell Structure & Organelles in detailed.
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Basic Mud Logging Guide for educational purpose
PPTX
Pharma ospi slides which help in ospi learning
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
GDM (1) (1).pptx small presentation for students
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
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Sports Quiz easy sports quiz sports quiz
PDF
Pre independence Education in Inndia.pdf
PDF
RMMM.pdf make it easy to upload and study
PPTX
Final Presentation General Medicine 03-08-2024.pptx
Computing-Curriculum for Schools in Ghana
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
102 student loan defaulters named and shamed – Is someone you know on the list?
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Supply Chain Operations Speaking Notes -ICLT Program
Cell Structure & Organelles in detailed.
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
Renaissance Architecture: A Journey from Faith to Humanism
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Basic Mud Logging Guide for educational purpose
Pharma ospi slides which help in ospi learning
TR - Agricultural Crops Production NC III.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
GDM (1) (1).pptx small presentation for students
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Sports Quiz easy sports quiz sports quiz
Pre independence Education in Inndia.pdf
RMMM.pdf make it easy to upload and study
Final Presentation General Medicine 03-08-2024.pptx

I/O in java 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