Java.io package contains the input and output (I/O) in Java.
All these streams represent an input source and an output destination
Uses the concept of steam to make Input/Output Fast.

A stream is a sequence of data that can be of two types viz. Byte Stream and Character Stream.

The byte stream is a stream that incorporates with byte data. When input and output happen with byte data then it is called the file handling process with byte stream.
Likewise for the Character Stream

The InputStream is used to read data from a source and the OutputStream is used for writing data to a destination.

character data
Represented as 1-byte ASCII, 2-bytes Unicode within Java Programs. The first 128 characters of Unicode are the ASCII character.
Read and Written by Reader, Writer and its subclasses.
We can also use a scanner to read characters in JDK1.5 onwards.

binary data

Read by InputStream and its subclasses.
Written by OutputStream and its subclasses.

Java File Methods
canRead()- Checks whether the file is readable or not
canWrite()– Checks whether the file is writable or not
createNewFile()– Method creates an empty file and returns boolean value if created or else false.
delete()- deletes a file and returns true if deleted
getName()- Returns the name of the file
getAbsolutePath-Returns the absolute pathname of the file
length()– Returns the size of the file in bytes
exists()- boolean
lastModified()-long

FileInputStream and FileOutputStream
InputStream f=new InputStream(“file_name.txt”);
The following constructor takes a file name as a string to create an input stream to read the file.

The following constructor takes a file object to create an input stream object to read the file. First, we create a file object using the File() method as follows:
File f = new File(“C:/java/hello”);

Same for OutputStream Just replace Input with Output

Creating a File

import java.io*;
public class Create{
public static void main(String args[])
{
File f=new File("file_name.txt");
if(file.exists==0)
{ f.createNewFile();
  System.out.println("New file has been created");
}
else
{
System.out.println("File already exist");
}
}

Reading a File in Java

import java.io*;
class Read
{ public static void main(String args[])
{
FileInputStream fstream=new FileInputStream("file_name.txt");
//get the object of DataInputStream
DataInputStream in=new DataInputStream(fstream);
BufferedReader br=new BufferedReader(new InputStreamReader(in);
String str;
while ((strLine = br.readLine()) != null) {
  // Print the content on the console
  System.out.println (strLine);
  }
  //Close the input stream
  in.close();
  }catch (Exception e)
  {//Catch exception if any occurs
  System.err.println("Error: " + e.getMessage());
  }
  }
}
//The Java.io.DataInputStream class lets an application read primitive Java data types from an underlying input stream in a machine-independent way.
//usinfg the fileWriter

import java.io*;
public class Write
{
public static void main(String args[])
{
FileWriter fw=new FileWriter("hello.txt");
fw.write("Write anything that you want to write here");
fw.close();
System.out.println("Write Successful");
}
catch(IOException e)
{
System.out.println("An error has occured");
}
}
}

Read From a File

import java.io*;
public class Demo1
{
public static void main(String args[])
{
 File fw=new File("hello.txt");
 Scanner sc=new Scanner(fw); 
 while(sc.hasNextLine())
 {
  String data=fw.nextLine();
  System.out.println(data);
}
fw.close
catch(FileNotFoundException e)
{System.out.println("ERROR OCCURED");

 e.printStackTrace();
}}}

Token A String

public static void main(String args[])
{ String str="This is my World";
StringTokenizer stn=new StringTokenizer(str,",");
String token=NULL;
while(stn.hasMoreElements())
{toke=stn.nextToken();
System.out.println("Token is "+token);
}
//Read char from a file
public static void main(String args[]) throws Exception
{
FileReader fw=new FileReader("file_name.txt");
int ch=fw.read(); 
char chr;
while(ch!=-1)
{
chr=(char)ch;
System.out.println(chr);
ch=fw.read();
}}

Copy A Text File

String src="file_name_1.txt";
String dst="file_name_2.txt";
FileReader fr=new FileReader(src);
FileWriter fw=new FileReader(dst);
int ch=fr.read();
while(ch!=-1)
{ fw.write(ch);
  ch=fr.read();
}
fw.close();
fr.close
System.out.println("Operation performed successfully");