{
  "id": 20287501,
  "title": "Writing Data into a File in Java | File Handling",
  "url": "https://urgent.news/2026/11/16/writing-data-into-a-file-in-java-file-handling",
  "topic": "tech",
  "section": "Tech",
  "published": "2026-11-16T14:59:28.000Z",
  "source": {
    "name": "Dev.to",
    "slug": "dev-to",
    "url": "https://dev.to/iamsubash/writing-data-into-a-file-in-java-file-handling-341m"
  },
  "original_language": "en",
  "account": "In this article, we will learn how to write data into a file using Java. To accomplish this task, we utilize three essential classes: File, FileWriter, and BufferedWriter. These classes work together to represent the file, write characters into it, and write data efficiently.\n\nFirst, we create a File object by passing the file name to the File constructor. This object represents the location of the file where we intend to write our data. Next, we instantiate a FileWriter object by passing the File object and a boolean value to its constructor. The boolean value determines whether we want to append the new data to the existing content or overwrite the existing content. If we set the boolean value to true, the new data will be added at the end of the existing content, preserving the original content. If we set it to false or omit the second argument, the existing content will be overwritten.\n\nTo enhance the efficiency of writing data, we introduce the BufferedWriter class. We create an instance of BufferedWriter by passing the FileWriter object to its constructor. This class helps buffer characters before writing them to the file, improving the overall writing process.\n\nOnce our FileWriter and BufferedWriter objects are in place, we can write data into the file using the write() method. This method allows us to write strings directly into the file. In our example, we write two strings: \"ghi\" and \"jkl\". However, it is important to note that the write() method does not automatically move to the next line. If we want each string to appear on a separate line in the file, we can use the newLine() method after writing each string.\n\nFinally, after we have finished writing data to the file, we must close the BufferedWriter to release any resources that were allocated. This step is crucial to ensure that our program runs smoothly and does not leave any lingering memory or file connections open.\n\nIn conclusion, FileWriter and BufferedWriter are the primary classes used in Java to write data into files. FileWriter handles the writing of individual characters, while BufferedWriter optimizes the writing process. By employing these classes, we can effortlessly write or append data to an existing text file.",
  "summary": "In my previous blog, I covered how to read data from a text file using Java. If you haven't read it yet, check it out here: Java File Handling: From Text File to ArrayList In the previous section, we saw how to read employee details from a text file using FileReader and BufferedReader. Now let's see how to write data into a file using Java. To write data into a file, we use three important…",
  "key_points": [],
  "editors_take": null,
  "illustration": null,
  "coverage": {
    "outlets": 1,
    "also_reported_by": []
  },
  "ai_generated": true,
  "disclaimer": "Summaries, key points and the editor’s take are written by software from other outlets’ reporting and may contain errors — always check the linked original."
}