Wednesday, November 28, 2012

How to read and write content from/to a text file using Selenium WebDriver

There are many ways you can do this.If you wanted to read the content line by line level you can BufferReader/BufferWriter and the below script will helps you.


import java.io.*;

public class FileHandling
{
    public static void main(String[] args)
    {
        File o_File1=new File("C:\\Input1.txt");
        File o_File2=new File("C:\\Input2.txt");
        String line;
        try
        {
            BufferedReader o_br1=new BufferedReader(new FileReader(o_File1));
            BufferedWriter o_bw1=null;
            if(!o_File2.exists())
            {
                o_File2.createNewFile();
            }
            o_bw1 = new  BufferedWriter(new FileWriter(o_File2) );
            while((line=o_br1.readLine())!=null)
            {
                    o_bw1.write(line);
            }
            o_bw1.close();
            o_br1.close();
        } catch (Exception e)
        {
            e.printStackTrace();
        }
       
    }

}