mauricemuteti

ReadAndWriteToATextFileUsingJava.java

May 18th, 2021
755
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * This Tutorial Shows How To Read And Write Data To A Text File.
  3.  * This is achieved by using filewriter and scanner classes to write and read data from files respectively.
  4.  * Just copy this code to your IDE and test it. I have used Netbeans IDE.
  5.  * http://mauricemuteti.info/how-to-write-and-read-from-a-file-in-java-using-filewriter-and-scanner-classes/
  6.  */
  7. package readandwritetoatextfileusingjava;
  8.  
  9. import java.io.File;
  10. import java.io.FileNotFoundException;
  11. import java.io.FileWriter;
  12. import java.io.IOException;
  13. import java.util.Scanner;
  14.  
  15. /**
  16.  *
  17.  * @author Authentic
  18.  */
  19. public class ReadAndWriteToATextFileUsingJava {
  20.  
  21.     /**
  22.      * @param args the command line arguments
  23.      */
  24.     public static void main(String[] args) {
  25.         // TODO code application logic here
  26.  
  27.         //FileName.
  28.         String fileName = "data.txt";
  29.         //Text To Be Written In A Text File.
  30.         String content = "This Data Will Be Written To A Text File";
  31.         //Calling Static Method for Writing Data To A Text File.
  32.         writeToATextFile(fileName, content);
  33.         //Reading data from a text file.
  34.         readFromATextFile(fileName);
  35.     }
  36.  
  37.     /**
  38.      * Method for Writing Data To A Text File.
  39.      *
  40.      * @param fileName
  41.      * @param Content
  42.      */
  43.     public static void writeToATextFile(String fileName, String Content) {
  44.  
  45.         //Creating/Declaring File Writer Object.
  46.         FileWriter fileWriter = null;
  47.  
  48.         try {
  49.             //Inside Try Catch Block.
  50.             System.out.println("Inside Try Catch Block");
  51.             //Initializing File Writer Object.
  52.             fileWriter = new FileWriter(new File(fileName));
  53.             //Writing to a text file.
  54.             fileWriter.write(Content);
  55.             //Printing On The Console.
  56.             System.out.println("Data Written successfully");
  57.  
  58.         } catch (IOException ex) {
  59.  
  60.             //Catching Exceptions
  61.             System.out.println("Error : " + ex.getMessage());
  62.         } finally {
  63.  
  64.             //Finally Execute this code
  65.             if (fileWriter != null) {
  66.                 try {
  67.                     //Closing File Writer.
  68.                     System.out.println("Closing File Writer.");
  69.                     //Releasing Resources By Closing Objects.
  70.                     fileWriter.close();
  71.                 } catch (IOException ex) {
  72.                     //Catching Exceptions
  73.                     System.out.println("Error : " + ex.getMessage());
  74.  
  75.                 }
  76.             }
  77.         }
  78.     }
  79.  
  80.     /**
  81.      * Method for Reading Data From A Text File
  82.      *
  83.      * @param fileName
  84.      */
  85.     public static void readFromATextFile(String fileName) {
  86.  
  87.         //Creating Scanner Object.
  88.         Scanner scannerObject = null;
  89.         try {
  90.  
  91.             scannerObject = new Scanner(new File(fileName));
  92.             System.out.println("\nReading Data From A Text File");
  93.             //While loop - Looping throught text file lines to get all data.
  94.             while (scannerObject.hasNext()) {
  95.                 //Store data from a text file in a variable
  96.                 String result = scannerObject.nextLine();
  97.                 //Print data on the console.
  98.                 System.out.println(result);
  99.             }
  100.         } catch (FileNotFoundException ex) {
  101.             //Catching Exceptions
  102.             System.out.println("Error : " + ex.getMessage());
  103.         } finally {
  104.             if (scannerObject != null) {
  105.                 System.out.println("Closing Scanner.");
  106.                 //Releasing Resources By Closing Objects
  107.                 //Closing Scanner Object.
  108.                 scannerObject.close();
  109.             }
  110.         }
  111.  
  112.     }
  113.  
  114. }
  115.  
RAW Paste Data