0% found this document useful (0 votes)
10 views57 pages

Iofiles

Uploaded by

ash909077
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views57 pages

Iofiles

Uploaded by

ash909077
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 57

Managing Input/output files in

Java
● Java I/O (Input and Output) is used to process the input and produce the
output.
● Java uses the concept of a stream to make I/O operation fast. The java.io
package contains all the classes required for input and output operations.

● We can perform file handling in Java by Java I/O API


Data Representation in java file
Concept of Streams
Managing Input/output files in
Java
● I/O package supports many serialized package:

1. RandomAccessFile: read/write byte ,text and data to any loication in file

2. Stream Tokenizer: sub class of object used to breaking up a stream of text from an
input text file into meaningful pieces called tokens.
Using the file class
● Java.io package includes a class File.
● This class contains several methods for java file:
● 1. Creating a file
● 2. opening a file
● 3. Closing a file
● 4. Deleting a File
● 5. Getting the name of a file
● 6. Getting the size of a file
● 7. Checking the existence of file
● 8. Renaming a file
● 9. Checking whether the file is writable
● 10. Checking whether the file is readable
I/O Exception
● The basic i/o exception and its functions:
● 1. EOFException: Signals that an end of file or end of stream has been reached
unexpectedly during input
● 2. FileNotFoundexception: informs that a file could not be found.
● 3. InterruptedIOException: warns that an IO operations has been interrupted
● 4. IOException: signals that an IO exception of some sort has occured
Creation of Files
● We need to decide about inteded purpose of a file:
● 1. suitable name for a file
● Data type to store
● Purpose- read/write or update
● Method of creaating a file
● Example
● FileInputStream fis,
● Try
● {
● Fis=new FileInputStream(“test.dat”);
● }
● catch(IOException e)
Creation of Files
● ......
● ......... The code includes five tasks:
1. Select a filename
● File inFile;
2. Declare a file object
● InFile =new File(“test.dat”);
3. Give the selected name to tyhe file object
● FileInputStream fis; declared
4. declare a file stream objet
● Try
5. connect the file to the file stream object
● {
● Fis =new FileInputStream(inFile);
● }
● Catch(.........)
● .....
● .........
Reading/Writing character
● Reader and writer implement streams that can handle characters. The two subclasses are
FileReader and FileWriter. Refer program 16.1
Reading/Writing Bytes
● File reader and File Writer classes to read and write 16bit characters. Two commonly
used classes for handling bytes are FileInputstream and File OutputStream. Refer
program 16.2,16.3 and 16.4
Reading from and writing to file
Interactive Input/Output
● Simple Input/Output:
● The system class contains three I/O objects,namely System.in, System.out
and system.err where in, out and err are static variables.
● The variable in is of InputStream type and the otjher two are of prntstream
type.
● Graphical Input and Output:GUI is about Frames, textboxes , buttons,
lables . These are the methods from java.awt package

You might also like