File class.48


Published on

  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • When designing an applet, you must remember the things that an applet cannot do. Some Web browsers or applet viewers may place different restrictions on applets. In general, applets cannot: read files on a local system write files to the local system delete files on the local system rename files on the local system create a directory on the local system list directory contents check for the existence of a file obtain the type, size or modification time of a file create a network connection to a system other than the one it was loaded from define system properties invoke any program on the local system manipulate any threadgroup, other than its own access or load classes other than the standard ones in Java api define classes that are part of packages on the local system
  • File class.48

    1. 1. File class 1
    2. 2. ObjectivesOn completion of this period, you would be able to learn • File class • Example program using file class • Example program to check the directory properties 2
    3. 3. RecapIn the last class, we have studied about the concept of streams• Stream is a flow bits of information either to or from input or output devices into or from a program• The various input output stream classes 3
    4. 4. File Class• It directly deals with files and the file system• File class does not specify how information is retrieved from or stored in files• It describes the properties of a file itself 4
    5. 5. File Class contd..• File class defines many methods that obtain the standard properties of a File object• getName( ) returns the name of the file• getParent( ) returns the name of the parent directory 5
    6. 6. File Class contd..• exists( ) returns true if the file exists, false if it doesnot• length() : returns length of a file in bytes• isFile() : check whether the file is normal file ordirectory file 6
    7. 7. // Program for finding File propertiesThe following program shows the use of File classimport;class FileDemo {static void p(String s) {System.out.println(s); }public static void main(String args[]) {File f1 = new File("/java/COPYRIGHT");p("File Name: " + f1.getName());p("Path: " + f1.getPath());p("Abs Path: " + f1.getAbsolutePath());p("Parent: " + f1.getParent());p(f1.exists() ? "exists" : "does not exist"); 7
    8. 8. // Program for finding File properties Contd . . .p(f1.canWrite() ? "is writeable" : "is not writeable");p(f1.canRead() ? "is readable" : "is not readable");p("is " + (f1.isDirectory() ? "" : "not" + " a directory"));p(f1.isFile() ? "is normal file" : "might be a named pipe");p(f1.isAbsolute() ? "is absolute" : "is not absolute");p("File last modified: " + f1.lastModified());p("File size: " + f1.length() + " Bytes");}}• When you run the program, it displays the properties of the selected file 8
    9. 9. Creating Directories• File utility methods are mkdir( ) and mkdirs( )• The mkdir ( ) method creates a directory, returning true on success and false on failure• mkdirs( ) method creates a directory for which no path exists 9
    10. 10. Other Method Related To Directory• isDirectory (): returns Boolean can be used to check the given file is a directory true directory false a file• list() : returns the list of file names available in given directory. 10
    11. 11. // Program for checking for directory propertiesimport;class DirList {public static void main(String args[]) {String dirname = "/java";File f1 = new File(dirname);if (f1.isDirectory()) {System.out.println("Directory of " + dirname);String s[] = f1.list(); 11
    12. 12. // Program for checking for directory properties Contd . . .for (int i=0; i < s.length; i++) {File f = new File(dirname + "/" + s[i]);if (f.isDirectory()) {System.out.println(s[i] + " is a directory");} else {System.out.println(s[i] + " is a file"); } }} else {System.out.println(dirname + " is not a directory");}}}• Running this program produces output based on what was in “/java” directory 12
    13. 13. Summary• File class describes the properties of a file itself• File defines many methods that obtain the standard properties of a File object• File utility methods for creating directories are mkdir( ) and mkdirs( ) 13
    14. 14. Quiz1. File class does not specify how information is retrieved from or stored in files A. True B. False 14
    15. 15. Quiz contd..2. Which File method returns the name of the file A. getName() B. getParent( ) C. exists() 15
    16. 16. Frequently Asked Questions1. Write a java program to find the various properties of a File class2. Explain File class, methods and attributes of it 16