File :-
File is a collection of records ,if we want to store program output permanently under computer hardisk then we prefer file handling program.
java provide io package to implement File Operation.
File
Text File Binary File
.txt,.doc.xlsx,.pdf .jpg,.png,.mp3,.mp4 etc
FileWriter FileOutputStream
FileReader FileInputStream
BufferedWriter
BufferedReader
To Write Data in File:-
public class FileHandlingProgram {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
float p=12000;
float r=2.2F;
float t=2;
float si= (p*r*t)/100;
File f = new File("E://hello.txt");
FileWriter fw = new FileWriter(f,true);
BufferedWriter bw = new BufferedWriter(fw);
bw.write("result is "+si);
bw.newLine();
System.out.println("Enter Content");
String d = sc.nextLine();
bw.write(d);
bw.close();
}
}
File is a collection of records ,if we want to store program output permanently under computer hardisk then we prefer file handling program.
java provide io package to implement File Operation.
File
Text File Binary File
.txt,.doc.xlsx,.pdf .jpg,.png,.mp3,.mp4 etc
FileWriter FileOutputStream
FileReader FileInputStream
BufferedWriter
BufferedReader
To Write Data in File:-
public class FileHandlingProgram {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
float p=12000;
float r=2.2F;
float t=2;
float si= (p*r*t)/100;
File f = new File("E://hello.txt");
FileWriter fw = new FileWriter(f,true);
BufferedWriter bw = new BufferedWriter(fw);
bw.write("result is "+si);
bw.newLine();
System.out.println("Enter Content");
String d = sc.nextLine();
bw.write(d);
bw.close();
}
}
Read Operation in File:-
public class FileHandlingProgram {
public static void main(String[] args) throws IOException {
File f = new File("E://hello.txt");
FileReader fw = new FileReader(f);
BufferedReader bw = new BufferedReader(fw);
String s="";
String data="";
while((s=bw.readLine())!=null)
{
data+=s;
}
System.out.println(data);
bw.close();
}
}
................................................................................................................................................
File Input/Output Example using Binary file:-
public class FileHandlingProgram {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
File f = new File("E://hello.txt");
FileOutputStream fout = new FileOutputStream(f);
System.out.println("Enter data");
String data = sc.nextLine();
byte arr[] = data.getBytes(); //string to byte conversion
fout.write(arr);
fout.close();
}
}
Program to Read Data From File:-
public class FileHandlingProgram {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
File f = new File("E://hello.txt");
FileInputStream fi = new FileInputStream(f);
String data = " ";
byte arr[] = new byte[1000];
int x;
while((x = fi.read(arr))!=-1)
{
String s = new String(arr);
System.out.print(s);
}
}
}
................................................................................................................................................
File Input/Output Example using Binary file:-
public class FileHandlingProgram {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
File f = new File("E://hello.txt");
FileOutputStream fout = new FileOutputStream(f);
System.out.println("Enter data");
String data = sc.nextLine();
byte arr[] = data.getBytes(); //string to byte conversion
fout.write(arr);
fout.close();
}
}
Program to Read Data From File:-
public class FileHandlingProgram {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
File f = new File("E://hello.txt");
FileInputStream fi = new FileInputStream(f);
String data = " ";
byte arr[] = new byte[1000];
int x;
while((x = fi.read(arr))!=-1)
{
String s = new String(arr);
System.out.print(s);
}
}
}
Post a Comment
POST Answer of Questions and ASK to Doubt