Saturday, August 15, 2015

Writing CSV File Using JAVA

We know how to read CSV file without using any API or external.JAR. Now we try to create CSV file with out using any API or external.JAR. Here we create CSV file using pure JAVA classes, for this we need to follow this steps.

Step 1:
  Create OutputStream object for file creation.
OutputStream fileStream = new BufferedOutputStream(new FileOutputStream(filePath) 

Step 2: 
  We need to OutputStream to Writer object to get support for non english characterset .
Writer outStreamWriter = new OutputStreamWriter(fileStream,StandardCharsets.UTF_8);

Step 3: 
  We create a BufferedWriter object to get a better performance. 
BufferedWriter buffWriter = new BufferedWriter(outStreamWriter))

Step 4 :
   Now we write data using append("Data") method and then flush the buffer and close it.

            buffWriter.append("Data1");
            buffWriter.append(separator);
            buffWriter.append("Data2");
            buffWriter.newLine();
            buffWriter.flush();


Example Code : 
 
package csvfilehandling;

import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;


public class CSVWritingWithoutAPI {

    public static void main(String[] args) throws IOException{
        writeCSV("Our File Path", ",");
    }
    public static void writeCSV(String filePath, String separator) throws IOException {

        /**
         * We use try with resource so we don't need to call close
         * method or write finally block explicitly it will be called automatically. 
         */

        try (OutputStream fileStream = new BufferedOutputStream(new FileOutputStream(filePath));
                Writer outStreamWriter = new OutputStreamWriter(fileStream, StandardCharsets.UTF_8);
                BufferedWriter buffWriter = new BufferedWriter(outStreamWriter)) {
            buffWriter.append("Data1");
            buffWriter.append(separator);
            buffWriter.append("Data2");
            buffWriter.newLine();
            buffWriter.flush();
            buffWriter.append("1");
            buffWriter.append(separator);
            buffWriter.append("2");
            buffWriter.newLine();
            buffWriter.flush();
            buffWriter.append("11");
            buffWriter.append(separator);
            buffWriter.append("22");
            buffWriter.flush();
        }
    }
}

Sunday, August 9, 2015

Reading CSV File Using JAVA

Sometime we need to parse CSV(Comma Separated Values) file for data reading or writing .We use CSV file due to platform independence .There are lots of API available to parse a CSV file but here we use simple JAVA code, later we will learn how to use JAVA API for reading and writing a CSV file.

STEP 1 :
 Basically we need this lines to read a file.
try (InputStream fileStream = new BufferedInputStream(new FileInputStream(filePath));

STEP 2 :
 To read a file with different character set except English then we need to support for UTF-8 characters.So we use Reader class for that.
     Reader ir = new InputStreamReader(fileStream, "UTF-8");

STEP 3 :
  Now we create a BufferedReader object to read a file in a speedy way.
     BufferedReader buffReader = new BufferedReader(ir));

STEP 4 :
  We use readLine() method on BufferedReader object and loop will iterate until this object will be null.
              while ((line = buffReader.readLine()) != null) {
                csvData = line.split(separator);
                list.add(csvData);
            }  



 Example Code :

