pr32 Java 4th Sem IT
pr32 Java 4th Sem IT
Practical:-32
Aim:- Develop programs to create, write, modify, read operations on Text files.
4341602 OOP With Java 226370316016
Output:-
4341602 OOP With Java 226370316016
Practical:-32
Aim:- Develop programs to create, write, modify, read operations on Text files.
~create file
import java.io.File;
import java.io.IOException;
class create
{
public static void main(String args[])
{
try {
File f0 = new File("creatingfile.txt");
if (f0.createNewFile()) {
System.out.println("File " + f0.getName() + " is created successfully.");
} else {
System.out.println("File is already exist in the directory.");
}
} catch (IOException exception) {
System.out.println("An unexpected error is occurred.");
exception.printStackTrace();
}
}
}
4341602 OOP With Java 226370316016
Output:-
4341602 OOP With Java 226370316016
~write file
import java.io.FileWriter;
import java.io.IOException;
class write
{
public static void main(String args[])
{
try {
FileWriter fwrite = new FileWriter("creatingfile.txt");
Output:-
4341602 OOP With Java 226370316016
~read file
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
class Read
{
public static void main(String args[])
{
try {
File f1 = new File("creatingfile.txt");
Scanner dataReader = new Scanner(f1);
while (dataReader.hasNextLine()) {
String fileData = dataReader.nextLine();
System.out.println(fileData);
}
dataReader.close();
}catch (FileNotFoundException exception) {
System.out.println("Unexcpected error occurred!");
exception.printStackTrace();
}
}
}