SlideShare ist ein Scribd-Unternehmen logo
1 von 16
File class




             http://improvejava.blogspot.in/   1
Objectives

On completion of this period, you would be able
 to learn
   • File class
   • Example program using file class
   • Example program to check the directory properties




                    http://improvejava.blogspot.in/      2
Recap

In 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




                    http://improvejava.blogspot.in/       3
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




                  http://improvejava.blogspot.in/    4
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




                  http://improvejava.blogspot.in/             5
File Class                contd..

• exists( ) returns true if the file exists, false if it does
not
• length() : returns length of a file in bytes
• isFile() : check whether the file is normal file or
directory file




                     http://improvejava.blogspot.in/            6
// Program for finding File properties
The following program shows the use of File class
import java.io.File;
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");
              http://improvejava.blogspot.in/       7
// 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
                http://improvejava.blogspot.in/             8
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




                   http://improvejava.blogspot.in/      9
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.




                     http://improvejava.blogspot.in/     10
// Program for checking for directory properties
import java.io.File;
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();


               http://improvejava.blogspot.in/     11
// 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
                 http://improvejava.blogspot.in/          12
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( )




                    http://improvejava.blogspot.in/      13
Quiz

1. File class does not specify how information is
   retrieved from or stored in files

   A. True
   B. False




                   http://improvejava.blogspot.in/   14
Quiz                          contd..

2. Which File method returns the name of the file

   A. getName()
   B. getParent( )
   C. exists()




                     http://improvejava.blogspot.in/             15
Frequently Asked Questions

1. Write a java program to find the various
   properties of a File class
2. Explain File class, methods and attributes of it




                    http://improvejava.blogspot.in/   16

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (11)

C# File IO Operations
C# File IO OperationsC# File IO Operations
C# File IO Operations
 
Pi j4.1 packages
Pi j4.1 packagesPi j4.1 packages
Pi j4.1 packages
 
Javase7 1641812
Javase7 1641812Javase7 1641812
Javase7 1641812
 
I/O in java Part 1
I/O in java Part 1I/O in java Part 1
I/O in java Part 1
 
Intake 37 11
Intake 37 11Intake 37 11
Intake 37 11
 
File management53(1)
File management53(1)File management53(1)
File management53(1)
 
Java I/O
Java I/OJava I/O
Java I/O
 
Advanced Wiki training course
Advanced Wiki training courseAdvanced Wiki training course
Advanced Wiki training course
 
OSCh11
OSCh11OSCh11
OSCh11
 
Input output files in java
Input output files in javaInput output files in java
Input output files in java
 
Basic of java
Basic of javaBasic of java
Basic of java
 

Andere mochten auch

AR+S The Role Of Abstraction In Human Computer Interaction
AR+S   The Role Of Abstraction In Human Computer InteractionAR+S   The Role Of Abstraction In Human Computer Interaction
AR+S The Role Of Abstraction In Human Computer InteractionAli Rıza SARAL
 
IoT and META-engineering with Service Templates
IoT and META-engineering with Service TemplatesIoT and META-engineering with Service Templates
IoT and META-engineering with Service TemplatesHans A. Kielland Aanesen
 
Interface and abstraction
Interface and abstractionInterface and abstraction
Interface and abstractionRaghav Chhabra
 
Java topology abstraction service for ip-vp ns
Java  topology abstraction service for ip-vp nsJava  topology abstraction service for ip-vp ns
Java topology abstraction service for ip-vp nsecwayerode
 
Final Animation Assessment Presentation: The Abstraction of Bohemian Rhapsody.
Final Animation Assessment Presentation: The Abstraction of Bohemian Rhapsody. Final Animation Assessment Presentation: The Abstraction of Bohemian Rhapsody.
Final Animation Assessment Presentation: The Abstraction of Bohemian Rhapsody. Sarah Ju-En Tan
 
06 abstract-classes
06 abstract-classes06 abstract-classes
06 abstract-classesAnup Burange
 
Object Oriented Programing JAVA presentaion
Object Oriented Programing JAVA presentaionObject Oriented Programing JAVA presentaion
Object Oriented Programing JAVA presentaionPritom Chaki
 
Character stream classes .52
Character stream classes .52Character stream classes .52
Character stream classes .52myrajendra
 
Inner classes ,annoumous and outer classes in java
Inner classes ,annoumous and outer classes in javaInner classes ,annoumous and outer classes in java
Inner classes ,annoumous and outer classes in javaAdil Mehmoood
 
Encapsulation anonymous class
Encapsulation anonymous classEncapsulation anonymous class
Encapsulation anonymous classHarry Potter
 

Andere mochten auch (20)

Java JDBC
Java JDBCJava JDBC
Java JDBC
 
AR+S The Role Of Abstraction In Human Computer Interaction
AR+S   The Role Of Abstraction In Human Computer InteractionAR+S   The Role Of Abstraction In Human Computer Interaction
AR+S The Role Of Abstraction In Human Computer Interaction
 
