- File handling in Java refers to the process of working or performing different types of operations with files and directories using Java programming language.
- File handling operations allow for the creation, reading, writing, deleting, modifying, renaming, copying, and performing other file-related various operations on files and directories.
- To perform file-handling operations in Java, we need to use the
java.io
package, which provides several classes and methods for the file-handling process. - The Basic file-handling activities in Java are:-
-
-
Basic File Operations:
- Creating a File: We can create a new file with the help of the
File
class and thecreateNewFile()
method. - Checking File Existence: The
exists()
method checks if a file or directory exists. - Deleting a File: The
delete()
method deletes a file or directory. - Renaming a File: The
renameTo()
method renames a file or directory. - Getting File Information: The
length()
method returns the size of a file,isFile()
method checks if it’s a file, andisDirectory()
checks if it’s a directory.
- Creating a File: We can create a new file with the help of the
-
File Input and Output Operations:
-
-
-
- File input and output (I/O) operations in Java are crucial for reading from and writing to files.
- They are commonly used for tasks such as data persistence, file manipulation, and handling large datasets.
- Reading from a File:
- We can read/fetch data from a file using various classes such as
FileInputStream
,BufferedReader
, orScanner
. - Here, the FileInputStream class contains read() & skip(), the BufferedReader class contains readLine(), and the Scanner class contains next(), nextLine(), nextInt(), etc.
- The
FileInputStream
class in Java is used for reading binary data from a file. It is a subclass of theInputStream
class and provides methods for reading bytes from a file input stream. Theread()
method of theFileInputStream
class reads the next byte of data from the input stream. It returns anint
value representing the byte read, or-1
if the end of the file has been reached. Methods ofFileInputStream
throwIOException
hence its handling is required. It’s important to close the FileInputStream object once we’re done reading from the file. This releases the file handle and any system resources associated with it.
- We can read/fetch data from a file using various classes such as
- Writing to a File:
- We can write/store data to a Java file using classes like
FileOutputStream
,BufferedWriter
, orPrintWriter
. - Here, the FileOutputStream class contains write() & flush(), the BufferedWriter class contains write() & newLine(), and the PrintWriter class contains print(), println(), etc.
- In Java, the
FileOutputStream
class is used for writing binary data to a file. It’s a subclass of theOutputStream
class and provides methods to write bytes to a file output stream. It’s typically used for writing binary data to files, such as images, videos, or any other non-textual data. Thewrite(int b)
method of theFileOutputStream
class writes the specified byte to the output stream. Theflush()
method flushes the stream, ensuring that any buffered output bytes are written to the file. As with any resource, it’s important to close theFileOutputStream
object once we’re done using it to release system resources. Methods ofFileOutputStream
can throwIOException
if an I/O error occurs, such as the file not being found or the stream being closed. - The
PrintWriter
class in Java provides methods to write formatted data to a file. It’s particularly useful when a user needs to write data in a human-readable format or when they want to easily concatenate text and data. It is an extension of theWriter
class and offers several convenient methods for writing various data types as text representations. ThePrintWriter
is the class that provides variousprint(),
andprintln()
methods to write data of different types to the file.void print(String s)
writes a string to the file without appending a newline character.void println(String s)
writes a string to the file and appends a newline character. Similar methods exist for other data types likeint
,double
,boolean
, etc. Theflush()
method of the PrintWriter class flushes the stream, ensuring that any buffered output data is written to the file. As with any resource, it’s important to close thePrintWriter
object once we’re done using it to release system resources. The methods ofPrintWriter
do not throwIOException
.
- We can write/store data to a Java file using classes like
- Reading from a File:
-
File and Directory Navigation Operations:
- Listing Files in a Directory: The
list()
method returns an array of files and directories present in an existing directory. - Navigating Directories: The
File
class provides methods likegetParent()
to get the parent directory, andlistFiles()
to retrieve all files present in a directory.
- Listing Files in a Directory: The
-
0 Comments