Вы находитесь на странице: 1из 36

File Class

Prof. Satyawan C. Hembade

File Class
File class is used to navigate the file system. Constructing a File instance (or GarbageCollecting it) never affects the file system. File class doesnt have a method to change the current working directory.

File Constructors

Constructor
File(File dir, String name)

Description
Creates a File instance that represents the file with the specified name in the specified directory Creates a File instance that represents the file whose pathname is the given path argument. Creates a File instance whose pathname is the pathname of the specified directory, followed by the separator character, followed by the name argument.

File(String path)

File(String path, String name)

File methods
Method Description

boolean canRead() boolean canWrite() boolean delete() boolean exists() String getAbsolutePath() String getCanonicalPath() String getPath()

Tests if the application can read from the file. Tests if the application can write to this file. Deletes the file specified by this object. Tests if this File exists. Returns the absolute pathname of the file represented by this object. Returns the canonical form of this File object's pathname. [.. and . are resolved. ] Returns the pathname of the file represented by this object.

File methods
String getParent() Returns the parent part of the pathname of this File object, or null if the name has no parent part. String getName() Returns the name of the file represented by this object. boolean Tests if the file represented by this File object isAbsolute() is an absolute pathname. boolean Tests if the file represented by this File object isDirectory() is a directory. boolean isFile() Tests if the file represented by this File object is a "normal" file.

File methods
long lastModified() Returns the time that the file represented by this File object was last modified. long length() Returns the length of the file (in bytes) represented by this File object. String[] list() Returns a list of the files in the directory specified by this File object. String[] Returns a list of the files in the directory list(FilenameFilter) specified by this File that satisfy the specified filter. FileNameFilter is an interface that has a method accept(). This list method will call accept for each entry in the list of files and only returns the files for which accept returns true.

File methods
boolean mkdir() boolean mkdirs() Creates a directory whose pathname is specified by this File object. Creates a directory whose pathname is specified by this File object, including any necessary parent directories. Renames the file specified by this File object to have the pathname given by the File argument.

boolean renameTo(File)

Java Input/Output Streams

Prof. Satyawan C. Hembade

What is a Stream?
In physical world, a stream is a continuous flow of water

Data Streams

What is a Stream?
z In java, a byte stream is a sequence of bytes. z An input stream is a source from which bytes are read one by one. Eg, a keyboard, a disk file opened for reading. z An output stream is a destination to which bytes are written one by one. Eg, a monitor, a printer, a disk file opened for writing.

Input Streams

Output Streams

How Files and Streams Work:


InputStream can flow the data from a disk file to the internal memory OutputStream can flow the data from the internal memory to a disk file. The disk-file may be a text file or a binary file

I/O Class Hierarchy

Reading/Writing Disk File


A disk file for reading in a program is mapped to an object of FileInputStream. A disk file for writing in a program is mapped to an object of FileOutputStream.

Methods of InputStream Class


int int int int read() read(byte cbuf[]) read(byte cbuf[], int offset, int length) available() //Throws IOException

Returns the number of bytes left in the file that can be read.

(Decrement by 1 after a byte is read.)

void close()
Closes the file.

//Throws IOException

Marks the current position in this input stream. Tests if this input stream supports the mark and reset methods.

void mark(int readlimit)

Boolean markSupported()

OutputStream Class
Writing data
int write(int c) int write(byte cbuf[]) int write(byte cbuf[], int offset, int length) void close()
Closes this output stream and releases any system resources associated with this stream.

void flush()

Flushes this output stream and forces any buffered output bytes to be written out.

File Read/ Write


Object InputStream FileInputStream OutputStream FileOutputStream

read() close() ...


outFile.dat inFile.dat
Disk

write() close() ...

FileInputStream
FileInputStream( String fileName) Creates a FileInputStream object and maps it to an actual disk file for reading //Throws FileNotFoundException int available() //Throws IOException Returns the number of bytes left in the file that can be read. (Decrement by 1 after a byte is read.) int read() //Throws IOException Reads a byte of data from the file. Returns -1 if the end of file is reached. void close() //Throws IOException Closes the file.

Reading and Writing Algorithm for Data


Reading open a stream while more information
read information

close the stream

Writing open a stream while more information write information close the stream

Reading a File
try { FileInputStream r = new FileInputStream( e.dat); int a = r.available(); int nByte = 0; int d = r.read(); while ( d != -1) { ++nByte; System.out.print( (char) d); d = r.read(); } r.close(); System.out.println( "\nnByte = " + nByte); System.out.println( "r.available() = " + a ); } catch ( IOException e) { System.out.println("\n### IO Error: " + e); }

Class FileOutputStream
z FileOutputStream( String fileName) Creates an OutputFileStream object and maps it to the disk file with the specified name for writing. z Void write( int b) Writes the specified byte to the file. z Void close( ) z The exceptions thrown are similar to that of FileInputStream.

Writing to a File
try { FileOutputStream fos = new FileOutputStream( sam.dat); int nByte = 0; int d = System.in.read();

while( d != 4) { ++nByte;
fosr.write( d);

// 4 is the code for <crtl-d>

} catch ( IOException e) { System.out.println("\n### IO Error: " + e); }

} fos.close();

d = System.in.read();

Filter I/O Streams


Special streams which filter input and output bytes and manipulate data reading from an underlying stream. Allows the user to make a chain using multiple input stream so that, the operations that are to be applied on this chain, may create a combine effects on several filters. By using these streams, there is no need to convert the data from byte to char while writing to a file. These are the more powerful streams than the other streams of Java.

Filter Streams

DataStreams
Filtered streams that perform binary I/O operation on primitive data type values and String Values If you need to work with data that is not represented as bytes or characters then you can use Data Streams These streams filter an existing byte stream so that each primitive data types can be read from or written to the stream directly. e.g. DataInputStream and DataOutputStream

DataInputStream
Allows you to read binary represented data of Java primitive data types from an underlying input stream in a machine-independent way. It reads only Java primitive data types and doesn't read the object values. Constructor accepts an existing input stream as argument that is to be filtered (e. g BufferedInputStream or FileInputStream).

DataInputStream Methods
The read( ) method is used to read the data according to its types. For example, the readInt( ) method reads the int type of value while the readFloat() method reads the fraction value. The readLine( ) Methods reads the string per line from a file.

DataOutputStream
Class that allows you to write binary represented data of Java primitive data types reading from an underlying output stream in a machineindependent way. It writes only Java primitive data types and doesn't write the object values. A data output stream is created with the constructor of DataOutputStream with argument an existing output stream e.g BufferedOutputStream or FileOutputStream).

DataOutputStream Methods
The write( ) method is used to write the data according to its types. For example, the writeInt( ) method writes the int type of value while writeFloat() method writes the fraction value. The writeUTF( ) method writes the string per line to a file.

Readers and Writers

Readers

Shaded: Those that read from or write to data sinks Unshaded: perform some sort of processing .

Writer Hierarchy

Reading and Writing Data


Reading Data
int read() int read(char cbuf[]) int read(cha

Writing Data
int write(int c) int write(char cbuf[]) int write(char cbuf[], int offset, int length)

Вам также может понравиться