Thursday, 10 December 2015

Tagged under: , , ,

It's official: People with power can get away with anything!

Satyamev Jayate...Truth alone Triumphs...really?? The Indian judiciary remains unpredictable: there are moments when you feel proud, most of the times you are let down. Just a few months back, the Supreme Court made headlines by granting a convicted terrorist his last judicial resort the night before his execution. The court was convened at the eleventh hour, never before in the history of Indian judiciary were the courts opened at midnight. Hats off! And just when you thought your faith in the judiciary was being restored, just when you thought that everyone is equal before law, the Indian judiciary somehow managed to reach a new low! The poster boy of Bollywood, Mr. Salman Khan gets acquitted of all charges in the 2002 hit and run case! I mean...whaaattt??!! Seriously??

Well, this is not the first high profile case which made a mockery of the judicial system. Not long ago, we had this case of Jessica Lal's murder, where a politician's son, showed us how to play with the judiciary. An epic case of witnesses turning hostile! But atleast after a long fight, Jessica's family got justice...though not without the support of the masses and the media. The general resentment for the political class among the common people was an important factor in the courts re-opening the case and overruling the verdict. But, in Mr. Khan's hit and run case, I don't see anything like this happening. He enjoys the support of the huge (blind) fan base. Media won't pursue the case either, considering the money that is riding on him, and the influence he has. So basically, to the man who died that fateful night...Sorry bro...you messed up with the wrong man!

If you look at the hit and run case and the way it unfolded over the last 13 years or so, the topic of debate today should have been: Why the hell is it taking so long to give out the verdict! There were witnesses, and one was an on duty cop, who testified against him. It was his car, it was he who was behind the wheels, the cop who was accompanying him testified that Mr. Khan was under the influence of alcohol. But still, somehow, the evidence isn't strong they say! I mean...what else do you want? Do you want the man who died that day to come down to the courts to testify? The court had already dismissed the claim by Salman's driver, who tried to take the blame for the accident. He obviously was a stooge and dummy witness who lied under oath. So, here's a funny scenario: Salman wasn't driving, his driver was not even in the car when the accident took place, the cop and Kamal Khan (surprisingly no one cared to question Kamal Khan about the accident) were definitely not driving. Perhaps Chulbul Pandey had a self driving car way before Google even came up with the idea! We all know the shocking story of Ravindra Patil, the cop who was accompanying Salman on that fateful night. He was probably the only witness who didn't turn hostile. So, what does he get in return? He is pressurized to retract his statement, then he is arrested, fired from the police department, his family disowns him, ends up on roads with tuberculosis and dies. Well done! Great example set for those who even think of testifying against the big shots. In 2002, the court directed Mr. Khan to pay a compensation of 19 Lakh. If he wasn't responsible for the act, why was he made to pay at that time? A few months back, the court convicted and sentenced him for 5 years. But that didn't last long, when he got out on bail within a few hours of the sentencing. Well, for a common man, this bail approval would have taken god knows how long considering the way our judicial system works. But no...this is not a common man...this is Salman Khan! I was appalled to see the way people reacted to the imprisonment verdict! The twitter was filled with all kinds of crappy posts of fans and Bollywood supporting Mr. Khan. Some stupid singer went on to the extent of comparing the homeless people to dogs! Wow!! I understand that you want to be in the good books of Salman, but dude...are you even listening to what you are saying?

I know that many of the 'die hard' fans of Mr. Khan will not agree with what I say, they may ridicule this post. But it's okay. Deep down even they know what the truth is. What amuses me is that how could someone be so blind and show such an apathy towards the victim. There were tears for Salman, but no thought was spared for the victim! He has a heart of gold they say...and yet they forget that he is the same man who was accused of assaulting and harassing his past girlfriends...he is the same 'Bhai' who is being tried in the poaching endangered deer case in Rajasthan. And don't give me the bullshit about the 'Being Human' crap! It's like saying: It is okay if you kill someone as long as you follow up with some in front of the media social work. Wake up people! How can you be so blind! You need to distinguish the on screen Salman Khan from the off screen one. You need to separate the art from the artiste.

