Professional Documents
Culture Documents
Module 4.2 - Java Programming
Module 4.2 - Java Programming
Module 4.2 - Java Programming
Files
• Secondary Storage Device
• Persistent Data – remains after program terminates
• File (Java) – Stream of Bytes
• End of File - End-of-file marker / count of byte
• Indicated by OS (Exception / Return value of method)
streams
Java I/O
• 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.
• file handling in Java is performed by Java I/O
API.
Streams
int i=System.in.read(); //returns ASCII code of 1st character
System.out.println((char)i); //will print the character
Hierarchy of classes In Streams
java.io (package) – File Processing
• Stream classes
– FileInputStream (byte-based i/p from a file)
– FileOutputStream (byte-based o/p to a file)
– FileReader (char-based i/p from a file)
– FileWriter (char-based o/p to a file)
• Inherits from
– InputStream
– OutputStream
– Reader
– Writer
• Objects
– ObjectInputStream
– ObjectOutputStream
• Character-based Stream
– Scanner (i/p data from keyboard / file)
– Formatter (o/p data to text-based stream ex: S.O.P)
File (class)
• Retrieve information about File / Directory
• Can open the file or manipulate the file using this object
• 4 Constructors
– File (String file name / directory name)
– File (String path name (absolute / relative path), String
file/dir)
– File (File object, String parent dir / dir)
– File (URI object)
• URI – Uniform Resource Identifier – general form of URL
• URI – locating files – vary with OS
• file://C:/data.txt windows
• file:/home/student/data.txt UNIX/Linux
File output stream(Bytes)
File output stream(Streams)
File input stream
BufferedInputStream
ByteArrayInputStream
FileInputStream
• This stream is used for reading data from the files.
Objects can be created using the keyword new and
there are several types of constructors available.
• Following constructor takes a file name as a string to
create an input stream object to read the file −
InputStream f = new FileInputStream ("C:/java/hello");
• Following constructor takes a file object to create an
input stream object to read the file.
File f = new File("C:/java/hello");
InputStream f = new FileInputStream(f);
Helper Methods
Sr.N Method & Description
o.
1 public void close() throws IOException{}
This method closes the file output stream. Releases any system resources associated
with the file. Throws an IOException.
try { System.out.print((char)is.read() +
byte bWrite [] = {11,21,3,40,5}; " ");
OutputStream os = new }
is.close();
FileOutputStream("test.txt");
} catch (IOException e) {
for(int x = 0; x < bWrite.length ; x++)
{ System.out.print("Exception");
os.write( bWrite[x] ); // writes }
the bytes }
} }
os.close();
Example To store student details in a text file
import java.io.*;
class StudentRecords
{
static BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
System.out.print("Address: ");
address = br.readLine();
System.out.print("Class: ");
Class = br.readLine();
System.out.print("Age: ");
age = Integer.parseInt(br.readLine());