File
allows you to look at these
attributes of a file.
File
ClassFile
does not let you do any I/O.
File
just has methods to look at and modify file-related
information (the so-called meta-data), not file contents.
File
class allow you to do:
File
object, given a String containing a path name.
File
object exists, can be read/written.
File
object is a file or a directory.
javap
. For example:
$ javap java.io.File
File
to
provide information about a file or a directory.
public class File { //constructors: public File(String path) public File(string path, String name) public File(File dir, String name) //deleting a file: public boolean delete() //to do with file name: public String getName() public String getPath() public String getAbsolutePath() public String getParent() public boolean renameTo(File dest) //getting the attributes of a file: public boolean exists() public boolean canWrite() public boolean canRead() public boolean isFile() public boolean isDirectory() public native boolean isAbsolute() public long lastModified() public long length() public boolean equals(Object obj) //to do with the directory: public boolean mkdir() public boolean mkdirs() public String[] list() public String[] list(FilenameFilter filter) //miscellaneous: public static final char pathSeperatorChar public int hashCode() public string toString()
File
object
if one of those operations is of interest to you.
Reader
is an abstract class, so it can be subclassed by other
classes in the java.io
package before it is used in practice.
Reader
, corresponding
to four possible sources of data:
FileReader
- Translates bytes from a file into a character stream
InputStreamReader
- Fetch characters from a byte stream
StringReader
- Gets characters from a String
CharArrayReader
- Gets characters from a char array
FileReader
to access and
count the number of bytes in a file.
File.length()
value. They should
be equal.
import java.io.*; public class exfileread { public static void main(String args[]) { int i=0; int bytesRead=0; try { FileReader fr = new FileReader( "animals.txt" ); while (i != -1) { i = fr.read(); if (i != -1) bytesRead++; } } catch (IOException ioe) { System.out.println( "IO error:" + ioe ); } System.out.println("bytes read from file:"+bytesRead); File f = new File("animals.txt") System.out.println("bytes in File.length(): "+f.length()); } }
animals.txt
with the names of
a few animals in it, and run the program, it will look something like this:
java exfileread bytes read from file: 27 bytes in File.length(): 27
FileReader
offers several constructors. Here are some of them:
File
object as an argument.
open()
step as
occurs in C.
open()
, there is a
close()
.
close()
to close your output
streams to ensure that the last few bytes are flushed into the system.
Writer
is an abstract class, so it is subclassed by other classes
in the java.io
package before it is used in practice (just like
with Reader
as we just saw).
Writer
, corresponding
to four possible destinations for the data:
FileWriter
- Writes a character stream into a file.
InputStreamWriter
- Writes characters into a byte stream.
StringWriter
- Writes characters into a String.
CharArrayWriter
- Writes characters into a char array.
Writer
methods outlined
above.
FileWriter
to write
whatever you type into a file. Provide the file name as a command-line
argument:
import java.io.*; public class exfilewrite { public static void main(String args[]) { FileWriter fw; int i; try { fw = new FileWriter(args[0]); while ( (i=System.in.read()) != -1) { fw.write( (char) i); } fw.close(); } catch (IOException ioe) { System.out.println("IO error:" + ioe); } } }
CharConversionException StreamCorruptedException WriteAbortedException FileNotFoundException InterruptedIOException URFDataFormatError EOFException
EOFException
would better be named
"UnexpectedEOFException", as it is only raised when the programmer has asked
for a fixed definite amount of input, and the end of file is reached before all
the requested amount of data has been obtained.
EOFException
is raised in only three classes:
DataInputStream
, ObjectInputStream
, and
RandomAccessFile
, (and their subclasses, of course).
EOFexception
.
abstract
appears at the start of a class
declaration, it means that zero or more of its methods are abstract.
abstract class WaterBorneTransport { abstract void set_direction (int n); abstract void set_speed (int n); }
class Canoe extends WaterBorneTransport { void set_direction (int n) { ... void set_speed (int n) { ... }
interface FlyingMachine { int navigate(Point from, Point to); void land(); void takeoff(double fuel); }
class helicopter implements FlyingMachine { double fueltank; int engine_rpm; int rotors; int navigate(Point from, Point to) { //full body of code appears here } void land() { for( ; engine_rpm>0; engine_rpm--); } void takeoff(double fuel) { fueltank += fuel; for( ; engine_rpm<6000; engine_rpm++); } void hover() { //full body of code appears here } //other methods can be in the class too }
interface FloatingVessel { int navigate(Point from, Point to); void dropAnchor(); void weighAnchor() }
class Seaplane implements FloatingVessel, FlyingMachine { double fueltank; int engine_rpm; int anchorline; void dropAnchor() { anchorline = 200; } void weighAnchor() { anchorline = 0; } int navigate(Point from, Point to) { //full body of code appears here } void land() { for ( ; engine_rpm>0; engine_rpm--) ; dropAnchor(); } void takeoff(double fuel) { weighAnchor(); fueltank += fuel; for( ; engine_rpm<6000; engine_rpm++) ; } //other methods can be in the class too }