0% found this document useful (0 votes)
6 views4 pages

java9

Uploaded by

sharadongare25
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

java9

Uploaded by

sharadongare25
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Practical no 09

Write code and understand file handling in java


1.Write To file
package File_Operations;
// Importing the FileWriter class
import java.io.FileWriter;

// Importing the IOException class for handling errors


import java.io.IOException;

public class WriteToFile {

public static void main(String[] args) {


try {
// Creating a FileWriter object
FileWriter fwrite = new
FileWriter("C:\\Users\\ankit\\OneDrive\\Desktop\\FileOperationExample.txt"); // Corrected the path
// Writing the content into the FileOperationExample.txt file
fwrite.write("Hello T.Y. Class. This is the very first line written into the file.");

// Closing the stream


fwrite.close();
System.out.println("Content is successfully written to the file.");
} catch (IOException e) {
System.out.println("Unexpected error occurred");
e.printStackTrace();
}
}
}

Output:-
2.Create To File

package File_Operations;

// Importing the File class


import java.io.File;

// Importing the IOException class for handling errors


import java.io.IOException;

public class Create_File {

public static void main(String[] args) {


try {
// Creating an object of a file
File f0 = new File("C:\\Users\\ankit\\OneDrive\\Desktop\\FileOperationExample.txt"); // Use a
valid path
if (f0.createNewFile()) {
System.out.println("File " + f0.getName() + " is created successfully.");
} else {
System.out.println("File already exists in the directory.");
}
} catch (IOException exception) {
System.out.println("An unexpected error occurred.");
exception.printStackTrace();
}
}
}

Output:-
3.Read To File
package File_Operations;
import java.io.File;

// Importing FileNotFoundException class for handling errors


import java.io.FileNotFoundException;

// Importing the Scanner class for reading text files


import java.util.Scanner;

public class ReadFromFile {

public static void main(String[] args) {


try {
// Create f1 object of the file to read data
File f1 = new File("C:\\Users\\ankit\\OneDrive\\Desktop\\FileOperationExample.txt"); // Update
path
Scanner dataReader = new Scanner(f1);
while (dataReader.hasNextLine()) {
String fileData = dataReader.nextLine();
System.out.println(fileData);
}
dataReader.close();
} catch (FileNotFoundException exception) {
System.out.println("Unexpected error occurred!");
exception.printStackTrace();
}
}
}

Output:-
4.Delete File
package File_Operations;

import java.io.File;

public class DeleteFile {

public static void main(String[] args) {


File f0 = new File("C:\\Users\\ankit\\OneDrive\\Desktop\\FileOperationExample.txt"); // Use
double backslashes
if (f0.delete()) {
System.out.println(f0.getName() + " file is deleted successfully.");
} else {
System.out.println("Unexpected error found in deletion of the file.");
}
}
}

Output:-

You might also like