File in Java

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

File Handling

The java.io package contains nearly every class you might ever need to perform input and
output (I/O) in Java. All these streams represent an input source and an output destination.
The stream in the java.io package supports many data such as primitives, Object, localized
characters, etc. A stream can be defined as a sequence of data. The InputStream is used to
read data from a source and the OutputStream is used for writing data to a destination.

Examples to Read Write from File are listed below

Reading Character from File

Declare and Create Input File

Used to Read Characters from File

read() method returns the integer


value of the character present in Text
File and returns “-1” to indicate End of
File.

To display character on screen use


(char) Integer Value for typecasting
Output

Writing Character to File

write() method is used to write the


contents to the File

Output
Read and Write to File

Output
Reading Bytes from File

Output
Writing Byte to File

Output
Reading Console Input
Java input console is accomplished by reading from System.in. To obtain a character-based
stream that is attached to the console, you wrap System.in in a BufferedReader object, to
create a character stream. Once BufferedReader is obtained, we can use read( ) method to
reach a character or readLine( ) method to read a string from the console.

1 Reading Characters
To read a character from a BufferedReader, use read( ) method. Each time that read( ) is
called, it reads a character from the input stream and returns it as an integer value. It returns
.1 when the end of the stream is encountered. As you can see, it can throw an IOException.

Output

2 Reading Strings
Writing Console Output
Console output is most easily accomplished with print( ) and println( ). These methods are defined
by the class PrintStream which is the type of the object referenced by System.out. Even though
System.out is a byte stream, using it for simple program output is still acceptable. Because
PrintStream is an output stream derived from OutputStream, it also implements the low-level
method write( ). Thus, write( ) can be used to write to the console. The simplest form of write( )
defined by PrintStream is shown below:

Java .io package


Java.io is a package which contains number of classes by using that classes we are able to sendthe data from
one place to another place.
In java language we are transferring the data in the form of two ways:-

1. Byte format

2. Character format

Stream/channel:-

It is acting as medium by using steam or channel we are able to send particular data from oneplace to the
another place.

Streams are two types:-


1. Byte oriented streams.(supports byte formatted data to transfer)
2. Character oriented stream.(supports character formatted data to transfer)

Byte oriented streams:-


Java.io.FileInputStream
To read the data from the destination file to the java application we have to use FileInputSream class.
To read the data from the .txt file we have to read() method.
Java.io.FileOutputStream:-
To write the data to the destination file we have to use the FileOutputStream.To write the
data to the destination file we have to use write() method.
Ex:- it will supports one character at a time.import
java.io.*;
class Test
{
static FileInputStream fis; static
FileOutputStream fos;
public static void main(String[] args)
{
try{
fis=new FileInputStream("get.txt"); fos=new
FileOutputStream("set.txt",true);int c;
while ((c=fis.read())!=-1)
{
fos.write(c);
}
fis.close();
fos.close();
}
catch(IOException io)
{
System.out.println("getting IOException");
}
}
}
Ex:-it will support one character at a time.import
java.io.*;
class Test
{
static FileReader fr;static FileWriter
fw;
public static void main(String[] args)
{
try{
fr=new FileReader("get.txt"); fw=new
FileWriter("set.txt",true);

int c;
while ((c=fr.read())!=-1)
{
fw.write(c);
}
fr.close();
fw.close();
}
catch(IOException io)
{
System.out.println("getting IOException");
}
}
}

Line oriented I/O:-


Character oriented streams supports single character and line oriented streams supports single line data.
BufferedReader:- to read the data line by line format and we have to use readLine() to read the data.
PrintWriter :- to write the data line by line format and we have to use println() to write the data.

import java.io.*;class
Test
{
static BufferedReader br;static
PrintWriter pw;
public static void main(String[] args)
{
try{
br=new BufferedReader(new FileReader("get.txt")); pw=new
PrintWriter(new FileWriter("set.txt"));

String line;
while ((line=br.readLine())!=null)
{
pw.println(line);
}
br.close();
pw.close();
}
catch(IOException io)
{
System.out.println("getting IOException");
}
}
}

Buffered Streams:-
Up to we are working with non buffered streams these are providing less performance becausethese are
interact with the hard disk, network.
Now we have to work with Buffered Streams
BufferedInputStream read the data from memory area known as Buffer.

We are having four buffered Stream classes


1. BufferedInputStream
2. BufferedOutputStream

3. BufferedReader

4. BufferedWriter

Ex:-
import java.io.*;class
Test
{
static BufferedReader br;static
BufferedWriter bw;
public static void main(String[] args)
{
try{
br=new BufferedReader(new FileReader("Test1.java")); bw=new
BufferedWriter(new FileWriter("States.java"));String str;
while ((str=br.readLine())!=null)
{
bw.write(str);
}
br.close();
bw.close();
}
catch(Exception e)
{
System.out.println("getting Exception");
}
}
}

Ex:-
import java.io.*;class
Test
{
static BufferedInputStream bis; static
BufferedOutputStream bos; public static void
main(String[] args)
{
try{
bis=new BufferedInputStream(new FileInputStream("abc.txt")); bos=new
BufferedOutputStream(new FileOutputStream("xyz.txt"));int str;
while ((str=bis.read())!=-1)
{
bos.write(str);
}
bis.close();
bos.close();
}
catch(Exception e)
{
System.out.println(e); System.out.println("getting Exception");
}
}
}

Ex:-
import java.io.*;class
Test
{
public static void main(String[] args) throws IOException
{
BufferedReader br=new BufferedReader(new FileReader("abc.txt"));String str;
while ((str=br.readLine())!=null)
{
System.out.println(str);
}

}
}

Java.util.Scanner:-
By using Scanner class we are able to divide the String into the number of tokens.To get
the integer value from the keyboard----------------------------- s.nextInt()
To get the String value from the keyboard --------------------- s.next()
To get the floating values from the keyboard------------------- s.nextFloat ();
i mport java.util.*;class
Test
{
public static void main(String[] args)
{
while (true)
{
Scanner s=new Scanner(System.in);
System.out.println("enter emp no");int eno=s.nextInt();

System.out.println("enter emp name");String ename=s.next();


System.out.println("enter emp salary");float
esal=s.nextFloat();

System.out.println("emp no-------------------------------------- >"+eno);


System.out.println("emp name ------------------------------------ >"+ename);
System.out.println("emp sal -------------------------------------- >"+esal);

System.out.println("do u want one more record(yes/no)");String


option=s.next();
if (option.equals("no"))
{
break;
}
}
}
}

Note :-
hasNext() method return true if the Scanner has another token as its input the return type is
boolean.next() is return next complete token from the Scanner

Ex:-
import
java.io.*;
import
java.util.*;class
Test
{
public static void main(String[] args)
{

try{
Scanner s=new Scanner(new BufferedReader(new FileReader("abc.txt")));while (s.hasNext())
{
System.out.println(s.next());
}
}
catch(Exception e)
{
System.out.println(e); System.out.println("getting Exception");
}
}
}

You might also like