Pug Files
Pug Files
IO
I/O is used to process the input & produce the output based
on the input
The input & output operations are performed by using
the concept of streams.
java.io package contains all the classes required for input
and output operations.
READING & WRITING IN JAVA
Java provides many classes to perform the program input and
output
Program I/O refers to any I/O performed by the program
File I/O refers specifically to I/O performed on files.
STREAM
A stream is a sequence of data.
THREE STREAMS
1. System.out : Standard output stream
2. System.in : Standard input stream
3. System.err : Standard error
STREAM CLASSES
The streams in java are divided into two categories
I. Byte Stream Classes
II. Character Stream Classes
nd
Java I/O [Files, Directories] [ 2 Year IT-4/8-S & R]
BYTE STREAM CLASSES
Byte stream gives a convenient way for handling input and output of
bytes.
Byte Streams are used for handling bytes
Binary data (bytes) are stored either as 8-bit byte or an ASCII
character code set
These classes are used to read and write binary data.
Top two classes
1. Input Stream : used for reading
2. OutputStream : used for writing
Important methods
1. read() : reading data one byte at a time
2. write() : writing data one byte at a time
nd
Java I/O [Files, Directories] [ 2 Year IT-4/8-S & R]
nd
Java I/O [Files, Directories] [ 2 Year IT-4/8-S & R]
nd
Java I/O [Files, Directories] [ 2 Year IT-4/8-S & R]
InputStream methods
S.N Method Purpose
3. read(byte b[], int m, int n) Reads m bytes into b starting from nth byte
OutputStream methods
3. Write(byte b[], int m, int n) writes m bytes from array b starting from
nth byte
Return type : byte array
Syntax
FileOutputStream fin=new FileOutputStream (“out.txt”);
Drawbacks
It is not possible to write the primitive data types (e.g: int, float, char) to
file.
Explanation
Java Application
110011011101
fout
cric.txt
CS9256 Web Technology IT-4/8-S Page 9 of 63
nd
Java I/O [Files, Directories] [ 2 Year IT-4/8-S & R]
FILE INPUT STREAM
2. INPUT FILE
tn.txt
3. OUTPUT
2. OUTPUT
2. INPUT FILE
SOURCE FILE : IT.txt
TARGET FILE : CSE.txt
1. SOURCE CODE
package files_bytes;
import java.io.*; public
class DataInOut {
public static void main(String[] args)throws Exception
{
System.out.println("==================================");
System.out.println("\tData Input & Output Stream Classes");
System.out.println("==================================");
// connect the file using file object
File fp=new File("prim.dat");
FileOutputStream fout=new FileOutputStream(fp.getName());
CS9256 Web Technology IT-4/8-S Page 21 of 63
nd
Java I/O [Files, Directories] [ 2 Year IT-4/8-S & R]
DataOutputStream dout=new DataOutputStream(fout);
// Employee details
String name="laxman";
char blood='A';
int code=193;
double
pack=59000.50; try
{
/ write string to file
dout.writeUTF(name);
/ write int to file
dout.writeInt(code)
;
/ write character to file
dout.writeChar(blood);
/ write double to file
dout.writeDouble(pack);
fout.close();
dout.close();
System.out.println("File is created successfully ...");
/ open the file to access its contents
FileInputStream fin=new FileInputStream(fp.getName());
// wrap the FileInputStream in DataInputStream
DataInputStream din=new DataInputStream(fin);
System.out.println("Accessing File Data");
System.out.println("Employee Informations");
System.out.println("Name\t: "+din.readUTF());
System.out.println("Id\t: "+din.readInt());
System.out.println("Blood\t: "+din.readChar()+"+ive");
System.out.println("Money\t: "+din.readDouble());
fin.close();
din.close()
}
catch(Exception e)
{
System.out.println("Error in creating a file...");
}
CS9256 Web Technology IT-4/8-S Page 22 of 63
nd
Java I/O [Files, Directories] [ 2 Year IT-4/8-S & R]
}
}
2. OUTPUT
nd
Java I/O [Files, Directories] [ 2 Year IT-4/8-S & R]
III. SEQUENCE INPUT STREAM CLASS
It is used to read the data from more than one streams (multiple
Streams).
Syntax
SequenceInputStream ss=new SequenceInputStream(InputStream
s1, InputStream s2);
2. INPUT FILES
1. mit.txt
Welcome to MIT campus, Anna University
MIT is a green campus of anna university.
2. ceg.txt
Welcome to CEG campus of Anna university.
2. write(char str[])
Write a array of characters to file
Return type : void
3. write(int c)
Write a single character to file
Return type : void
Where,
c -> int specifying a character to be written
4. append(int c)
appends the specified character to the output writer
Return type : Writer
Where,
c -> the 16-bit character to append.
Drawbacks
It is not possible to write the line by line data.
nd
Java I/O [Files, Directories] [ 2 Year IT-4/8-S & R]
XI. EXAMPLE OF FILE WRITER CLASS
1. SOURCE CODE
package files;
import java.io.*;
public class Fwriter {
public static void main(String[] args)throws Exception
{
System.out.println("==============================");
System.out.println("\tFile Writer class");
System.out.println("==============================");
// FileWriter creation
FileWriter fw=new FileWriter("ram.txt");
String str="Jai sri ram...";
/ Write strings to file using
write() fw.write(str);
/ close the file
fw.close();
System.out.println("File is created successfully...");
}
}
3. OUTPUT FILE
ram.txt
2. read(char[] buff)
reads characters into array
Return type : int
Drawbacks
It is not possible to read line by line data from a file
XII. EXAMPLE OF FILEREADER CLASS
1. SOURCE CODE
package files; import
java.io.*; public
class Freader {
public static void main(String[] args)throws Exception
{
System.out.println("==========================================");
System.out.println("\tFile Reader class");
System.out.println("==========================================");
FileReader fr;
int ch;
try
{
CS9256 Web Technology IT-4/8-S Page 38 of 63
nd
Java I/O [Files, Directories] [ 2 Year IT-4/8-S & R]
/ connect the file by using the FileReader class
fr=new FileReader("mit.txt");
System.out.println("Contents of the file");
/ read the contents of a file by using read()
while((ch=fr.read())!=-1)
{
System.out.print((char)ch);
}
System.out.println();
fr.close();
}
catch(FileNotFoundException e)
{
System.out.println("File not found...");
}
}
}
4. close()
Closes the file & releases any resources associated with
the output stream
Return type : void
5. close()
Closes the file & releases any resources associated with
the output stream
Return type : void
XV. APPENDING TEXT TO FILE USING BUFFERED WRITER
1. SOURCE CODE
package files;
import java.io.*;
public class BuffWriter {
public static void main(String[] args)throws Exception
{
String text="Welcome to Tamil Nadu";
/ connect the file using file object
File fp=new File("tamil.txt");
/ connect
FileWriter fw=new FileWriter(fp.getName());
/ wrap the file writer to buffered writer
BufferedWriter bw=new BufferedWriter(fw);
try
{
/ verify the file using exist()
if(fp.exists()==false)
{
/ create a new empty file
CS9256 Web Technology IT-4/8-S Page 49 of 63
nd
Java I/O [Files, Directories] [ 2 Year IT-4/8-S & R]
fp.createNewFile();
}
/ append the text
bw.write(text);
/ close the file
bw.close();
System.out.println("File is created successfully at\n"+fp.getAbsolutePath());
}
catch(Exception e)
{
System.out.println("Error in Writing a file...");
}
}
2. OUTPUT FILE