Sunday, August 19, 2012

Google Drive - Language Change issue.

This post is not something which is related to Programming. It's the issue I had with the newly launched Google Drive. I have my windows 7 in French but with some tweaks now its in English. The issue I had with Google Drive was that no matter how many times I installed/re-installed the application on windows, it always was with French as language. There is no option in the settings of Google Drive to change it. And I prefer to have it in English. So here is the trick to solve this issue.

Close Google Drive from the system tray and do following:

Solution 1 (Need to be done every time you restart windows and that's annoying):
Step 1: Go to command prompt. (Run -> cmd).
Step 2: run the following command: 
C:\>set LANG="en_US" C:\Program Files\Google\Drive\googledrive.exe 
Solution 2 (Easy and not Annoying):
Set a new environment variable with variable name "LANG" and value "en_US" (In case if you don't know how to set enviornment variable in Windows check my old post on Windows Environment Variable).

Once all this is done start Google drive again and Voila.. problem solved.

Hope this helped you. 

Thursday, August 9, 2012

Writing in a File using Java

In my previous posts I talked about how to Create and Delete files using Java. Now, once you have created a file the next step is to write some content in those files and that can be achieved by using:
  1. BufferedWriter, or
    FileWriter writer = new FileWriter(fileToWriteIn);
    BufferedWriter bufferedWriter = new BufferedWriter(writer);
    bufferedWriter.write(toWrite);
    bufferedWriter.close();
  2. FileOutputStream.
    FileOutputStream stream = new FileOutputStream(fileToWriteIn);
    stream.write(toWrite.getBytes());
    stream.flush();
    stream.close();
There are classes like FileWriter available to perform write operation on a file but since writer sends its output immediately to the underlying character or byte stream. So that is why until prompt output is required, it is recommended to wrap a BufferedWriter around it.

Here is an example with full code and main method:

package home.flicker.java.io.file;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class WriteInFile {
    File file = null;
    /**
     * Method to create a file with the given name.
     */
    public boolean createFile(String fileName) {
        boolean result = false;
        file = new File(fileName);
        // creating the new File. The Method createNewFile() returns TRUE if file is successfully created else it returns FALSE.
        try {
            result = file.createNewFile();
        } catch (IOException e) {
            System.err.println("Error while creating file!!! " + e.getMessage());
        }
        return result;
    }
    /**
     * Method to read content from user and write it in given file using BufferedReader.
     */
    public boolean writeInFileWithBufferedWriter(File fileToWriteIn){
        boolean result = false;
        String toWrite = readFromUser("Data to write");
        try {
            FileWriter writer = new FileWriter(fileToWriteIn);
            BufferedWriter bufferedWriter = new BufferedWriter(writer);
            bufferedWriter.write(toWrite);
            bufferedWriter.close();
            result = true;
        } catch (IOException e) {
            System.err.println("Error while writing in file!!! " + e.getMessage());
        }
        return result;
    }

    /**
     * Method to read content from user and write it in given file using FileOutputStream.
     */
    public boolean writeInFileWithFileOutputStream(File fileToWriteIn) {
        boolean result = false;
        String toWrite = readFromUser("Data to write");
        try {
            FileOutputStream stream = new FileOutputStream(fileToWriteIn);
            stream.write(toWrite.getBytes());
            stream.flush();
            stream.close();
            result = true;
        } catch (FileNotFoundException e) {
            System.err.println("Error: File not found!!! How the hell I am supossed to write   :P " + e.getMessage());
        } catch (IOException e) {
            System.err.println("Error while writing in file!!! It's stupid   x( " + e.getMessage());
        }
        return result;
    }

    /**
     * Reads input from user as string and puts the label as question.
     */
    public static String readFromUser(String label) {
        Scanner scanner = new Scanner(System.in);
        System.out.println(label + ": ");
        String input = scanner.next();
        return input;
    }

    /**
     * Main method.
     */
    public static void main(String args[]) throws IOException{
        WriteInFile example = new WriteInFile();
        String fileName = readFromUser("Enter File name");
        example.createFile(fileName);
        example.writeInFileWithBufferedWriter(example.file);  
        example.writeInFileWithFileOutputStream(example.file);
    }
}


The thing with these methods is that they over-write the content which is already in the file. To avoid that you need to append data. Will cover that some other time. 
I hope this post helps you. Share your experience or issues...

Saturday, August 4, 2012

JUnit: Basic Annotations


What is JUnit?

JUnit 4.x is a test framework which uses annotations to identify methods that are test methods. JUnit assumes that all test methods can be executed in an arbitrary order. Therefore tests should not depend on other tests.
To write a test with JUnit
  • Annotate a method with @org.junit.Test
  • Use a method provided by JUnit to check the expected result of the code execution versus the actual result
You can use Eclipse or the org.junit.runner.JUnitCore class to run the test.

Various Annotations of JUnit:


  1. @Test: This annotations tells which method is a test method. 

    @Test
    public void methondName(){
    ...........
    }


    You can some parameters with this annotations.
    • @Test (expected = Exception.class) Fails, if the method does not throw the named exception. 
    • @Test(timeout=1000) Fails, if the method takes longer than 100 milliseconds.

  2. @Before: The method which this annotation will execute before each test method. For example in cases where you need to initialize new class or read input data.

    @Before
    public void methodName(){
    ........
    }
  3. @After: This annotation signifies the method which executes after each test. For example where you need to roll back changes done by tests. 

    @After
    public void methodName(){
    ........
    }
  4. @BeforeClass: Method with this annotations executes only once before the start of all tests. This can be used in case where you need a database connection for the tests and you create it only once in the beginning. 

    @BeforeClass
    public void methodName(){
    ........
  5. @AfterClass: This annotation signifies the method which executes only once and that too at the end of all tests for example to close the data connection. 

    @AfterClass
    public void methodName(){
    ........
    }
  6. @Ignore: Will ignore the test method. You can use it in cases where your code has changed and the test is not yet adapted to accommodate those changes. 

    @Ignore
    public void methodName(){
    ........
    }