IoT and META-engineering with Service Templates
IoT and META-engineering with Service TemplatesIoT and META-engineering with Service Templates
IoT and META-engineering with Service Templates
 
IEEE DEST CEE 2013 - Paper 4 (1) (1)
IEEE DEST CEE 2013 - Paper 4 (1) (1)IEEE DEST CEE 2013 - Paper 4 (1) (1)
IEEE DEST CEE 2013 - Paper 4 (1) (1)
 
Interface and abstraction
Interface and abstractionInterface and abstraction
Interface and abstraction
 
Java topology abstraction service for ip-vp ns
Java  topology abstraction service for ip-vp nsJava  topology abstraction service for ip-vp ns
Java topology abstraction service for ip-vp ns
 
Final Animation Assessment Presentation: The Abstraction of Bohemian Rhapsody.
Final Animation Assessment Presentation: The Abstraction of Bohemian Rhapsody. Final Animation Assessment Presentation: The Abstraction of Bohemian Rhapsody.
Final Animation Assessment Presentation: The Abstraction of Bohemian Rhapsody.
 
Core java questions
Core java questionsCore java questions
Core java questions
 
Intro to JavaScript
Intro to JavaScriptIntro to JavaScript
Intro to JavaScript
 
Java stereams
Java stereamsJava stereams
Java stereams
 
Java lecture
Java lectureJava lecture
Java lecture
 
06 abstract-classes
06 abstract-classes06 abstract-classes
06 abstract-classes
 
Object Oriented Programing JAVA presentaion
Object Oriented Programing JAVA presentaionObject Oriented Programing JAVA presentaion
Object Oriented Programing JAVA presentaion
 
Character stream classes .52
Character stream classes .52Character stream classes .52
Character stream classes .52
 
Inner classes ,annoumous and outer classes in java
Inner classes ,annoumous and outer classes in javaInner classes ,annoumous and outer classes in java
Inner classes ,annoumous and outer classes in java
 
PAC
PACPAC
PAC
 
Java Inner Classes
Java Inner ClassesJava Inner Classes
Java Inner Classes
 
Encapsulation anonymous class
Encapsulation anonymous classEncapsulation anonymous class
Encapsulation anonymous class
 
Abstract class
Abstract classAbstract class
Abstract class
 
Operators in java
Operators in javaOperators in java
Operators in java
 

Ähnlich wie Learn File Class in Java

File handling.pptx
File handling.pptxFile handling.pptx
File handling.pptxVishuSaini22
 
Android Lab Test : Reading the foot file list (english)
Android Lab Test : Reading the foot file list (english)Android Lab Test : Reading the foot file list (english)
Android Lab Test : Reading the foot file list (english)Bruno Delb
 
9780538745840 ppt ch05
9780538745840 ppt ch059780538745840 ppt ch05
9780538745840 ppt ch05Terry Yoast
 
Files and Directories in PHP
Files and Directories in PHPFiles and Directories in PHP
Files and Directories in PHPNicole Ryan
 
import required package file import java.io.File; The Filed.pdf
 import required package file import java.io.File; The Filed.pdf import required package file import java.io.File; The Filed.pdf
import required package file import java.io.File; The Filed.pdfanandinternational01
 
Wiesław Kałkus: C# functional programming
Wiesław Kałkus: C# functional programmingWiesław Kałkus: C# functional programming
Wiesław Kałkus: C# functional programmingAnalyticsConf
 
Character stream classes introd .51
Character stream classes introd  .51Character stream classes introd  .51
Character stream classes introd .51myrajendra
 
Csphtp1 17
Csphtp1 17Csphtp1 17
Csphtp1 17HUST
 
3.1 file input and output
3.1   file input and output3.1   file input and output
3.1 file input and outputallenbailey
 
Open Harvester - Search publications for a researcher from CrossRef, PubMed a...
Open Harvester - Search publications for a researcher from CrossRef, PubMed a...Open Harvester - Search publications for a researcher from CrossRef, PubMed a...
Open Harvester - Search publications for a researcher from CrossRef, PubMed a...Muhammad Javed
 
Java 7 - short intro to NIO.2
Java 7 - short intro to NIO.2Java 7 - short intro to NIO.2
Java 7 - short intro to NIO.2Martijn Verburg
 
chap 10 : Development (scjp/ocjp)
chap 10 : Development (scjp/ocjp)chap 10 : Development (scjp/ocjp)
chap 10 : Development (scjp/ocjp)It Academy
 
Android datastorage
Android datastorageAndroid datastorage
Android datastorageKrazy Koder
 

Ähnlich wie Learn File Class in Java (20)

Files
FilesFiles
Files
 
My History
My HistoryMy History
My History
 
File handling.pptx
File handling.pptxFile handling.pptx
File handling.pptx
 
Android Lab Test : Reading the foot file list (english)
Android Lab Test : Reading the foot file list (english)Android Lab Test : Reading the foot file list (english)
Android Lab Test : Reading the foot file list (english)
 
History
HistoryHistory
History
 
