0% found this document useful (0 votes)
12 views15 pages

Java File Operations Guide

The document provides an overview of file operations in Java, explaining the importance of files for data persistence beyond program execution. It details the types of file streams, the order of file operations, and includes code examples for reading and writing both text and binary files. Additionally, it covers concepts like buffering, appending data, and handling exceptions during file operations.

Uploaded by

hitesh.aimes
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)
12 views15 pages

Java File Operations Guide

The document provides an overview of file operations in Java, explaining the importance of files for data persistence beyond program execution. It details the types of file streams, the order of file operations, and includes code examples for reading and writing both text and binary files. Additionally, it covers concepts like buffering, appending data, and handling exceptions during file operations.

Uploaded by

hitesh.aimes
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

Page |1

ISC COMPUTER SCIENCE Class - 11th

COMPUTER ACADEMY
9559961111
Page |2

Operations on File in Java


What is a file: A file is a named location on a secondary storage media where data are permanently stored
for later access.

Why Files here is Java ? Till now when we execute a Java program the program data( User input , values
comes after calculation or any data stored in an object) is get stored in RAM and its lifetime is till the
program is executing . So, if we need that data again when we execute the same program or by some other
program we will not get it. But if we create a file and store that data to the file before its lifetime gets over
then we can access it whenever we want it.

Java streams: There are 2 stream classes in Java to perform input and output. These are stored in [Link]
package.

Input Stream classes


Byte stream classes
(Binary files)

Output Stream classes

[Link]

Reader classes
Character stream
classes (Text files)
Writer classes

COMPUTER ACADEMY
9559961111
Page |3

Character Files (text files) : store the character using Unicode


Types of files
Binary Files : store the data in machine readable form.

Order of operations on a file:

1. Open a file

2. Read or write/append (perform operation)

3. Close the file

Buffer: It is a temporary storage used to hold data until enough has been collected that it is worth
transferring. It can be used for input & output both. ( input buffer , Output buffer )

COMPUTER ACADEMY
9559961111
Page |4

Text files
Writing to a Text file / Output to a text file
import [Link].*;
import [Link].*;
public class File
{
public static void main( String aa[ ] )
{
try
{
Scanner sc=new Scanner([Link]);
FileWriter fout =new FileWriter("[Link]");
BufferedWriter bw=new BufferedWriter(fout);
PrintWriter pw = new PrintWriter(bw);
[Link]("Enter Your name:");
String s=[Link]( );
[Link](s);
[Link]( );
}
catch(IOException e)
{
[Link](e);
}
}
}

COMPUTER ACADEMY
9559961111
Page |5

PrintWriter pw = new PrintWriter (new BufferedWriter(new FileWriter("[Link]")));


import [Link].*;
import [Link].*;
public class File
{
public static void main(String aa[ ]) {
try {
Scanner sc=new Scanner([Link]);
PrintWriter pw = new PrintWriter (new BufferedWriter(new FileWriter("[Link]")));
[Link]("How many names to be write:");
int n = [Link]( );
[Link]( );
for ( int i=1; i<=n ; i++) {
[Link]("Enter Your name:" + i +" ");
String s=[Link]( );
[Link](s);
}
[Link]( );
}
catch(IOException e)
{
[Link](e);
}
}
}

COMPUTER ACADEMY
9559961111
Page |6

//Input Using BufferedReader class instead of Scanner class


import [Link].*;
public class File
{
public static void main(String aa[ ])
{
try
{
BufferedReader br = new BufferedReader(new InputStreamReader([Link]));
PrintWriter pw = new PrintWriter (new BufferedWriter(new FileWriter("[Link]")));
[Link]("How many names to be write:");
int n = [Link]([Link]( ));
for ( int i=1; i<=n ; i++)
{
[Link]("Enter Your name:" + i +" ");
String s=[Link]( );
[Link](s);
}
[Link]( );
}
catch(IOException e)
{
[Link](e);
}
} }

COMPUTER ACADEMY
9559961111
Page |7

By default the file is created in the current working directory of Java if we give only the file name (Relative
path) but we can choose the location of the file we can use Absolute path ( Full path) while opening the file.

e.g.

