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

Introduction

Content

Apply

Reflect

Extend

previous.gif
 (3087 bytes)

next.gif
 (2959 bytes)


Content Index

Content Page # 17

Java’s ‘File’ class

Java’s File class models both files and directories. It manipulates both files and directories as entities.

Often the first complication that new Java programmers experience when they begin to write programs that use files is the function of the File class. This class is used to manipulate files and directories, but only as whole entities. You cannot use the File class to read or write data . A File object is created like this:

File aFile = new File("filename");

Having created the object we have access to some useful methods, for example:

File.canRead(), File.canWrite()

Return true if the program is allowed to read or write the file

File.createNewFile()

As the name suggests, creates a new file with the specified filename

File.Delete()

Delete the file

File.length()

Returns the number of bytes in the file

The file class is also important because most of the other classes that read and write files will require the filename to be given as a File object, not as a text string.

Note: all the Java file handling classes are part of the package java.io, so you probably need to include the line

import java.io.*;

At the top of your program

Back to top

basicline.gif (169 bytes)

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