input.txt
View input.txt
View input.txt
package com.readingwriting.file; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import utility.Helper; public class TextFileDemo { public static void main(String[] args) { WebDriver driver = Helper.launchBrowser("chrome"); driver.get("https://www.facebook.com/"); //First Method Reading from a text File //Create a file input.txt inside your project //Enter some data inside this file try { File file = new File("input.txt"); FileReader fr = new FileReader(file); char[] data = new char[(int) file.length()]; fr.read(data); System.out.println(data); fr.close(); } catch (IOException e) { e.printStackTrace(); } //Second Method Reading from a text File //Create a file input.txt inside your project //Enter some data inside this file try { File file = new File("input.txt"); FileReader fr = new FileReader(file); int value=0; while((value=fr.read())!=-1){ System.out.println((char)value); } fr.close(); } catch (IOException e) { e.printStackTrace(); } //Third Method Reading from a text File //Create a file input.txt inside your project //Enter some data inside this file try { FileReader fr = new FileReader("input.txt"); BufferedReader br = new BufferedReader(fr); String[] line = br.readLine().split(":"); driver.findElement(By.id(line[2])).sendKeys(line[3]); line = br.readLine().split(":"); driver.findElement(By.id(line[2])).sendKeys(line[3]); line = br.readLine().split(":"); driver.findElement(By.id(line[2])).click(); br.close(); } catch (IOException e) { e.printStackTrace(); } //First Writing into a file try { File file = new File("output.txt"); if(!file.exists()){ file.createNewFile(); } FileWriter fw = new FileWriter(file); fw.write("google"); fw.write("\n"); fw.write("Facebook"); fw.write("\n"); fw.close(); } catch (Exception e) { e.printStackTrace(); } //Second Writing into a file try { File file = new File("output.txt"); if(!file.exists()){ file.createNewFile(); } //true it will append previous contents with existing contents //default is false if don't provide the second parameter FileWriter fw = new FileWriter(file, true); BufferedWriter bw = new BufferedWriter(fw); bw.write("google"); bw.newLine(); bw.write("Facebook"); bw.close(); } catch (Exception e) { e.printStackTrace(); } } }
No comments:
Post a Comment