Java handling text files




















By Priya Pedamkar. File handling refers to working with the file in java. The FIle is a container that can contain different types of information. The file can contain text, images, videos, tables, etc. In Java, the File class enables us to work with different types of files. File class is a member of the java. In the below-given syntax, package java. In Java, File handling takes place by streaming concepts. Stream refers to a sequence of data.

This example shows how different methods are used in the program to get the specified details. In the above code, we import the java. File package and create a class FileInfo. In the main method, we create an object of the text file which we have created in our previous example. We check the existence of the file using a conditional statement, and if it is present, we get the following information about that file:.

The next operation which we can perform on a file is "writing into a file". In order to write data into a file, we will use the FileWriter class and its write method together.

We need to close the stream using the close method to retrieve the allocated resources. FileWriter and java. IOException classes. We create a class WriteToFile, and in its main method, we use the try-catch block. In the try section, we create an instance of the FileWriter class, i. We call the write method of the FileWriter class and pass the content to that function which we want to write. After that, we call the close method of the FileWriter class to close the file stream.

After writing the content and closing the stream, we print a custom message. If we get any error in the try section, it jumps to the catch block. In the catch block, we handle the IOException and print a custom message.

The next operation which we can perform on a file is "read from a file". In order to write data into a file, we will use the Scanner class. Here, we need to close the stream using the close method. We will create an instance of the Scanner class and use the hasNextLine method nextLine method to get data from the file. In the above code, we import the "java.

Scannner", "java. File" and "java. IOException" classes. We create a class ReadFromFile , and in its main method, we use the try-catch block. In the try section, we create an instance of both the Scanner and the File classes. We pass the File class object to the Scanner class object and then iterate the scanner class object using the "While" loop and print each line of the file.

We also need to close the scanner class object, so we use the close function. The next operation which we can perform on a file is "deleting a file". In order to delete a file, we will use the delete method of the file. We don't need to close the stream using the close method because for deleting a file, we neither use the FileWriter class nor the Scanner class.

In the above code, we import the File class and create a class DeleteFile. In the main method of the class, we create f0 object of the file which we want to delete. In the if statement, we call the delete method of the file using the f0 object.

If the delete method returns true, we print the success custom message. FileWriter FileWriter is useful to create a file writing characters into it. This class inherits from the OutputStream class. The constructors of this class assume that the default character encoding and the default byte-buffer size are acceptable. FileWriter is meant for writing streams of characters. For writing streams of raw bytes, consider using a FileOutputStream. FileWriter creates the output file , if it is not present already.

FileWriter String fileName, Boolean append — Constructs a FileWriter object given a file name with a Boolean indicating whether or not to append the data written. Skip to content. Change Language. Related Articles.



0コメント

  • 1000 / 1000