Thursday, 16 May 2013

File URL Duplicate Killer


http://docs.oracle.com/javase/6/docs/api/javax/swing/JFileChooser.html

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/*
* DuplicateKiller.java
*
* Created on Jan 4, 1980, 2:16:09 AM
*/
package duplicatekiller;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

/**
*
* @author saeed
*/
public class DuplicateKiller extends javax.swing.JFrame {
JFileChooser jf;
File file;
String path = null;
String onlyURL=null;
String targetFile=null;
/** Creates new form DuplicateKiller */
public DuplicateKiller() {
initComponents();
}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jLabel1 = new javax.swing.JLabel();
txtFile = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
display = new javax.swing.JTextArea();
btnRead = new javax.swing.JButton();
btnShowDuplicates = new javax.swing.JButton();
btnExit = new javax.swing.JButton();
btnsSave = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Duplicate Killer v25.0.0.11 [Beta Release]");
setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

jLabel1.setText("File ");

txtFile.setToolTipText("Enter the file path or press Browse button");

jButton1.setMnemonic('b');
jButton1.setText("Browse");
jButton1.setToolTipText("Guide to select the appropriate file");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

display.setColumns(20);
display.setRows(5);
display.setToolTipText("Display Area by Duplicate Killer");
jScrollPane1.setViewportView(display);

btnRead.setMnemonic('r');
btnRead.setText("Split URL");
btnRead.setToolTipText("Read the file selected by Browse button");
btnRead.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnReadActionPerformed(evt);
}
});

btnShowDuplicates.setMnemonic('k');
btnShowDuplicates.setText("Show Duplicates");
btnShowDuplicates.setToolTipText("To eliminate the duplicate URL");
btnShowDuplicates.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnShowDuplicatesActionPerformed(evt);
}
});

btnExit.setMnemonic('x');
btnExit.setText("Exit");
btnExit.setToolTipText("Exit the System ");
btnExit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnExitActionPerformed(evt);
}
});

btnsSave.setMnemonic('s');
btnsSave.setText("Save As");
btnsSave.setToolTipText("Save the Changed File as Following");
btnsSave.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnsSaveActionPerformed(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(22, 22, 22)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 618, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(txtFile, javax.swing.GroupLayout.DEFAULT_SIZE, 595, Short.MAX_VALUE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(btnShowDuplicates, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnRead, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnExit, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 97, Short.MAX_VALUE)
.addComponent(btnsSave, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap())
);

layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btnExit, btnRead, btnShowDuplicates, btnsSave, jButton1});

layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(txtFile, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton1))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 365, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(btnRead)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(btnShowDuplicates)
.addGap(8, 8, 8)
.addComponent(btnsSave)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnExit)))
.addContainerGap())
);

layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {btnExit, btnRead, btnShowDuplicates, btnsSave, jButton1});

pack();
}// </editor-fold>

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:

jf =new JFileChooser();
int returnVal = jf.showOpenDialog(this);
file = jf.getSelectedFile();
path=file.getAbsolutePath();
txtFile.setText(path);
}

private void btnReadActionPerformed(java.awt.event.ActionEvent evt) {

if(evt.getSource()==btnRead){
if(txtFile.getText().equals("")){
JOptionPane.showMessageDialog(rootPane, "File Path is not valid", "Duplicate Killer", WIDTH);
}else{
try{
FileInputStream fstream = new FileInputStream(txtFile.getText());
int lastIndex=path.lastIndexOf("\\");

SimpleDateFormat sdf = new SimpleDateFormat("MMddyyyyhhmmss");
Date myDate = new Date();
targetFile=path.substring(lastIndex+1,path.length()-4);

String newpath=path.substring(0, lastIndex).concat("\\");
newpath=newpath.concat(sdf.format(myDate));
onlyURL=newpath.concat(targetFile+"URLONLY.txt");
BufferedWriter writer = new BufferedWriter(new FileWriter(onlyURL));

DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) {
int position=strLine.indexOf("/");
String only_URL=strLine.substring(0, position);
display.append(only_URL);// to set the text in JTextArea
display.append("\n");//to add the new line in JTextArea
writer.write(only_URL);
writer.newLine();

}
display.setEditable(false);
in.close();// Close the Main Duplicate file
writer.close();// close the onlyURL File
JOptionPane.showMessageDialog(rootPane, "Check the file that containes URL\n "+onlyURL, "Duplicate Killer", WIDTH);
}catch (Exception e){//Catch exception if any
JOptionPane.showMessageDialog(rootPane, e.getMessage(), "Duplicate Killer", WIDTH);
System.err.println(e.getMessage());

}


}
}
}

private void btnExitActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}

private void btnShowDuplicatesActionPerformed(java.awt.event.ActionEvent evt) {
if(evt.getSource()==btnShowDuplicates){
try{
FileInputStream fstream = new FileInputStream(onlyURL);
int lastIndex=onlyURL.lastIndexOf("\\");

SimpleDateFormat sdf = new SimpleDateFormat("MMddyyyyhhmmss");
Date myDate = new Date();
targetFile=path.substring(lastIndex+1,path.length()-4);
String newpath=path.substring(0, lastIndex).concat("\\");
newpath=newpath.concat(sdf.format(myDate));
onlyURL=newpath.concat(targetFile+"DUPLICATES.txt");
BufferedWriter writer = new BufferedWriter(new FileWriter(onlyURL));

DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;

ArrayList al=new ArrayList();
while ((strLine = br.readLine()) != null) {
al.add(strLine);
display.append(onlyURL);// to set the text in JTextArea
display.append("\n");//to add the new line in JTextArea
writer.write(onlyURL);
writer.newLine();
}
JOptionPane.showMessageDialog(rootPane, "Check the file that containes DuplicateURL\n "+onlyURL, "Duplicate Killer", WIDTH);

//to check the duplicates and write inside the writer file
String firstUrl;
String nextUrl;
int i,j;
for(i=0;i<al.size();i++){
firstUrl=(String) al.get(i);
for(j=0;j<al.size();j++){
nextUrl=(String) al.get(j);
if(firstUrl.equalsIgnoreCase(nextUrl)){
System.out.println(al.get(i));
}
}
}
display.setEditable(false);
fstream.close();// Close the Main Duplicate file
writer.close();// close the onlyURL File
}catch (Exception e){//Catch exception if any
JOptionPane.showMessageDialog(rootPane, e.getMessage(), "Duplicate Killer", WIDTH);System.err.println(e.getMessage());
}
}//end of the button action
}

private void btnsSaveActionPerformed(java.awt.event.ActionEvent evt) {

}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {

@Override
public void run() {
new DuplicateKiller().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btnExit;
private javax.swing.JButton btnRead;
private javax.swing.JButton btnShowDuplicates;
private javax.swing.JButton btnsSave;
private javax.swing.JTextArea display;
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextField txtFile;
// End of variables declaration
}

No comments:

Post a Comment