package csvfilehandling;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CSVReadingWithOutAPI {

    public static void main(String[] args) {
        try {
            //System.out.println((getCSVInList("
Our CSV File Path", ",").get(0))[1]);
            for (String[] csvDataRow : getCSVInList("Our CSV File Path", ",")) {
                for (String csvDataOnColumn : csvDataRow) {
                    System.out.println(csvDataOnColumn);
                }
            }
            //System.out.println(getCSVInMap("
Our CSV File Path", ",").get(0).get(1));
            for (Map.Entry<Integer, List<String>> csvDataSet : getCSVInMap("
Our CSV File Path", ",").entrySet()) {
                for (String csvData: csvDataSet.getValue()) {
                    System.out.println(csvData);
                }
            }
            //System.out.println(getCSVFromColumn("
Our CSV File Path", ",", "DATACOLUMN1"));
            for (String columndata : getCSVFromColumn("
Our CSV File Path", ",", "DATACOLUMN1")) {
                System.out.println(columndata);
            }

        } catch (IOException ex) {
            System.out.println(ex.getMessage());
        }
    }

    public static List<String[]> getCSVInList(String filePath, String separator) throws IOException {
        List<String[]> list = new ArrayList<>();
        /**
         * We use try-with-resources Statement for close the resource and
         * release it .
         */

try (InputStream fileStream = new BufferedInputStream(new FileInputStream(filePath));
                Reader ir = new InputStreamReader(fileStream, "UTF-8");
                BufferedReader buffReader = new BufferedReader(ir)) {
            String line;
            String[] csvData;
            while ((line = buffReader.readLine()) != null) {
                csvData = line.split(separator);
                list.add(csvData);
            }
        }
        return list;
    }

    public static Map<Integer, List<String>> getCSVInMap(String filePath, String separator) throws IOException {
        Map<Integer, List<String>> map = new HashMap<>();
        /**
         * We use try-with-resources Statement for close the resource and
         * release it .
         */

try (InputStream fileStream = new BufferedInputStream(new FileInputStream(filePath));
                Reader ir = new InputStreamReader(fileStream, "UTF-8");
                BufferedReader buffReader = new BufferedReader(ir)) {
            String line;
            String[] csvData;
            int lineNumber = 0;
            while ((line = buffReader.readLine()) != null) {
                csvData = line.split(separator);
                map.put(lineNumber, Arrays.asList(csvData));
                lineNumber++;
            }
        }
        return map;
    }

    public static List<String> getCSVFromColumn(String filePath, String separator, String columnName) throws IOException {
        List<String> columnList = new ArrayList<>();
        /**
         * We use try-with-resources Statement for close the resource and
         * release it .
         */

try (InputStream fileStream = new BufferedInputStream(new FileInputStream(filePath));
                Reader ir = new InputStreamReader(fileStream, "UTF-8");
                BufferedReader buffReader = new BufferedReader(ir)) {
            String line;
            String[] csvDataInCurrentLine;
            int lineNumber = 0;
            int columnNumber = 0;
            while ((line = buffReader.readLine()) != null) {
                csvDataInCurrentLine = line.split(separator);
                /**
                 * This is a one time checking for First Line Reading .
                 */

                if (lineNumber == 0) {
                    /**
                     * Use binarySearch to find the column name from String
                     * Array String[] csvDataInCurrentLine; .
                     */

                    if (Arrays.binarySearch(csvDataInCurrentLine, columnName) < 0) {
                        break;
                    }
                    columnNumber = Arrays.asList(csvDataInCurrentLine).indexOf(columnName);
                } else {
                    columnList.add(csvDataInCurrentLine[columnNumber]);
                }
                lineNumber++;
            }
        }
        return columnList;
    }
}

 

Saturday, August 1, 2015

FileDownload Using Firefox Profile

We know how to create Firefox profile using Selenium WebDriver code and what it is. We also download a file using using AutoIt tool. Now here we try to learn how to download file using using Firefox profile.

For downloading a file using Selenium WebDriver we need to set some properties in Firefox profile.In below we know what is the meaning of that properties.

Step 1.

        FirefoxProfile profile = new FirefoxProfile();
This is required to create a Firefox profile form Selenium WebDriver,

Step 2.
        profile.setPreference("browser.download.folderList", 2); 
Default Value of  browser.download.folderList is 1. We can set it as 0, 1, or 2.
          0  =  Firefox will save all files downloaded via the browser on the user's Desktop.
          1  =  Downloads are stored in the Downloads folder.
          2  =  Location specified for the most recent download is utilized again . 

Step 3.
        profile.setPreference("browser.download.manager.showWhenStarting", false); 
Default Value of browser.download.manager.showWhenStarting is true. This option means whether or not the Download Manager window is displayed when a file download is initiated or not. If we make it as false then Window should not be appeared.

Step 4.
        profile.setPreference("browser.download.dir", downloadLocation); 
browser. download. dir properties is required for last directory used for saving a file from the "What should (browser) do with this file?"

Step 5.
        profile.setPreference("browser.helperApps.neverAsk.openFile",
                "text/csv,application/x-msexcel,application/excel,application/x-excel,application/vnd.ms-excel,image/png,image/jpeg,text/html,text/plain,application/msword,application/xml");
 


This property browser. helperApps. neverAsk. openFile A comma-separated list of MIME types to open directly without asking for confirmation. Default value is an empty string.

Step 6. 
        profile.setPreference("browser.helperApps.neverAsk.saveToDisk",
                "text/csv,application/x-msexcel,application/excel,application/x-excel,application/vnd.ms-excel,image/png,image/jpeg,text/html,text/plain,application/msword,application/xml");


This property browser. helperApps. neverAsk. saveToDisk A comma-separated list of MIME types to save to disk without asking what to use to open the file. Default value is an empty string.

Step 7.
        profile.setPreference("browser.helperApps.alwaysAsk.force", false);
This property browser. helperApps. alwaysAsk. force True: Always ask what to do with an unknown MIME type,and disable option to remember what to open it with False (default): Opposite of above.

Step 8.
        profile.setPreference("browser.download.manager.alertOnEXEOpen", false);
This Property browser.download.manager.alertOnEXEOpen True (default): When it is true then warn the user attempting to open an executable from the Download Manager False: display no warning and allow executable to be run Note: In Firefox, this can be changed by checking the "Don't ask me this again" box when you encounter the alert.

Step 9.
        profile.setPreference("browser.download.manager.focusWhenStarting", false);
        profile.setPreference("browser.download.manager.useWindow", false);
        profile.setPreference("browser.download.manager.showAlertOnComplete", false);

This Properties browser. download. manager. focusWhenStarting True: Set the Download Manager window as active when starting a download False (default): Leave the window in the background when starting a download.

Step 10.
        profile.setPreference("browser.download.manager.closeWhenDone", false);
This Property browser. download. manager. closeWhenDone True: Close the Download Manager when all downloads are complete False (default): Opposite of the.

N.B  For detail list of MIME Type is available here
  1. http://www.freeformatter.com/mime-types-list.html
  2. http://www.sitepoint.com/web-foundations/mime-types-complete-list/
Complete Example :


package filedownload;

import java.io.File;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;


public class FileDownload {

    public static void main(String[] args) {
        WebDriver driver = new FirefoxDriver(new FirefoxBinary(new File("G:/Program Files/Mozilla Firefox/firefox.exe")),
getFireFoxProfile("G:\\Test"));
        driver.get("http://www.google.com");
    }

    public static FirefoxProfile getFireFoxProfile(String downloadLocation) {
        FirefoxProfile profile = new FirefoxProfile();
        /**
         * Default Value: 1 The value of browser.download.folderList
         * can be set to either 0, 1, or 2.
         * 0 = Firefox will save all files downloaded via the browser on the user's desktop.
         * 1 = these downloads are stored in the Downloads folder.
         * 2 = location specified for the most recent download is utilized again .
         */

        profile.setPreference("browser.download.folderList", 2);
        /**
         * Default Value: true The browser.download.manager.showWhenStarting Preference in Firefox's
         *about:config interface allows the user to specify
         * whether or not the Download Manager window is displayed when a file download is initiated.
         */

        profile.setPreference("browser.download.manager.showWhenStarting", false);
        /**
         * browser. download. dir The last directory used for saving a file from the
         * "What should (browser) do with this file?" dialog.
         */

        profile.setPreference("browser.download.dir", downloadLocation);
        /**
         * browser. helperApps. neverAsk. openFile A comma-separated list of MIME types to
         * open directly without asking for confirmation. Default value is an empty string.
         */

        profile.setPreference("browser.helperApps.neverAsk.openFile",
                "text/csv,application/x-msexcel,application/excel,application/x-excel,application/vnd.ms-excel,image/png,image/jpeg,text/html,text/plain,application/msword,application/xml");
         /**
         * browser. helperApps. neverAsk. saveToDisk A comma-separated list of MIME types
         * to save to disk without asking what to use to open the file. Default value
         * is an empty string.
         */

        profile.setPreference("browser.helperApps.neverAsk.saveToDisk",
                "text/csv,application/x-msexcel,application/excel,application/x-excel,application/vnd.ms-excel,image/png,image/jpeg,text/html,text/plain,application/msword,application/xml");
        /**
         * browser. helperApps. alwaysAsk. force True: Always ask what to do with an unknown MIME type,
         * and disable option to remember what to open it with False (default): Opposite of above
         */

        profile.setPreference("browser.helperApps.alwaysAsk.force", false);
        /**
         * browser.download.manager.alertOnEXEOpen True (default):
         * warn the user attempting to open an executable from the
         * Download Manager False: display no warning and allow
         * executable to be run Note: In Firefox, this can be changed
         * by checking the "Don't ask me this again" box when you
         * encounter the alert.
         */

        profile.setPreference("browser.download.manager.alertOnEXEOpen", false);
        /**
         * browser. download. manager. focusWhenStarting
         * True: Set the Download Manager window as active when starting a download
         * False (default): Leave the window in the background when starting a download
         */

        profile.setPreference("browser.download.manager.focusWhenStarting", false);
        profile.setPreference("browser.download.manager.useWindow", false);
        profile.setPreference("browser.download.manager.showAlertOnComplete", false);       profile.setPreference("services.sync.prefs.sync.browser.download.manager.showWhenStarting", false);
        profile.setPreference("pdfjs.disabled", true);
        /**
         * browser. download. manager. closeWhenDone
         * True: Close the Download Manager when all downloads are complete
         * False (default): Opposite of the
         */

        profile.setPreference("browser.download.manager.closeWhenDone", false);
        return profile;
    }
}