It is a sad day for Indian judiciary. It failed...it failed miserably. If you are a common man, please understand that no one gives a shit about your life. So, next time you see a fancy car driving towards you, be advised: please get out of the way....and no, even the footpaths aren't safe!

P.S.: A very heart touching post by a Hit and Run survivor, Charudatt Acharya.

Tuesday, 24 November 2015

Tagged under: , , , , , ,

Excuse me...Did you just say 'Intolerant'?

                The Modi government has somehow found a way to stay in news for the wrong reasons for quite some time now. Be it the FTII row, or the most recent spree of bans being imposed, the opposition has tried to rip apart the government with every ammo they've got. The Prime Minister failing to take a hard stance on these issues hasn't helped their cause either. But the real question to consider here is...Is the situation really so bad? Is intolerance really on the rise as claimed by the opposition and a section of intellectuals and artists who have started protesting by the 'Award Waapsi' campaign? Has my country...a birth place of four major religions and one that has deep rooted principles of secularism and harmony for ages, suddenly become intolerant in a matter of few months? Why is the country which has a long history of religious and cultural assimilation suddenly debating the rising intolerance and extremism.
Now, I may have been away from the country for more than a year, but one just cannot read enough about the recent happenings back home. It pains me to see the cheap vote bank politics being used by the government and the opposition alike.

There are concerns that intolerance is on a rise. Though the opposition had failed even more miserably to take on the fringe elements of the society during their term, they have made this their agenda to take on the government. Now, this is kind of expected from them. But the real concern is, the intellectuals in the country have started to feel insecure and are raising their concerns, calling the country 'Intolerant'! They have taken resort to returning the awards given to them by the government for achievements in their respective fields. The question here is...Do the actions of certain fringe elements make my country intolerant? I myself am totally against the recent bans being imposed by the government and I equally condemn the recent killing at Dadri and the brutal murder of Mr. Kalburgi, and yes, swift and harsh actions need to be taken against the perpetrators. But these occurrences don't make my country intolerant. When people are lynched, face blackened, or abused, it is a serious failure of law and order situation, but in no way does that make my country intolerant. There have been countless such occurrences in the past, infact a lot more ghastly ones and all equally condemnable. The common Indian is as much ashamed of the Sikh killings of '84 as he is ashamed of the Ayodhya and Gujrat riots. But it is the desire to go on, the belief that there is a greater good in the society, that makes us secular and tolerant. I am a firm believer that India has been one of the most tolerant country for ages, especially considering the diversity in religion, culture and way of life of the people who live here. You hardly ever get to see such a co-existence of different religions in any other country. Now, with such a diversity, problems are natural to arise. But the biggest achievement of ours as a successful secular country has been our ability and willingness to accept, bond and learn from different cultures. There were, are and will always be those extremists who will try to manipulate the crowd's sentiments for their gains. But those elements don't make my country intolerant.

There is no country which doesn't have their own internal problems. United States, a country which we all look up to as an example of a successful state, has been battling racism since ages. But the Americans don't call themselves an intolerant state! For me, the very fact that every individual is free to voice his opinion, may it be for or against the government itself proves how strong the foundations of our democratic principles are.

What we all need to understand here is that the recent row is not painting a good picture of our country as a whole. The international press has taken notice of these happenings. A recent Moody's report has warned that there is a rising religious strife in the country. Being people of eminence, your words will be taken seriously. It is your responsibility to protect the international outlook towards the country. Of-course you have the right to protest...of-course you have the right to voice your opinion and concerns. But won't it be more effective if we mount pressure on the respective state governments to take swift actions against the perpetrators of the recent killings instead of generalizing the actions of some extremists? Having said that, I am equally against the recent uncivilized reactions that some of the celebrities received for expressing their concern. Although our views differ, and I think that your statement was irresponsible, I will defend till death your right to say it. I only hope that these eminent personalities who wield the power to drive the change, realize that such statement of theirs has a significant impact on how the world looks at our country. We need to understand that two extreme stands are being taken here. But in reality, the intolerance atmosphere is being exaggerated, and lets accept, intolerant sections have always been there.

