Program showing creating, reading and writing files in Java
Java contains a package named Java.io is having multiple classes that are used to perform input output operation of Files.
Stream- a Stream is nothing but a sequence of data.There are two Streams in Java.
Inputstream - to data from Files.
Outputstreams- to write data in Files. Basically there are 3 streams in Java.
Byte Stream- performs I/O of 8 bit bytes.
Character Stream- used to perform input output on 16 bit Unicode characters.
Standard Stream
Byte Stream
The following program shows how to read and write bytes using two classes. FileInputStream and FileOutputStream.
package FilePackage;
import java.io.IOException;
import java.io.*;
public class FileClass {
public static void main(String[] args {
FileInputStream fin=null;
FileOutputStream fout=null;
try
{
fin=new FileInputStream("D:/input.txt");
fout=new FileOutputStream("D:/output.txt");
int n;
while((n=fin.read())!= -1)
{
fout.write(n);
}
}
catch(IOException ex)
{
System.out.println("Exception occured " + ex.toString());
}
finally
{
if(fin!=null)
{
fin.close();
}
if(fout!=null)
{
fout.close();
}
}
import java.io.IOException;
import java.io.*;
public class FileClass {
public static void main(String[] args {
FileInputStream fin=null;
FileOutputStream fout=null;
try
{
fin=new FileInputStream("D:/input.txt");
fout=new FileOutputStream("D:/output.txt");
int n;
while((n=fin.read())!= -1)
{
fout.write(n);
}
}
catch(IOException ex)
{
System.out.println("Exception occured " + ex.toString());
}
finally
{
if(fin!=null)
{
fin.close();
}
if(fout!=null)
{
fout.close();
}
}
Explaination
Character Stream
The following program shows how to read and write characters using two classes. FileReader and FileWriter.
package FilePackage;
import java.io.IOException;
import java.io.*;
public class FileClass {
public static void main(String[] args {
FileReader fin=null;
FileWriter fout=null;
try
{
fr=new FileReader("D:/input.txt");
fw=new FileWriter("D:/output.txt");
int n;
while((n=fr.read())!= -1)
{
fw.write(n);
}
}
catch(IOException ex)
{
System.out.println("Exception occured " + ex.toString());
}
finally
{
if(fr!=null)
{
fr.close();
}
if(fw!=null)
{
fw.close();
}
}
import java.io.IOException;
import java.io.*;
public class FileClass {
public static void main(String[] args {
FileReader fin=null;
FileWriter fout=null;
try
{
fr=new FileReader("D:/input.txt");
fw=new FileWriter("D:/output.txt");
int n;
while((n=fr.read())!= -1)
{
fw.write(n);
}
}
catch(IOException ex)
{
System.out.println("Exception occured " + ex.toString());
}
finally
{
if(fr!=null)
{
fr.close();
}
if(fw!=null)
{
fw.close();
}
}
Explaination
Standard Streams
Like other programming languages , Java also provides mechanism for Standard input , standard output and standard Error.
Standard Input -Providing data to users program via standard input device i.e Keyboard.
Standard output- Producing output data provided by input to the output device i.e monitor.
Standard Error- Outout the Error data to the computer screen using the standard error stream.
Below Program demonstrate the use of Standard Input.
import java.io.*;
public class ReadStream {
public static void main(String args[]) {
InputStreamReader cin = null;
try {
cin = new InputStreamReader(System.in);
System.out.println("Enter the characters, press 'q' to quit.");
char ch;
do {
ch = (char) cin.read();
System.out.print(ch);
} while(ch != 'q');
catch(IOException ex) { System.out.println("Exception occurred" + ex.toString());
}
}finally {
if (cin != null) {
cin.close();
}
}
}
}
public class ReadStream {
public static void main(String args[]) {
InputStreamReader cin = null;
try {
cin = new InputStreamReader(System.in);
System.out.println("Enter the characters, press 'q' to quit.");
char ch;
do {
ch = (char) cin.read();
System.out.print(ch);
} while(ch != 'q');
catch(IOException ex) { System.out.println("Exception occurred" + ex.toString());
}
}finally {
if (cin != null) {
cin.close();
}
}
}
}
Please write your comment below
Program showing creating, reading and writing files in Java
Reviewed by LanguageExpert
on
July 08, 2018
Rating:
No comments