File Operations in Java

File Operations in Java
File operations plays an important role in java, Reading a file, renaming, deleting etc. In this article we are going to see some file operations,

  • Creating a file
  • Writing a content into that file
  • Reading the content from the file

Before we proceed to the coding, it is important for us to get familiar regarding some Input/Output methods

File – Used to create an object for file
FileReader – Used to read characters
BufferedReader – Enhances the filereader by reading a more data and storing it in buffer
FileWriter – Write characters to a file
BufferedWriter – Enhances filewriter by writing more data
PrintWriter – used for enhancement and has other methods than bufferedwriter

NOTE:
Please note that,

File file = new File(“notes.txt”); –> This does not create a new File, This just creates an object

Output:
op1

Read More

Search and Replace Content inside a File

Search and Replace Content inside a File
Today, We are going to see how to search a file for specific content and replace the content and write to a new File.

Let us create a text document with some content in it

screen1

Now we are going to write a code to change “File 1” into “File 2”, The content will be

“Sample Content in File 2”

Once the code is executed, File2.txt will get created and content will be,

screen2

 

Read More