- File class tells the size of the file
- When it was created
- File filename = new File();
Communication between computer and outside world.
Different types of IO:
1) Console I/O
2) Keyboard I/O
Java performs I/O through streams.
- It is a continuous flow of data.
Two kinds of stream:
- Input Stream – It is used to read data from a source.
- Output Stream – It is used for writing a data to a destination.
System.out : Standard Output
System.in : Standard Input
System.err : Standard error Stream
Example:
package JDBC;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class FileDemo {
public static void main(String args[]) throws IOException {
Scanner reader = new Scanner(System.in);
boolean success = false;
System.out.println(“Enter path of directory to create”);
String dir = reader.nextLine();
File directory = new File(dir);
if (directory.exists()) {
System.out.println(“Directory already exists …”);
} else {
System.out.println(“Directory not exists, creating now”);
success = directory.mkdir();
if (success) {
System.out.printf(“Successfully created new directory : %s%n”, dir);
} else {
System.out.printf(“Failed to create new directory: %s%n”, dir);
}
}
System.out.println(“Enter file name to be created “);
String filename = reader.nextLine();
File f = new File(directory,filename); //Command to create a new file
//success =
if (f.exists()) {
System.out.println(“File already exists”);
} else {
System.out.println(“No such file exists, creating now”);
success = f.createNewFile();
if (success) {
System.out.printf(“Successfully created new file: %s%n”, f);
} else {
System.out.printf(“Failed to create new file: %s%n”, f);
}
}
// close Scanner to prevent resource leak
reader.close();
}
}
It act as a temporary memory (block of main memory used as a work area).
Processing stream :
It operates on the data supplied by another stream.
Type of Stream:
- Byte Stream: Handling input and output of byte
- Perform I/O of 8 Bytes.
- Character Stream: Handling input and output of characters.
- UNICODE character
- Reader and Writer classes found in java.io package
- Creates BufferedReader and BufferedWriter.
- Input Stream and Output Stream are Byte-Oriented Streams
- Reader and Writer are Character-Oriented Streams
- All these are Abstract Class
- Reader : File Reader —à Buffered Reader
- Writer : File Writer —–àBuffered Writer
- Buffered Reader and Buffered Writer are used to improve the performance.
It extends InputStream and OutputStream class.
- InputStream class: FileInput stream
ByteArrayInput stream
FilterInput Stream
ObjectInput Stream
- OutputStream class: FileOutput Stream
ByteArrayOutput Stream
FilterOutput Stream
ObjectOutput Stream
Example:
- Reader (Character Oriented Stream)
package JDBC;
import java.io.Reader;
import java.io.FileReader;
public class ReaderExample {
public static void main(String[] args) {
// TODO Auto-generated method stub
try
{
Reader reader=new FileReader(“D:\\data.txt”);
int data=reader.read();
while(data!=-1)
{
System.out.print((char)data);
data=reader.read();
}
reader.close();
}
catch(Exception ex)
{
System.out.println(ex.getMessage());
}
}
}
- Buffered Reader
import java.io.FileReader;
import java.io.File;
public class BufferedReaderExample {
public static void main(String[] args) {
// TODO Auto-generated method stub
try
{
BufferedReader br=new BufferedReader(new FileReader(new File(“D:\\Temp\\countries.txt”)));
String line=””;
while((line=br.readLine())!=null)
{
System.out.println(line);
}
br.close();
}
catch(FileNotFoundException e)
{
System.out.println(“File not exists or insufficient rights”);
e.printStackTrace();
}
catch(IOException e)
{
System.out.println(“An exception occured while reading the file”);
e.printStackTrace();
}
}
}
- Writer(Character Oriented Stream)
package JDBC;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
public class WriterExample {
public static void main(String[] args) {
// TODO Auto-generated method stub
try
{
Writer w=new FileWriter(“D:\\data.txt”);
String content=”She Sells Sea Shells In the Sea”;
w.write(content);
w.close();
System.out.println(“Data Written”);
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
- Buffered Writer
package JDBC;
import java.io.FileWriter;
import java.io.IOException;
import java.io.BufferedWriter;
public class WriteToFile {
public static void main(String[] args) {
// TODO Auto-generated method stub
//Writing to a file using BufferedWriter in Java
try
{
FileWriter writer=new FileWriter(“D:\\names.txt”,true); //Append true and false
BufferedWriter bwr=new BufferedWriter(writer); //Improve Reading and Writing fast(Improve performance and Efficiency
bwr.write(“James”);
bwr.newLine();//.write(‘\n’);
bwr.write(“Hobert”);
bwr.close();
System.out.println(“Successfully written to a file”);
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}
}
- Input Stream (Byte Oriented Stream)
package JDBC;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class InputStreamToFileCopy {
public static void main(String[] args) {
// TODO Auto-generated method stub
try
{
InputStream is=new FileInputStream(“D:\\Temp\\source.txt”);
FileOutputStream os= new FileOutputStream(“D:\\Temp\\new_source.txt”);
byte[] buffer=new byte[1024];
int bytesRead;
//read from is to buffer
while((bytesRead=is.read(buffer))!=-1)
{
os.write(buffer,0,bytesRead);
}
System.out.println(“File Copied”);
is.close();
//flush OutputStream to write any buffered data to file
os.flush();
os.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}