Showing posts with label BufferedWriter. Show all posts
Showing posts with label BufferedWriter. Show all posts

Monday, 9 October 2017

Reading a file as String

Sometimes we need to read test input from a file(.txt/.xml etc) and enter into a text area field. Below code will help to read the content of file and stored in a string.

To achieve this we need to take the help of Apache CommonsIO. This will be available at Apache CommonsIO and download  zip file

Extract the zip file and you will find commons-io-2.5.jar file

Add the jar file to the project 

import java.io.File;
import java.nio.charset.Charset;
import org.apache.commons.io.FileUtils;

public class ReadFileToString {
public static void main(String[] args){
File inputFile = new File("d://test.xml");
//location of the tile
try {
               //Reading file to string
String str = FileUtils.readFileToString(inputFile,Charset.forName("utf-8"));
   System.out.println(str);
} catch (Exception e) {
    e.printStackTrace();
}
}
}

With the help of above code, we can have the content of file. Now we can use the variable to enter the text in text area input field.

In this case, 

driver.findElement(By.id("TextArea")).sendKeys(str);

Wednesday, 16 October 2013

Reading Test Data from Text File and Writing in to Text File

Purpose: Reading Test Data from Text File and Writing in to Text File

Some times we may need to read test data from text file. We can achieve this using simple Java snippet. following code will help you to address this

import java.io.*;
public class ReadTextFile {
    public static void main(String[] args){
        String readFile ="D://test.txt";
        String writeFile ="D://test1.txt";
       
        //reading contents from a text file  
        try{
            InputStream ips=new FileInputStream(readFile);
            InputStreamReader ipsr=new InputStreamReader(ips);
            BufferedReader br=new BufferedReader(ipsr);
            String line;
            br.readLine();
           
            String[] n = null;
            while ((line =br.readLine())!=null){
             n = line.split(",");
             for(int i = 0; i < n.length; i++)
             {
              System.out.print(n[i]);
             }
             System.out.println("\n");
            }
            br.close();
        }      
        catch (Exception e){
            System.out.println(e.toString());
        }
        //writing to a text file
        try {
            FileWriter fw = new FileWriter (writeFile);
            BufferedWriter bw = new BufferedWriter (fw);
            PrintWriter fileOut = new PrintWriter (bw);
           
                fileOut.println ("\n1. Writing Content to text file");
                fileOut.println ("\n2. Writing content to text file");
               
            fileOut.close();
        }
        catch (Exception e){
            System.out.println(e.toString());
        }      
    }
}

Inupt file contents(test.txt):


First Name:, User First Name
Last Name:, User Last Name
User Name:, User Name
Password:, Password
Email:, email
First Name:, User First Name
Last Name:, User Last Name
User Name:, User Name1
Password:, Password1
Email:, email1