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

Sample Program File Handling

The document provides code examples for handling files in Java including checking if a file exists, creating a new file, writing to a file, reading from a file, and deleting a file.

Uploaded by

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

Sample Program File Handling

The document provides code examples for handling files in Java including checking if a file exists, creating a new file, writing to a file, reading from a file, and deleting a file.

Uploaded by

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

SAMPLE PROGRAMS: FILE HANDLING

FILE_CLASS

//Submitted by:
//

import java.io.File;

public class App {


public static void main(String[] args) throws Exception {

try {
// Create object from File class
File myFile = new File("C:\\Users\\ELSIE\\Documents\\sample.txt");

// .exists() method checks if a file exists in the pathname


if (myFile.exists()) {
System.out.println(myFile.getName() + "exists!");
} else {
System.out.println(myFile.getName() + " does not exist!");
}
} catch (Exception e) {
System.out.println("There is an error");
e.printStackTrace();
}

}
}

CREATE FILE

import java.io.File;

public class App {


public static void main(String[] args) throws Exception {

try {
// Create object from File class
File myFile = new File("C:\\Users\\ELSIE\\Documents\\sample.txt");

// .createNewFile() method creates a new file if the file in the pathname does
// not exist
if (myFile.createNewFile()) {
System.out.println(myFile.getName() + " created!");
} else {
System.out.println(myFile.getName() + " already exists!");
}
} catch (Exception e) {
System.out.println("There is an error");
e.printStackTrace();
}
}
}

WRITE FILE

import java.io.FileWriter;
import java.io.IOException;

public class App {


public static void main(String[] args) throws Exception {

try {
// Create object from FileWriter class
FileWriter myWriter = new FileWriter("C:\\Users\\ELSIE\\Documents\\sample.txt");

// .write() methods adds content to the file


myWriter.write("Hello i am now taking intermediate programming in Java!");

// Close FileWriter
myWriter.close();

System.out.println("File Write successful!");


} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}

READ FILE

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class App {


public static void main(String[] args) throws Exception {

try {
// Create an object from the File class
File myFile = new File("C:\\Users\\ELSIE\\Documents\\sample.txt");

// Create an object from the Scanner class


Scanner scan = new Scanner(myFile);

// Read the content of the file


while (scan.hasNextLine()) {
String data = scan.nextLine();
System.out.println(data);
}
// Close scanner
scan.close();
} catch (FileNotFoundException e) {
System.out.println("An error occurred");
e.printStackTrace();
}
}
}

DELETE FILE

import java.io.File;

public class App {


public static void main(String[] args) throws Exception {

try {
// Create object from File class
File myFile = new File("C:\\Users\\ELSIE\\Documents\\sample.txt");

// .delete() method removes a file if the file exists in the pathname


if (myFile.delete()) {
System.out.println(myFile.getName() + " deleted successfully!");
} else {
System.out.println("Failed to delete " + myFile.getName());
}
} catch (Exception e) {
System.out.println("There is an error");
e.printStackTrace();
}

}
}

You might also like