Posted in Java

[Java Core] How to read/write file in Java ?

java_logo_100

In Java, we often face the requirement to read content from a file or write content to file. In Java, we have many ways to do that :).

A) How to READ file?

  • Using BufferedReader is the simplest and most common-used method. It helps us read file line by line:

public class ReadWriteFileHandler {

public void readFileByBufferedReader(String fileName) {
 BufferedReader br = null;
 FileReader fr = null;
 try {
   fr = new FileReader(fileName);
   br = new BufferedReader(fr);
   String currentLine = "";
   while ((currentLine = br.readLine()) != null) {
// Read line by line
      System.out.println(currentLine);
   }
 } catch (IOException e) {
   e.printStackTrace();
 } finally {
   try {
     if (br != null)
        br.close();
     if (fr != null)
        fr.close();
    } catch (IOException ex) {
     ex.printStackTrace();
    }
   }
 }

}

 

 

B) How to WRITE file?

Using BufferedWriter to write content to a file, create the file if doesn’t exist, the existing content will be overridden.

The BufferedWriter is a character stream class to handle the character data. Unlike byte stream (convert data into bytes), you can just write the strings, arrays or character data directly to a file.

Please find an example below:


public void writeFile(String fileName, String content) {
BufferedWriter bw = null;
FileWriter fw = null;
try {
fw = new FileWriter(fileName);
bw = new BufferedWriter(fw);
bw.write(content);
System.out.println("Write Done !");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bw != null)
bw.close();
if (fw != null)
fw.close();
} catch (IOException ex) {
ex.printStackTrace();

}

}
}

 

Basically, you can refer above code from ReadWriteFileHandler.java

Hope this topic helpful for you :), please share if you like it 🙂

Advertisements
Posted in Java, QA, Tips

Useful scripts for Selenium in Java

selenium_icon

Selenium HQ is really useful for testing, you can find its info at Selenium Home Page . During working on it in Java projects, I found some scripts which help us save time for test cases implementation.

  • First is the necessary component in Selenium lib:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
  • Constructors:
WebDriverWait wait = new WebDriverWait(driver, 60);
WebDriver driver;
  • Collapse / Expand

This script is an action of Mouse when User clicks on an expand-icon for collapsing or expanding

JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("$('.expand-icon')[0].dispatchEvent(new MouseEvent('click'))");
  • Click on a button:
String buttonId = "//button[@id='button_id_here']";
driver.findElement(By.xpath(buttonId)).click();
  • Check a webpage exist or not
String pageTitle = "//span[contains(.,'Page_Title')]";
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(pageTitle)));
  • Find a button
String buttonElement = "//button[@title='Button text here']";
WebElement buttonElement = driver.findElement(By.xpath(buttonElement ));
  • Find a content:
String contentId = "//span[@id='Content ID Here']";
WebElement contentElement = driver.findElement(By.xpath(contentId ));
  • Others (continues)

 

 

If this topic is helpful for you. Please share 🙂

 

Posted in Frameworks, Java

FFmpeg – A complete, cross-platform solution to record, convert and stream audio and video.

java_logo_100ffmpeg

Nowaday, the Audio and Video are so popular. As a developer, you have many works to process them such as record, convert, stream, etc.

Actually, if you are an Java-er, C#-er, etc, it is really difficult to find the way to do that with pure language programming.

Today, I want to introduce one powerful ‘tool’ which can do these tasks quickly. It is FFMPEG.

FFMPEG is a complete, cross-platform solution to record, convert and stream audio and video. After using it, I found that converting video and audio has never been so easy ! You only use commands and option which are supported, only one line command ! Wow

Please find detail at below website:

http://www.ffmpeg.org/

Look at below commant to converting a .mp4 file to .avi file:

ffmpeg -i input.mp4 output.avi

Run it and Done ! You have a AVI file. How it is easy !

Because the FFMPEG is a cross-platform, so you can use it in any popular OS today such as Window, Linux, MAC OS, etc.

Beside running pure FFMPEG command directly, you can use it in your project using Java, C#, etc.

For Java, you only define a certain FFMPEG command and run it using Process object like below code:

String exeCommand = “ffmpeg -i your_input_file your_output_file”

// Run FFMPEG command
Process process = Runtime.getRuntime().exec(exeCommand);

Done. You can find the demo JAVA code using FFMPEG at Github here.

Share this topic if it is helpful 🙂

Thank you

 

Posted in Java

[Java] Java instruction for Beginner.

java_logo_100

Hi Java beginner :), if you are a Java newcomer, I think you will have many questions such as: where should I begin ? How to learn Java ? Where are good Java lession ? etc.

So, I will list some items from low to high for you. They are tools, forums, framework, design, etc. I will update this topic with new other items day-by-day and I hope it can help you :). Ok, let start:

  1. IDE tool for Java programming: eclipse, Netbean, (I like using Eclipse than Netbean 🙂 )
  2. Java Forums: java_on_stackoverflow
  3. Helpful Java frameworks: JavaFX, Springs, Hibernate, etc
  4. OO Design: http://www.oodesign.com/
  5. Basic Java tutorial: http://www.w3schools.in/java-tutorial/intro/

Share this topic if it is helpful 🙂