Utiliser un outil de comparaison comme Incomparable , KDiff3 , ou Forcément devrait être suffisant.
METTRE À JOUR:
Je me sentais généreux ce matin alors je vous ai concocté cela. Devrait faire ce que tu veux.
Quelques notes:
1.) Ce code gérera les doublons. Par exemple, si une ligne avec le même texte apparaît deux fois dans le petit fichier, elle est censée apparaître deux fois dans le gros fichier.
2.) Ce code ignore la commande de ligne selon votre cas d'utilisation.
3.) Petit bug concernant les lignes vierges à la fin d'un fichier que je ne voulais pas gâcher. Ce code traite une ligne vide comme une ligne comme n'importe quelle autre ligne tant qu'elle ne se trouve pas à la fin du fichier, auquel cas une ligne vide est autorisée (et ignorée). Par exemple, si le petit fichier comporte 3 lignes vierges à la fin du fichier et aucune autre ligne vierge, alors le fichier volumineux doit comporter au moins 2 lignes vierges au milieu des autres lignes ou 3 lignes vierges au début. fin du fichier.
Courir:
1.) Assurez-vous d'avoir un JDK installée
2.) Assurez-vous que Java est sur votre chemin. Si vous utilisez un système Windows, accédez au Panneau de configuration & gt; Système & gt; Paramètres système avancés & gt; Variables d'environnement et sélectionnez Path
sous la section Variables système. Ajoutez l’emplacement de votre dossier JDK bin à la variable de chemin d’accès, en veillant à bien le séparer de l’entrée précédente par un point-virgule.
C:\Program Files (x86)\Java\jdk1.6.0_38\bin;
3.) Copiez le code ci-dessous dans un fichier nommé FileLineComparator.java
4.) Ouvrez une invite de commande et accédez au répertoire contenant le fichier que vous venez de créer.
5.) Type javac FileLineComparator.java
6.) Type java -cp . FileLineComparator
7.) Profitez!
import java.io.*;
import java.util.ArrayList;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
public class FileLineComparator extends javax.swing.JFrame {
public FileLineComparator() {
initComponents();
}
@SuppressWarnings( "unchecked" )
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
fileChooser = new javax.swing.JFileChooser();
smallFileTextField = new javax.swing.JTextField();
smallFileLabel = new javax.swing.JLabel();
largeFileLabel = new javax.swing.JLabel();
largeFileTextField = new javax.swing.JTextField();
outputFileLabel = new javax.swing.JLabel();
outputFileTextField = new javax.swing.JTextField();
goButton = new javax.swing.JButton();
smallFileButton = new javax.swing.JButton();
largeFileButton = new javax.swing.JButton();
outputFileButton = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
smallFileLabel.setText("Small text file:");
largeFileLabel.setText("Large text file:");
outputFileLabel.setText("Output file:");
goButton.setText("Go!");
goButton.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
goButtonMouseClicked(evt);
}
});
smallFileButton.setText("Browse");
smallFileButton.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
smallFileButtonMouseClicked(evt);
}
});
largeFileButton.setText("Browse");
largeFileButton.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
largeFileButtonMouseClicked(evt);
}
});
outputFileButton.setText("Browse");
outputFileButton.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
outputFileButtonMouseClicked(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(largeFileLabel)
.addComponent(smallFileLabel))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(outputFileTextField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 194, Short.MAX_VALUE)
.addComponent(largeFileTextField, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(smallFileTextField)))
.addComponent(outputFileLabel))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(largeFileButton)
.addComponent(smallFileButton)
.addComponent(outputFileButton)))
.addComponent(goButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap(16, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(smallFileTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(smallFileLabel)
.addComponent(smallFileButton))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(largeFileLabel)
.addComponent(largeFileTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(largeFileButton))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(outputFileTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(outputFileLabel)
.addComponent(outputFileButton))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(goButton, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void smallFileButtonMouseClicked( java.awt.event.MouseEvent evt ) {
setSelectedFile( FILE_TYPES.SMALL );
}
private void largeFileButtonMouseClicked( java.awt.event.MouseEvent evt ) {
setSelectedFile( FILE_TYPES.LARGE );
}
private void outputFileButtonMouseClicked( java.awt.event.MouseEvent evt ) {
setSelectedFile( FILE_TYPES.OUTPUT );
}
private void goButtonMouseClicked( java.awt.event.MouseEvent evt ) {
errorStub = new StringBuilder();
smallFile = new File( smallFileTextField.getText() );
smallFileTextField.setText( smallFile.getAbsolutePath() );
largeFile = new File( largeFileTextField.getText() );
largeFileTextField.setText( largeFile.getAbsolutePath() );
outputFile = new File( outputFileTextField.getText() );
outputFileTextField.setText( outputFile.getAbsolutePath() );
process();
}
private void setSelectedFile( FILE_TYPES fileType ) {
int returnVal = fileChooser.showOpenDialog( null );
if( returnVal == JFileChooser.APPROVE_OPTION ) {
File file = fileChooser.getSelectedFile();
switch( fileType ) {
case SMALL:
smallFileTextField.setText( file.getPath() );
break;
case LARGE:
largeFileTextField.setText( file.getPath() );
break;
case OUTPUT:
outputFileTextField.setText( file.getPath() );
break;
}
}
}
private void process() {
ArrayList<String> smallFileLines = readFileLines( smallFile );
ArrayList<String> largeFileLines = readFileLines( largeFile );
ArrayList<String> outputFileLines = new ArrayList<String>();
for( String line : smallFileLines ) {
if( !largeFileLines.contains( line ) ) {
outputFileLines.add( line );
} else {
largeFileLines.remove( line );
}
}
if( errorStub.length() == 0 ) {
writeOutput( outputFileLines );
}
if( errorStub.length() == 0 ) {
JOptionPane.showMessageDialog( null, "Finished Successfully!" );
} else {
JOptionPane.showMessageDialog( null, errorStub.toString() );
}
}
private ArrayList<String> readFileLines( File file ) {
ArrayList<String> al = new ArrayList<String>();
try {
FileReader fr = new FileReader( file );
BufferedReader bufRdr = new BufferedReader( fr );
String line = null;
while( ( line = bufRdr.readLine() ) != null ) {
al.add( line );
}
bufRdr.close();
} catch( IOException ioex ) {
errorStub.append( String.format( "Error reading file %s\r\n", file.getAbsolutePath() ) );
System.err.println( ioex.getMessage() );
}
return al;
}
private void writeOutput( ArrayList<String> outputFileLines ) {
try {
FileWriter fw = new FileWriter( outputFile );
BufferedWriter bw = new BufferedWriter( fw );
for( int i = 0; i < outputFileLines.size(); i++ ) {
String line = String.format( "%s%s", outputFileLines.get( i ), i + 1 == outputFileLines.size() ? "" : "\r\n" );
bw.write( line );
}
bw.close();
} catch( Exception ex ) {
errorStub.append( String.format( "Error writing file %s\r\n", outputFile.getAbsolutePath() ) );
System.err.println( ex.getMessage() );
}
}
public static void main( String args[] ) {
try {
for( javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels() ) {
if( "Nimbus".equals( info.getName() ) ) {
javax.swing.UIManager.setLookAndFeel( info.getClassName() );
break;
}
}
} catch( ClassNotFoundException ex ) {
java.util.logging.Logger.getLogger( FileLineComparator.class.getName() ).log( java.util.logging.Level.SEVERE, null, ex );
} catch( InstantiationException ex ) {
java.util.logging.Logger.getLogger( FileLineComparator.class.getName() ).log( java.util.logging.Level.SEVERE, null, ex );
} catch( IllegalAccessException ex ) {
java.util.logging.Logger.getLogger( FileLineComparator.class.getName() ).log( java.util.logging.Level.SEVERE, null, ex );
} catch( javax.swing.UnsupportedLookAndFeelException ex ) {
java.util.logging.Logger.getLogger( FileLineComparator.class.getName() ).log( java.util.logging.Level.SEVERE, null, ex );
}
java.awt.EventQueue.invokeLater( new Runnable() {
public void run() {
new FileLineComparator().setVisible( true );
}
} );
}
private enum FILE_TYPES {
SMALL,
LARGE,
OUTPUT
}
private File smallFile = null;
private File largeFile = null;
private File outputFile = null;
private StringBuilder errorStub = null;
// Variables declaration - do not modify
private javax.swing.JFileChooser fileChooser;
private javax.swing.JButton goButton;
private javax.swing.JButton largeFileButton;
private javax.swing.JLabel largeFileLabel;
private javax.swing.JTextField largeFileTextField;
private javax.swing.JButton outputFileButton;
private javax.swing.JLabel outputFileLabel;
private javax.swing.JTextField outputFileTextField;
private javax.swing.JButton smallFileButton;
private javax.swing.JLabel smallFileLabel;
private javax.swing.JTextField smallFileTextField;
// End of variables declaration
}