FileWriter fout =new FileWriter("D:\Data\Myfiles\[Link]");

Appending the data to a Text file: In outputStream a file is create and get opened when ew try to open the
a file but if the file is already exist on the given location the outputStream perform overwriting means
previously written data get erased. But we can save the previously written data if u want to just passing an
extra argument while creating FileWriter object.

FileWriter fw = new FileWriter("[Link]" , true );

flush( ) : Used to clear the buffer.

COMPUTER ACADEMY
9559961111
Page |8

Reading from a Text file / Input from a text file

import [Link].*;
class File2
{
public static void main(String aa[ ])
{
try{
FileReader f=new FileReader("[Link]");
BufferedReader fin=new BufferedReader(f);
String s="";
while(true)
{
s=[Link]( );
if (s==null)
break;
else
[Link]( s);
}
}
catch(IOException e){
[Link](e);
}
}
}

COMPUTER ACADEMY
9559961111
Page |9

OR
import [Link].*;
class File2
{
public static void main(String aa[ ])
{
try
{
BufferedReader fin=new BufferedReader(new FileReader("[Link]"));
String s="";
while((s=[Link]( ))!=null)
{
[Link]( s);
}
}
catch(IOException e){
[Link](e);
}
}
}

COMPUTER ACADEMY
9559961111
P a g e | 10

Binary files
Writing to a Binary file / Output to a Binary file
import [Link].*;
public class File3 {
public static void main(String aa[ ]) {
BufferedReader br = new BufferedReader(new InputStreamReader([Link]));
try {
FileOutputStream f=new FileOutputStream("[Link]");
DataOutputStream fout=new DataOutputStream(f);
[Link]("Enter the Rollno , Name and Total Marks of Student");
int r=[Link]([Link]( ));
String n=[Link]( );
double m=[Link]([Link]( ));
[Link](r);
[Link](n);
[Link](m);
[Link]( );
[Link]( );
}
catch(IOException e)
{
[Link](e);
}
}
}

COMPUTER ACADEMY
9559961111
P a g e | 11

writeByte ( byte ) – to write a byte type value

writeShort ( short ) – to write a short type value

writeInt ( int ) – to write a int type value

writeFloat ( byte ) – to write a float type value

writeDouble ( double ) – to write a double type value

writeLong ( long ) – to write a long type value

writeChar ( char ) – to write a char type value

writeBoolean ( boolean ) – to write a boolean type value

writeBytes ( String ) – to write a String type value

writeChars ( String ) – to write a String type value

writeUTS ( String ) – to write a Unicode transformed String type value

COMPUTER ACADEMY
9559961111
P a g e | 12

Reading from a binary file / Input from a binary file


import [Link].*;
public class File4 {
public static void main(String aa[ ])
{
boolean eof=false;
try
{
FileInputStream f=new FileInputStream("[Link]");
DataInputStream fin=new DataInputStream(f);
while (!eof)
{
try {
int r=[Link]( );
String n=[Link]( );
double m=[Link]( );
[Link](r+"\t"+n+"\t"+m);
}
catch(EOFException e1)
{
[Link]("File ended");
eof=true;
}

COMPUTER ACADEMY
9559961111
P a g e | 13

catch(IOException e2)
{
[Link](e2);
}
}
}
catch(FileNotFoundException e3)
{
[Link]("Wrong File Name");
}
}
}

Note: The inputstreams automatically closed when End of File is reached.

COMPUTER ACADEMY
9559961111
P a g e | 14

import [Link].*;
public class File4 {
public static void main(String aa[ ]) throws IOException
{
boolean eof=false;
try
{
FileInputStream f=new FileInputStream("[Link]");
DataInputStream fin=new DataInputStream(f);
while (!eof)
{
try {
int r=[Link]( );
String n=[Link]( );
double m=[Link]( );
[Link](r+"\t"+n+"\t"+m);
}
catch(EOFException e1)
{
[Link]("File ended");
eof=true;
}

}
}

COMPUTER ACADEMY
9559961111
P a g e | 15

catch(FileNotFoundException e3)
{
[Link]("Wrong File Name");
}
}
}

COMPUTER ACADEMY
9559961111

You might also like