Thursday 22 February 2018

How to write data into Text file in Selenium

Write data to any external source in selenium is very important when we are working with framework.
Data can be write in any format like excel, csv, text etc.


first of all you have to import below packages for apache poi.

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

Now you need to create separate function for write data into text. If file is not there at specific path then code will create first file the write data into it.



public void mWriteDataInText(String vFilePath, String[][] vData, Boolean append)

 {
  try

  {

   File file = new File(vFilePath);
  
   if(!file.exists())
   {

    file.createNewFile();

   }
   
   FileWriter fw = new FileWriter(file.getAbsolutePath(), append);

   BufferedWriter bw = new BufferedWriter(fw);

   bw.newLine();   

   for(int i=0;i<vData.length;i++)
   {

    bw.newLine();

    for(int j=0;j<vData[0].length; j++)

    {     

     bw.write(vData[i][j]);

     bw.write(" : ");     

    }       

   }

   bw.close();   

  }

  catch(Exception E)

  {

   System.out.println("Error in write data into text file.");

  }

 }
Please add comment if you have any question regarding this post.

No comments:

Post a Comment

Popular