File in Java
File in Java
File in Java
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.
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:
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.
int c;
while ((c=fr.read())!=-1)
{
fw.write(c);
}
fr.close();
fw.close();
}
catch(IOException io)
{
System.out.println("getting IOException");
}
}
}
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.
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();
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");
}
}
}