0% found this document useful (0 votes)
4 views

pr32 Java 4th Sem IT

Uploaded by

maitrikjoshi2007
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

pr32 Java 4th Sem IT

Uploaded by

maitrikjoshi2007
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

4341602 OOP With Java 226370316016

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");

fwrite.write("A named location used to store related information is referred to as a


File.");
fwrite.close();
System.out.println("Content is successfully wrote to the file.");
}catch (IOException e) {
System.out.println("Unexpected error occurred");
e.printStackTrace();
}
}
}
4341602 OOP With Java 226370316016

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();
}
}
}

You might also like