Sitemap
Cahit Barkin Ozer

Cahit Barkin Ozer

I learn about innovations in technology, especially generative AI, and share them with you. My YouTube Channel: https://www.youtube.com/@cbarkinozer

Java File Operations

3 min readNov 15, 2021

--

File operations in Java.

A File is an abstract data type in Java. Creating, writing, reading, deleting, and retrieving information are all File Operations. [1]

Hierarchy of I/O Classes in Java[2]

For Access: https://cbarkinozer.medium.com/java-file-operations-df292593a756?source=friends_link&sk=8a9fd25e81b083df5ad84a115b6ec029

To understand file operations we need to understand streams. [1]

Stream

A stream refers to a series of data. In Java, Streams in Java are divided into two types: Byte Stream and Character Stream.[1]

Byte Stream deals mostly with byte data. A file handling process with a byte stream is one in which the byte data is used to execute an input. Java byte streams are utilized to perform 8-bit byte input and output. For secure processes, this is the best option.[1][2]

Character Stream’s major focus is character data. A file handling procedure with a character stream is one in which an input is given and the character data is executed. Java Character streams are used to perform input and output for 16-bit Unicode [1][2]

Java File Class Methods: [1]

Boolean canRead(): Can we read the file’s data?

Boolean createNewFile(): Creates a new empty file.
Boolean canWrite(): Can we write the data into the file?

Boolean exists(): Is the specified file present?

Boolean delete(): Deletes a file.

String getName(): Finds the file name.

Boolean getAbsolutePath() : Gets the absolute pathname of the file.

Long length(): Gets the size of the file in bytes.

String[] list(): Array of the files available in the directory.

Get Cahit Barkin Ozer’s stories in your inbox

Join Medium for free to get updates from this writer.

Boolean mkdir() : Creats a new directory.

Example to file operations[1]

Note: This code shows overall how and where to use file methods. Please do not use this much code in a single Try block.

import java.io.File;// File classimport java.io.IOException; // IOException for errorsclass CreateFile {public static void main(String args[]) {try {// Creating an object of a fileFile f0 = new File(“D:FileOperationExample.txt”);if (f0.createNewFile()) {
System.out.println(f0.getName());
// Getting path of the file
System.out.println(f0.getAbsolutePath()); // Checking whether the file is writable or not
System.out.println(f0.canWrite());
// Checking whether the file is readable or not
System.out.println(f0.canRead());
// Getting the length of the file in bytes
System.out.println(f0.length());
}
// Writing the content into the FileOperationExample.txt filefwrite.write("A named location used to store related information is referred to as a File.");
Scanner dataReader = new Scanner(f1);
while (dataReader.hasNextLine()) {String fileData = dataReader.nextLine();System.out.println(fileData);}fwrite.close(); // Closing the stream//Using delete() in if() as depicted, shows the deleted file's nameif (f0.delete()) { System.out.println(f0.getName()); }String dirname = "/tmp/user/java/bin";
File d = new File(dirname);
d.mkdirs();// Create directory now.
file = new File("/tmp");
// List downs all the files and directories available in a directory as array of files
paths = file.list();

} else {
System.out.println(“File is already exist in the directory.”);}} catch (IOException exception) {System.out.println(“An error is occurred.”,exception);exception.printStackTrace();
}}}

Please do not forget to clap if you enjoyed my article. Thank you.

Source:

[1]javatpoint,(2021), File Operaitons in Java:

[https://www.javatpoint.com/file-operations-in-java]

[2]tutorialspoint,(2021), Java Files IO:

[https://www.tutorialspoint.com/java/java_files_io.htm]

--

--

I learn about innovations in technology, especially generative AI, and share them with you. My YouTube Channel: https://www.youtube.com/@cbarkinozer