Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Materi File Handling

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Java File Handling

Java File Handling

• File handling in Java implies reading from and writing data


to a file.
• The File class from the java.io package, allows us to work
with different formats of files.
• In order to use the File class, you need to create an object
of the class and specify the filename or directory name.

// Import the File class


import java.io.File

// Specify the filename


File obj = new File("filename.txt");
Java File Methods

Method Type Description


canRead() Boolean It tests whether the file is readable or not
canWrite() Boolean It tests whether the file is writable or not
createNewFile() Boolean This method creates an empty file
delete() Boolean Deletes a file
exists() Boolean It tests whether the file exists
getName() String Returns the name of the file
getAbsolutePath() String Returns the absolute pathname of the file
length() Long Returns the size of the file in bytes
list() String[] Returns an array of the files in the directory
mkdir() Boolean Creates a directory
File Operations in Java

• Create a File
• Get File Information
• Write To a File
• Read from a File
Create a File
// Import the File class
import java.io.File;

// Import the IOException class to handle errors


import java.io.IOException;

public class CreateFile {


public static void main(String[] args) {
try {
// Creating an object of a file
File myObj = new File("c:\\test\\test.txt");
if (myObj.createNewFile()) {
System.out.println("File created: " + myObj.getName());
} else {
System.out.println("File already exists.");
}
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
File created: test.txt
File name: NewFilef1.txt
Get file Information Absolute path: D:FileHandlingNewFilef1.txt
import java.io.File; // Import the File class Writable: true
Readable true
public class FileInformation { File size in bytes 52
public static void main(String[] args) {
// Creating an object of a file
File myObj = new File(“c:\\test\\test.txt");
if (myObj.exists()) {
// Returning the file name
System.out.println("File name: " + myObj.getName());

// Returning the path of the file


System.out.println("Absolute path: " + myObj.getAbsolutePath());

// Displaying whether the file is writable


System.out.println("Writeable: " + myObj.canWrite());

// Displaying whether the file is readable or not


System.out.println("Readable " + myObj.canRead());

// Returning the length of the file in bytes


System.out.println("File size in bytes " + myObj.length());
} else {
System.out.println("The file does not exist.");
}
}
}
Write to a File
// Import the FileWriter class
import java.io.FileWriter;

// Import the IOException class to handle errors


import java.io.IOException;

public class WriteToFile {


public static void main(String[] args) {
try {
FileWriter myWriter = new FileWriter(“C:\\test\\test.txt");
// Writes this content into the specified file
myWriter.write(Java is the prominent programming language of the millenium!");

// Closing is necessary to retrieve the resources allocated


myWriter.close();
System.out.println("Successfully wrote to the file.");
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
Successfully wrote to the file
Read from a File
// Import the File class
import java.io.File;

// Import this class to handle errors


import java.io.FileNotFoundException;

// Import the Scanner class to read text files


import java.util.Scanner;

public class ReadFromFile {


public static void main(String[] args) {
try {
// Creating an object of the file for reading the data
File myObj = new File("D:FileHandlingNewFilef1.txt");
Scanner myReader = new Scanner(myObj);
while (myReader.hasNextLine()) {
String data = myReader.nextLine();
System.out.println(data);
}
myReader.close();
} catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}

Java is the prominent programming language of the millennium!


Task

• Kerjakan slide no 5 - 8
• https://www.edureka.co/blog/file-handling-in-java/#createfi
le

You might also like