To all those who are concerned about the growing intolerance, let me remind you that not long ago, this was the same country where the Supreme Court opened its doors at midnight to hear the last plea of a convicted terrorist. This is the same country whose people tolerated the hero's farewell given to a convicted terrorist in a city which he blasted a few years back! This is the same country which respected the decision of a section of society when they said that they cannot sing the national song 'Vande Mataram' as it was against their religious beliefs. Just as it is wrong to label Islam as violent after a few stray terror attacks, it is equally wrong to label my country intolerant after a few stray incidences of violence. To quote a Saudi Arabian columnist Khalaf Al-Harbi, “In India, there are more than 100 religions and more than 100 languages. Yet, the people live in peace and harmony. They have all joined hands to build a strong nation that can produce everything from a sewing needle to the rocket which is preparing to go to Mars. I must say that I feel a bit jealous because I come from a part of the world which has one religion and one language and yet there is killing everywhere. No matter how the world speaks about tolerance, India remains the oldest and most important school to teach tolerance and peaceful co-existence regardless of the religious, social, political or ethnical differences.”

India, from ages has been a multi cultural, multi ethnic society with a vibrant democracy. There is a reason we call this country 'India' and not 'Hindustan'. Hinduism, Islam, Christianity, Buddhism, Jainism and Sikhism have together shaped the course of Indian history, and have had a reflective influence on our way of life. So excuse me for taking offence when someone calls my country intolerant!

Tuesday, 29 July 2014

Tagged under: , , , , ,

Zend Framework from Scratch Part II: MVC and Creating Zend Project

In the Part I of the 'Zend from Scratch' series, we installed Zend and configured the environment to create our first Zend project. Continuing with our 'Zend from Scratch' series, we will have a look at the MVC framework using Zend and then create our first Zend web application. For those who haven't read the Part I of this series, here's the blog post of it:

Model-View-Controller (MVC) framework:

Model–view–controller (MVC) is a software architectural pattern for implementing user interfaces. It divides a given software application into three interconnected parts, so as to separate internal representations of information from the ways that information is presented to or accepted from the user.
  • Model - This is the part of your application that defines its basic functionality behind a set of abstractions. Data access routines and some business logic can be defined in the model.
  • View - Views define exactly what is presented to the user. Usually controllers pass data to each view to render in some format. Views will often collect data from the user, as well. This is where you're likely to find HTML markup in your MVC applications.
  • Controller - Controllers bind the whole pattern together. They manipulate models, decide which view to display based on the user's request and other factors, pass along the data that each view will need, or hand off control to another controller entirely.

We will discuss more on MVC and how it fits in the Zend framework once we create our first project.

Create a Zend Framework Project:

Once you have completed all the steps in Part I of this series, we can start with creating of first PHP Zend project.
Open command prompt and navigate to the folder where you want to create your project. It should be the folder where the server points to. If you have XAMPP installed, its 'C:\xampp\htdocs'. Now run the below command:
zf create project zendtest
Here, 'zendtest' is the name of the project.

You may get an error for 'PHPUnit_Framework_TestCase', but this should not be a concern as it will not create any hindrance while working with the development activities. The error is related to Unit Testing.

Once you run the above command, you can find a folder named 'zendtest' (the name of our project) in the 'htdocs' folder. Below is the directory structure of our newly created project:


Now, once we can see the directory structure, we can relate this back to the MVC framework.

Zend Framework and MVC:

Open the 'application' folder in the newly created project:

As you can see, there are three prominent folders: models, views and controllers.

Controllers:

Your application's action controllers contain your application workflow, and do the work of mapping your requests to the appropriate models and views.
Open the IndexController.php file in the controllers folder:

<?php

class IndexController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        // action body
    }


}

Note the method 'indexAction()'. IndexController is a fallback controller and which also serves the home page of the site. Since there is nothing in the indexAction() method right now, it takes the default view, which is 'C:\xampp\htdocs\zendtest\application\views\scripts\index\index.phtml'. This is nothing but the home page of your project.
Open your browser and goto
http://localhost/zendtest/public
This is what you get:
This page is nothing but the index.phtml in 'C:\xampp\htdocs\zendtest\application\views\scripts\index'.
Now, lets have a look into Views.