9780538745840 ppt ch05
9780538745840 ppt ch059780538745840 ppt ch05
9780538745840 ppt ch05
 
Files and Directories in PHP
Files and Directories in PHPFiles and Directories in PHP
Files and Directories in PHP
 
FileHandling.docx
FileHandling.docxFileHandling.docx
FileHandling.docx
 
import required package file import java.io.File; The Filed.pdf
 import required package file import java.io.File; The Filed.pdf import required package file import java.io.File; The Filed.pdf
import required package file import java.io.File; The Filed.pdf
 
Wiesław Kałkus: C# functional programming
Wiesław Kałkus: C# functional programmingWiesław Kałkus: C# functional programming
Wiesław Kałkus: C# functional programming
 
20-packages-jar.ppt
20-packages-jar.ppt20-packages-jar.ppt
20-packages-jar.ppt
 
Character stream classes introd .51
Character stream classes introd  .51Character stream classes introd  .51
Character stream classes introd .51
 
Csphtp1 17
Csphtp1 17Csphtp1 17
Csphtp1 17
 
File system
File systemFile system
File system
 
Java Week4(A) Notepad
Java Week4(A)   NotepadJava Week4(A)   Notepad
Java Week4(A) Notepad
 
3.1 file input and output
3.1   file input and output3.1   file input and output
3.1 file input and output
 
Open Harvester - Search publications for a researcher from CrossRef, PubMed a...
Open Harvester - Search publications for a researcher from CrossRef, PubMed a...Open Harvester - Search publications for a researcher from CrossRef, PubMed a...
Open Harvester - Search publications for a researcher from CrossRef, PubMed a...
 
Java 7 - short intro to NIO.2
Java 7 - short intro to NIO.2Java 7 - short intro to NIO.2
Java 7 - short intro to NIO.2
 
chap 10 : Development (scjp/ocjp)
chap 10 : Development (scjp/ocjp)chap 10 : Development (scjp/ocjp)
chap 10 : Development (scjp/ocjp)
 
Android datastorage
Android datastorageAndroid datastorage
Android datastorage
 

Mehr von myrajendra (20)

Fundamentals
FundamentalsFundamentals
Fundamentals
 
Data type
Data typeData type
Data type
 
Hibernate example1
Hibernate example1Hibernate example1
Hibernate example1
 
Jdbc workflow
Jdbc workflowJdbc workflow
Jdbc workflow
 
2 jdbc drivers
2 jdbc drivers2 jdbc drivers
2 jdbc drivers
 
3 jdbc api
3 jdbc api3 jdbc api
3 jdbc api
 
4 jdbc step1
4 jdbc step14 jdbc step1
4 jdbc step1
 
Dao example
Dao exampleDao example
Dao example
 
Sessionex1
Sessionex1Sessionex1
Sessionex1
 
Internal
InternalInternal
Internal
 
3. elements
3. elements3. elements
3. elements
 
2. attributes
2. attributes2. attributes
2. attributes
 
1 introduction to html
1 introduction to html1 introduction to html
1 introduction to html
 
Headings
HeadingsHeadings
Headings
 
Forms
FormsForms
Forms
 
Css
CssCss
Css
 
Views
ViewsViews
Views
 
Views
ViewsViews
Views
 
Views
ViewsViews
Views
 
Starting jdbc
Starting jdbcStarting jdbc
Starting jdbc
 

Learn File Class in Java

  • 1. File class http://improvejava.blogspot.in/ 1
  • 2. Objectives On completion of this period, you would be able to learn • File class • Example program using file class • Example program to check the directory properties http://improvejava.blogspot.in/ 2
  • 3. Recap In 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 http://improvejava.blogspot.in/ 3
  • 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 http://improvejava.blogspot.in/ 4
  • 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 http://improvejava.blogspot.in/ 5
  • 6. File Class contd.. • exists( ) returns true if the file exists, false if it does not • length() : returns length of a file in bytes • isFile() : check whether the file is normal file or directory file http://improvejava.blogspot.in/ 6
  • 7. // Program for finding File properties The following program shows the use of File class import java.io.File; 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"); http://improvejava.blogspot.in/ 7
  • 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 http://improvejava.blogspot.in/ 8
  • 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 http://improvejava.blogspot.in/ 9
  • 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. http://improvejava.blogspot.in/ 10
  • 11. // Program for checking for directory properties import java.io.File; 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(); http://improvejava.blogspot.in/ 11
  • 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 http://improvejava.blogspot.in/ 12
  • 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( ) http://improvejava.blogspot.in/ 13
  • 14. Quiz 1. File class does not specify how information is retrieved from or stored in files A. True B. False http://improvejava.blogspot.in/ 14
  • 15. Quiz contd.. 2. Which File method returns the name of the file A. getName() B. getParent( ) C. exists() http://improvejava.blogspot.in/ 15
  • 16. Frequently Asked Questions 1. Write a java program to find the various properties of a File class 2. Explain File class, methods and attributes of it http://improvejava.blogspot.in/ 16

Hinweis der Redaktion

  1. 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