Example of using File class Consider a text file called examples.txt containing the following 2 lines of text: it has 2 lines of text
We can write an application ( FileTest1.java ) to interrogate this file via an object of class File: public class FileTest1 { public static void main( String args[] ) { String fileName = "example.txt"; File f1 = new File( fileName ); System.out.println("properties of file " + fileName ); System.out.println("canRead() : " + f1.canRead() ); System.out.println("canWrite() : " + f1.canWrite() ); System.out.println("length() : " + f1.length() ); } } // class
The result of running our application is the following: 
If we edit examples.txt to make it contain just the character: A the output of the application is as follows: 
You might like to try compiling this application, and changing the contents of file examples.txt to see the length in bytes change (note that the end of line character also takes up a byte
). Back to top 
RITSEC - Global Campus Copyright ?1999 RITSEC- Middlesex University. All rights reserved. webmaster@globalcampus.com.eg |