Views:

View scripts are placed in application/views/scripts/, where they are further categorized using the controller names. In our case, we have an IndexController and an ErrorController, and thus we have corresponding index/ and error/ subdirectories within our view scripts directory.

Creating a Layout:

You may have noticed that the view scripts in the previous sections were HTML fragments- not complete pages, and they lack html, head, and body tags. This is purposely done by design, so that all actions return content related to that action only, and not the whole application. This gives a consistent look and feel throughout the application.
To create a layout, open the command prompt and navigate to the project directory. Now run the below command:
zf enable layout
The output is as below:

This will create a 'layout.phtml' file in 'C:\xampp\htdocs\zendtest\application\layouts\scripts'. If we open the file, below is what we see:

<?php echo $this->layout()->content; ?>

This is nothing but echoing the content from the view. We need to wrap this content with proper header and footer files. Replace the code in 'layout.phtml' file with the below code:

<!DOCTYPE html>
<html>
    <head>
        <title>Zend Test</title>
    </head>
    <body>
        <div id="wrap">
            <div id="header">
                <p>This is the header</p>
            </div>
            <div id="content">
                <!-- Echo our content here! -->
                <?php echo $this->layout()->content; ?>
            </div>
            <div id="footer">
                <p>This is the footer</p>
            </div>
        </div>
    </body>
</html>

Now, again goto 'http://localhost/zendtest/public' in your browser. You now have the previous page with a proper header and footer!!


So far, we have created the basic layout of our site using the Zend framework. In the next part, we will cover creating Models and database, creating new controllers and the rest of the things required to create a full-fledged web site.

A summary of what we covered in this tutorial:

  • What is Model-View-Controller framework
  • Zend Framework as an MVC
  • Creating Zend Project
  • Understanding the Controllers and Views in Zend Framework
  • Creating a layout using Zend
Stay tuned for the final segment of this three part Zend from scratch series.
You can subscribe for free or become a follower to get the latest alerts and posts.

Sunday, 27 July 2014

Tagged under: , , ,

Zend Framework from Scratch Part I: Install and Configure PHP Zend Framework

Zend Framework (ZF) is an open source, object-oriented, full-stack PHP framework created by Zend Technologies implemented in PHP 5 and licensed under the New BSD License.

In this 'Zend from Scratch' series, we will try to get the understanding about Zend Framework...not theoretically, but covering things like installing and configuring Zend Framework, understanding the MVC framework and developing a sample application using Zend.

In this series, I would be using XAMPP for all purposes. XAMPP is nothing but a package of PHP, MySQL, Apache and other services. So, the PHP environment at my end is set up with XAMPP. You can download xampp from https://www.apachefriends.org/index.html.

So, lets get started...This part of the series will cover Installing and Configuring the Zend Framework on Windows.

System Requirements:

Zend Framework version 1.7 requires PHP 5.2.4 or later. Previous versions required PHP 5.1.4 or later, although the ZF Programmer's Reference Guide strongly recommended PHP 5.2.3 or later for security and performance improvements included in these versions of PHP.

Step 1: Download Zend Framework

Download the latest version of Zend Framework 1 (Full) from http://framework.zend.com/. At the time of writing this tutorial, the latest version of Zend was 1.12.7.

