您的位置:寻梦网首页编程乐园Java天地Core JavaJava Lecture Notes

Introduction

Content

Apply

Reflect

Extend

previous.gif
 (3087 bytes)

next.gif
 (2959 bytes)

 

Content Index

Content Page # 18

Example of using File class

Consider a text file called examples.txt containing the following 2 lines of text:

This is a test file

it has 2 lines of text

We can write an application ( FileTest1.java ) to interrogate this file via an object of class File:

import Java.io.*;

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

basicline.gif (169 bytes)

RITSEC - Global Campus
Copyright ?1999 RITSEC- Middlesex University. All rights reserved.
webmaster@globalcampus.com.eg