File Handling :How we can read the data from file and how we can write data into
file .
All Classes are available in java.io package.
1)File
2)FileReader
3)FileWriter
4)BufferedReader
5)BufferedWriter
6)PrintWriter
Example 1:
package basicsOfFilehandling;
import java.io.File;
import java.io.IOException;
public class FileDemo {
public static void main(String[] args) throws IOException {
File f = new File("C:\\Users\\HP\\OneDrive\\Desktop\\java\\Demo.txt");
boolean newFile = f.createNewFile();
if (newFile) {
System.out.println("File Created Successfully");
}else {
System.out.println("File Not Created Successfully");
}
}
}
Example 2:
package basicsOfFilehandling;
import java.io.File;
import java.io.IOException;
public class FileDemo {
public static void main(String[] args) throws IOException {
File f = new File("C:\\Users\\HP\\OneDrive\\Desktop\\java\\Demo.txt");
System.out.println(f.exists());
}
}
Example 3:
package basicsOfFilehandling;
import java.io.File;
import java.io.IOException;
public class FileDemo {
public static void main(String[] args) throws IOException {
File f = new File("C:\\Users\\HP\\OneDrive\\Desktop\\java\\Demo1.txt");
System.out.println(f.exists());
}
}
Example 4:
package basicsOfFilehandling;
import java.io.File;
import java.io.IOException;
public class FileDemo {
public static void main(String[] args) throws IOException {
File f = new File("C:\\Users\\HP\\OneDrive\\Desktop\\java\\Demo.txt");
System.out.println(f.getAbsolutePath());
}
}
Example 5:
package basicsOfFilehandling;
import java.io.File;
import java.io.IOException;
public class FileDemo {
public static void main(String[] args) throws IOException {
File f = new File("C:\\Users\\HP\\OneDrive\\Desktop\\java\\Demo.txt");
System.out.println(f.canWrite());
System.out.println(f.canRead());
}
}
Example 6:
package basicsOfFilehandling;
import java.io.File;
import java.io.IOException;
public class FileDemo {
public static void main(String[] args) throws IOException {
File f = new File("C:\\Users\\HP\\OneDrive\\Desktop\\java\\Demo.txt");
System.out.println(f.length());
}
}
Example 7:
package basicsOfFilehandling;
import java.io.File;
import java.io.IOException;
public class FileDemo {
public static void main(String[] args) throws IOException {
File f = new File("C:\\Users\\HP\\OneDrive\\Desktop\\java\\Demo.txt");
System.out.println(f.delete());
}
}
3)FileWriter
Example :
package basicsOfFilehandling;
import java.io.FileWriter;
import java.io.IOException;
public class FileWritterDemo {
public static void main(String[] args) throws IOException {
FileWriter fw=new FileWriter("C:\\Users\\HP\\OneDrive\\Desktop\\java\\
Demo.txt");
fw.write("Welcome to BikkadIT \n");
fw.write("I want to become Java Developer \n ");
fw.write("kjfhjdhkjfhskdjh");
fw.close();
}
}
4) BufferedWriter :
package basicsOfFilehandling;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class FileWritterDemo {
public static void main(String[] args) throws IOException {
FileWriter fw = new FileWriter("C:\\Users\\HP\\OneDrive\\Desktop\\
java\\Demo.txt");
BufferedWriter bw = new BufferedWriter(fw);
bw.write("Welcome to BikkadIT");
bw.newLine();
bw.write("I want to become java Developer");
bw.newLine();
bw.write("I am from line");
bw.close();
fw.close();
}
}
5) PrintWritter
package basicsOfFilehandling;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class FileWritterDemo {
public static void main(String[] args) throws IOException {
FileWriter fw = new FileWriter("C:\\Users\\HP\\OneDrive\\Desktop\\
java\\Demo.txt");
PrintWriter pw = new PrintWriter(fw);
pw.println("welcome to BikkadIT");
pw.println("I am from pune");
pw.println("I want to become java Developer");
pw.close();
fw.close();
}
}
6) FileReader:
package basicsOfFilehandling;
import java.io.FileReader;
import java.io.IOException;
public class FileReaderDemo {
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("C:\\Users\\HP\\OneDrive\\Desktop\\
java\\Demo.txt");
int i;
while ((i = fr.read()) != -1) {
System.out.print((char) i);
}
}
}
4)BufferedReader
package basicsOfFilehandling;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class BufferedReaderDemo {
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("C:\\Users\\HP\\OneDrive\\Desktop\\
java\\Demo.txt");
BufferedReader br = new BufferedReader(fr);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
}
}