Step 2: Configure Zend Framework

  • Once you download the .zip file, extract its contents to a folder. The folder structure after extracting should look like this:
  • Now, copy the 'bin' folder and paste it in the 'PHP' folder, which for me (since I'm using xampp) is 'C:\xampp\php'. Rename the copied folder to 'zend-bin'.
  • Open the 'library' folder in the extracted folder. You can find a folder named 'Zend' in it. Copy this 'Zend' folder and paste it in your PHP's include_path. You can find the include_path for your environment in the 'php.ini' file. The default include_path for xampp is:
For UNIX:
include_path = ".:/php/includes"

For Windows:
include_path = ".;C:\xampp\php\PEAR"

The format of include_path is "path1;path2". The '.'(dot) in path1 means the current directory. So, there are basically 2 include_path(s).
  • Lastly, we need to configure the Zend CLI tool. To do this, we need to add the 'zend-bin' folder to the path System variables. Right click on My Computer and select Properties. In the Advanced tab, click on 'Environment variables'. Here, in the 'System Variables' section, click on edit, and add the path to 'zend-bin' folder (C:\xampp\php\zend-bin) at the end:
  • Just to ensure that the setup is fine (which it should be if you have followed the steps exactly), open the command prompt and run 'zf --help'. This command gives a list of all the commands you can use with Zend CLI. You should get he below output:

And there you are!! You have just set up the environment for creating your first project using the Zend Framework.

The next part of this series will give a brief idea about the MVC architecture of the Zend framework, and we will create our first project using Zend...So stay tuned!!

You can subscribe for free or become a follower to get the latest alerts and posts.

For the Part II of this series, click here.

Monday, 21 July 2014

Tagged under: , , ,

Print Web Page using JavaScript

Many a times, we come across situations where we have to selectively print only a certain part of a web page. When it comes to a normal print command, it will print the entire web page for you. However, what if you want only a certain section, say a certain <div>...</div> to be printed, and exclude the content of the rest of the page?
This tutorial aims at doing just the thing...

Step 1: Enclose the content that you want to be printed in a <div> tag, and name it, say 'report':

<div id="report">
.
.
Your Content goes here...
.
.
</div>

Step 2: Add a print button:

<input type="button" value="Print Summary" onclick="printContent('report')"/>

As we see in the above snippet, 'printContent()' is the function that would do the printing job for us. To this function, we pass the id of the <div> tag whose content we want to print.

Step 3: Create a javaScript function 'printContent()':
At the bottom of the page, add the below javascript function

<script type="text/javascript">
<!--
function printContent(id){
str=document.getElementById(id).innerHTML
newwin=window.open('','printwin','left=100,top=100,width=400,height=400')
newwin.document.write('<HTML>\n<HEAD>\n')
newwin.document.write('<TITLE>Summary</TITLE>\n')
newwin.document.write('<script>\n')
newwin.document.write('function chkstate(){\n')
newwin.document.write('if(document.readyState=="complete"){\n')
newwin.document.write('window.close()\n')
newwin.document.write('}\n')
newwin.document.write('else{\n')
newwin.document.write('setTimeout("chkstate()",2000)\n')
newwin.document.write('}\n')
newwin.document.write('}\n')
newwin.document.write('function print_win(){\n')
newwin.document.write('window.print();\n')
newwin.document.write('chkstate();\n')
newwin.document.write('}\n')
newwin.document.write('<\/script>\n')
newwin.document.write('</HEAD>\n')
newwin.document.write('<BODY onload="print_win()">\n')
newwin.document.write(str)
newwin.document.write('</BODY>\n')
newwin.document.write('</HTML>\n')
newwin.document.close()
}
//-->
</script>

The above function will take the id of the <div> tag whose content we want to print.
And there you are!! You can now print the page. With the help of software like PDF Creator, now you can also selectively print the page to a PDF file.
This functionality can be used where you want to print reports, receipts etc.

Thursday, 17 July 2014

Tagged under: , , , ,

Forgot Password Functionality in PHP


When you implement a Login functionality, one thing that becomes an obvious inclusion is the 'Forgot Password' functionality. There are several ways to implement the forgot password functionality, some of which may be:


  • Ask the security Question
  • Send a new password to the registered mail, etc etc
But the most popular one is
  • Send an encoded, one time password reset link to the registered mail id
We would be discussing on this, i.e. the password reset link functionality implementation in this tutorial.

Before we get on with the actual coding, we need to create a proper database schema:
In the Table where you store the login credentials, add another column 'forgotpassword', which will store the encoded random string that you would be sending to the registered mail id.

Now comes the code...

Step 1: Ask for the email id with which the user is registered

<?php

$db = mysql_connect($dbhost, $dbuser, $dbpassword);
mysql_select_db($dbdatabase, $db);

if(isset($_POST['submit'])) {
$selectuser="Select * from USERTABLE where emailid='".addslashes($_POST['email'])."'";
$result = mysql_query($selectuser);
$numrows = mysql_num_rows($result);
if($numrows == 1) {
 $row = mysql_fetch_assoc($result);
 $validusername=$result['username'];
 for($i = 0; $i < 16; $i++) {
  $randomstring .= chr(mt_rand(1,126));
 }
 $verifyurl = "resetpwd.php";
 $verifystring = urlencode($randomstring);
 $verifyemail = urlencode($_POST['email']);
 
 $updateuser="Update USERTABLE SET forgotpassword='".addslashes($randomstring)."' WHERE emailid='".addslashes($_POST['email'])."'";
 mysql_query($updateuser);
 
 $mail_body=<<<_MAIL_
Hi,
A request has been made to reset the password for your account.
Please click on the following link to go to the password reset page:
$verifyurl?email=$verifyemail&verify=$verifystring
_MAIL_;
 
 require("class.phpmailer.php");
 $mailer = new PHPMailer();
 $mailer->IsSMTP();
 $mailer->Host = ""; //Add smtp details
 $mailer->SMTPAuth = TRUE;
 $mailer->Username = "";  // Change this to your gmail adress
 $mailer->Password = "";  // Change this to your gmail password
 $mailer->From = "";  // This HAVE TO be your gmail adress
 $mailer->FromName = ""; // This is the from name in the email, you can put anything you like here
 $mailer->Body = $mail_body;
 $mailer->Subject = "User Verification";
 $mailer->AddAddress($_POST['email']);  // This is where you put the email adress of the person you want to mail
 if(!$mailer->Send()){
  echo "Message was not sent<br/ >";
  echo "Mailer Error: " . $mailer->ErrorInfo;
 }
 else{
  echo "<center>A link has been
emailed to the address you entered below.
Please follow the link in the email to reset the passwod for 
your account.</center><br>";
 }
}
else{
 echo "We could not find any registered user with the email id as ".addslashes($_POST['email'])."<br>
 Please Enter the correct mail id & try again";
}
}
else{
?>

<div id="content">
<form action="forgotpwd.php" method="post">
<table>
<tr>
<td>Enter the Mail Id you registered with</td>
<td><input type="text" name="email"></td>
</tr>
<tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value="Submit"></td>
</tr>
</table>
</form>
</div>
<?php
}
?>


In the above snippet of code, We ask the user to enter the mail id he/she registered with. Once the user submits the form, you first check if the email id is present in the database, if not, you notify the user accordingly.
Once you find the email id in the database, the next step is to create a random string, encode it and append it to the password reset page url. Encoding of a string can be done using urlencode() method in PHP.
We also store the encoded randomly generated string to the database. Next, encode the mail id of the user and append it too to the url. The next part is where we send this url to the registered mail id. For this, I have used PHP Mailer.
To know more about PHP Mailer as to how it works, refer to my tutorial on this: Sending Email using PHP

Step 2: Check the encoded url when the user clicks it from his mail

<?php

$verify = addslashes(urldecode($_GET['verify']));
$verifymail = urldecode($_GET['email']);

if (isset($_POST['confirmpwdreset'])) {
 $resetpwd="Update USERTABLE SET password='". md5($_POST['newpwd'])."' WHERE emailid='".$verifymail."'"; 
 if(mysql_query($resetpwd))
 {
  $removeverifystring="Update USERTABLE SET forgotpwd='' WHERE emailid='".$verifymail."'";
  mysql_query($removeverifystring);
 }
 echo "<b>Your Password has been reset successfully.Login with your new password</b>";
}
else{
if($verify!=''){
$sql = "SELECT * FROM USERTABLE WHERE forgotpwd= '" . $verify . "' AND emailid = '" .$verifymail . "';";
$result = mysql_query($sql);
$numrows = mysql_num_rows($result);
if($numrows == 1) {
$row = mysql_fetch_assoc($result);
echo "<br><h3>Hi </h3><br>";
echo "You can now reset your password<br><br>";
?>
<form name="reset" action="resetpwd.php?emailid=<?php echo $_GET['emailid'];?>&verify=<?php echo $_GET['verify'];?>" method="post">
<table>
<tr>
<td>New password: </td><td><input type="password" name="newpwd"></td>
</tr>
<tr>
<td>Confirm password: </td><td><input type="password" name="confirmpwd"></td>
</tr>
<tr>
<td></td><td><input type="submit" name="confirmpwdreset" value="Confirm"></td>
</tr>
</table>
<?php
} 

else {
echo "The link is either invalid or has expired";
}
}
else echo "The link is either invalid or has expired";
}
?>


Once the user clicks the url in the mail, we decode the random string and the email-id in the url. For decoding, we can use the urldecode() method in PHP. Next, all we have to do is to check the database for the combination of the random string and the mail-id. Once we find that combo, we ask the user to enter the new password, which on submission is updated in the database. As we update the database with the new password value, we reset the value of the corresponding 'forgotpassword' field to '' so that the same random string cannot be reused, thus ensuring one time usability of password reset url

Thats it!! We now have a full fledged 'Forgot Password' functionality to make life easy for the users.

Saturday, 14 June 2014

Tagged under: , ,

Java code to remove redundant data / duplicate entries spread across multiple Files

The scenario here was that there are several text files, with redundant data in them. The objective was to remove the redundant data spread across multiple text files, and collate the entire data in a single file. This task can ofcourse be done using excel macros, but not being too fond of excel, I preferred writing a java code for the same.
Below is the Java code to accomplish this task:


package aj;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
 
public class Load {
 public static void main(String[] args) throws IOException {
 
  System.out.println("Enter the no of files");
  Scanner scr=new Scanner(System.in);
  int no=scr.nextInt();
  int i=1;
  String source;
  String dest = "Filepath\\Dest.txt";
  String result = "Filepath\\Result.txt";


  while(i<=no)
  {
  source = "Filepath\\File"+i+".txt";
  copyContent(source, dest);
  ++i;
  }
  
  removeDuplicates(dest, result);
 }
 
 static void copyContent(String source, String dest)throws IOException {
  File fin = new File(source);
  FileInputStream fis = new FileInputStream(fin);
  BufferedReader in = new BufferedReader(new InputStreamReader(fis));
 
  FileWriter fstream = new FileWriter(dest, true);
  BufferedWriter out = new BufferedWriter(fstream);
 
  String aLine = null;
  while ((aLine = in.readLine()) != null) {
   out.write(aLine);
   out.newLine();
  }
  out.newLine();
  out.newLine();
 
  out.write("***************************************************");
  out.newLine();
  out.newLine();
  
  in.close();
 
  out.close();
  
 }
 
 static void removeDuplicates(String dest, String result) throws IOException{

  int flag=0;
  File fin = new File(dest);
  FileInputStream fis = new FileInputStream(fin);
  BufferedReader in = new BufferedReader(new InputStreamReader(fis));

  
  FileWriter fstream = new FileWriter(result, true);
  BufferedWriter out = new BufferedWriter(fstream);
 
  String aLine = null;
  String bLine = null;

  int i=1,j=1;
  while ((aLine = in.readLine()) != null) {
   j=i;
   flag=0;
   File fin1 = new File(dest);
   FileInputStream fis1 = new FileInputStream(fin1);
   BufferedReader in1;
   in1 = new BufferedReader(new InputStreamReader(fis1));

   if(aLine.equals("***************************************************")==true || i==1)
   {
    out.write(aLine);
    out.newLine();
    
   }
   else{
    while(j>1)
    {
     bLine = in1.readLine();
     if (bLine!=null && bLine.equals(aLine)) {
      flag=1;
     }
     --j;
    }

    if(flag==0)
    {
     out.write(aLine);
     out.newLine();

    }
   }
   ++i;
   in1.close();

  }
  
  in.close();
  out.close();
  
  
 }
}


The function copyContent() copies the data from all the files to a single text file.
Once this is done, the function removeDuplicates() removes the redundant data from the newly created file. The final cleaned data is now available in the 'Result.txt' file.