Created
July 20, 2011 21:13
-
-
Save Shadonwk/1095949 to your computer and use it in GitHub Desktop.
Codigo de la Interfaz del programita Que busca cadenas en archivos.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package mx.com.robertoleon.code; | |
import java.io.BufferedReader; | |
import java.io.File; | |
import java.io.FileReader; | |
import java.io.FilenameFilter; | |
import java.util.ArrayList; | |
import java.util.logging.Filter; | |
/** | |
* Created by IntelliJ IDEA. | |
* User: shadonwk | |
* Date: 20/07/11 | |
* Time: 11:28 AM | |
* To change this template use File | Settings | File Templates. | |
*/ | |
public class BuscaCadenaEnArchivos { | |
public String buscar(String criterio, String directorio, String tipoArchivo){ | |
File ruta = new File(directorio); | |
ArrayList<String> arreglo = new ArrayList<String>(); | |
filtrarYObtenerArchivos(arreglo,ruta,tipoArchivo); | |
ArrayList<String> listResultados = obtenerArchivosConCoincidencias(arreglo,criterio); | |
String salida="Se obtuvieron coincidencias en los siguientes archivos:\n"; | |
if(listResultados.size()>0){ | |
for(int x=0; x<listResultados.size();x++){ | |
salida+=listResultados.get(x)+"\n"; | |
} | |
} else{ | |
salida+="no se encontraron resultados..."; | |
} | |
return salida; | |
} | |
private ArrayList<String> obtenerArchivosConCoincidencias(ArrayList<String> arreglo, String criterio) { | |
ArrayList<String> archivosAceptados = new ArrayList<String>(); | |
String contenidoArchivo=""; | |
for(int m=0; m<arreglo.size();m++){ | |
contenidoArchivo=""; | |
File archivo = null; | |
FileReader fr = null; | |
BufferedReader br = null; | |
try { | |
// Apertura del fichero y creacion de BufferedReader para poder | |
// hacer una lectura comoda (disponer del metodo readLine()). | |
archivo = new File (arreglo.get(m)); | |
fr = new FileReader (archivo); | |
br = new BufferedReader(fr); | |
// Lectura del fichero | |
String linea; | |
while((linea=br.readLine())!=null){ | |
//System.out.println(linea); | |
contenidoArchivo+=linea; | |
} | |
}catch(Exception e){ | |
e.printStackTrace(); | |
}finally{ | |
// En el finally cerramos el fichero, para asegurarnos | |
// que se cierra tanto si todo va bien como si salta | |
// una excepcion. | |
try{ | |
if( null != fr ){ | |
fr.close(); | |
} | |
}catch (Exception e2){ | |
e2.printStackTrace(); | |
} | |
} | |
//System.out.println("****buscando en..."+contenidoArchivo); | |
contenidoArchivo=contenidoArchivo.toUpperCase(); | |
criterio=criterio.toUpperCase(); | |
//System.out.println(contenidoArchivo); | |
//System.out.println(criterio); | |
if(contenidoArchivo.indexOf(criterio)!=-1){ | |
archivosAceptados.add(arreglo.get(m)); | |
// System.out.println("Se econtro el criterio dentro del archivo"); | |
// System.out.println(arreglo.get(m)); | |
} | |
} | |
//System.out.println(res2); | |
return archivosAceptados; | |
} | |
public void filtrarYObtenerArchivos(ArrayList<String> archivosFiltrados,File ruta, String tipoArchivo){ | |
File[] ficheros = ruta.listFiles(); | |
for (int x=0;x<ficheros.length;x++){ | |
//ficheros[x]=ficheros[x].getPath(); | |
String [] resultado= ficheros[x].list( new Filtro(tipoArchivo)); | |
if (ficheros[x].isDirectory()){ | |
for(int j=0;j<resultado.length;j++){ | |
System.out.println(ficheros[x].getPath()+"\\"+resultado[j]); | |
archivosFiltrados.add(ficheros[x].getPath()+"\\"+resultado[j]); | |
//System.out.println(ficheros[x].getPath()); | |
} | |
//obtener el paht | |
filtrarYObtenerArchivos(archivosFiltrados, ficheros[x], tipoArchivo); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package mx.com.robertoleon.code; | |
import java.io.*; | |
/** | |
* Created by IntelliJ IDEA. | |
* User: Shadonwk | |
* Date: 20/07/11 | |
* Time: 12:02 PM | |
* To change this template use File | Settings | File Templates. | |
*/ | |
public class Filtro implements FilenameFilter{ | |
String extension; | |
Filtro(String extension){ | |
this.extension=extension; | |
} | |
public boolean accept(File dir, String name){ | |
return name.endsWith(extension); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package mx.com.robertoleon.gui; | |
import mx.com.robertoleon.code.BuscaCadenaEnArchivos; | |
import javax.swing.*; | |
import java.awt.*; | |
import java.awt.event.ActionEvent; | |
import java.awt.event.ActionListener; | |
/** | |
* Created by IntelliJ IDEA. | |
* User: Shadonwk | |
* Date: 20/07/11 | |
* Time: 10:39 AM | |
* To change this template use File | Settings | File Templates. | |
*/ | |
public class GUIBuscaCoincidencias extends JFrame{ | |
private Container cont; | |
private JLabel lblCriterio; | |
private JTextField txtCriterio; | |
private JLabel lblDirectorio; | |
private JTextField txtDirectorio; | |
private JButton cmdExaminar; | |
private JFileChooser directorio; | |
private JLabel lblTipoArchivo; | |
private JTextField txtTipoArchivo; | |
private JButton cmdBuscar; | |
private JButton cmdSalir; | |
private JTextArea resultados; | |
private BuscaCadenaEnArchivos buscarCadenas; | |
public GUIBuscaCoincidencias(){ | |
super("Busca cadena en Archivos by Shadonwk"); | |
cont = getContentPane(); | |
cont.setLayout(null); | |
generaGUI(); | |
setSize(new Dimension(470,355)); | |
setVisible(true); | |
} | |
private void generaGUI() { | |
lblCriterio = new JLabel("Criterio de busqueda:"); | |
lblCriterio.setBounds(10,10,150,20); | |
cont.add(lblCriterio); | |
txtCriterio = new JTextField(); | |
txtCriterio.setBounds(170,10,150,20); | |
cont.add(txtCriterio); | |
lblDirectorio = new JLabel("Buscar en:"); | |
lblDirectorio.setBounds(10,35,150,20); | |
cont.add(lblDirectorio); | |
txtDirectorio = new JTextField(); | |
txtDirectorio.setBounds(170,35,100,20); | |
cont.add(txtDirectorio); | |
cmdExaminar = new JButton("..."); | |
cmdExaminar.setBounds(280,35,40,20); | |
cont.add(cmdExaminar); | |
lblTipoArchivo = new JLabel("Tipo Archivo"); | |
lblTipoArchivo.setBounds(10,60,150,20); | |
cont.add(lblTipoArchivo); | |
txtTipoArchivo = new JTextField(); | |
txtTipoArchivo.setBounds(170,60,150,20); | |
cont.add(txtTipoArchivo); | |
resultados = new JTextArea(); | |
resultados.setBounds(10,85,305,200); | |
cont.add(resultados); | |
cmdBuscar = new JButton("Buscar"); | |
cmdBuscar.setBounds(350,10,100,20); | |
cont.add(cmdBuscar); | |
cmdSalir = new JButton("Salir"); | |
cmdSalir.setBounds(350,35,100,20); | |
cont.add(cmdSalir); | |
cmdSalir.addActionListener(new ActionListener() { | |
public void actionPerformed(ActionEvent e) { | |
System.exit(0); | |
} | |
}); | |
cmdExaminar.addActionListener(new ActionListener() { | |
public void actionPerformed(ActionEvent e) { | |
directorio = new JFileChooser(); | |
directorio.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); | |
directorio.showOpenDialog(null); | |
txtDirectorio.setText("" + directorio.getSelectedFile()); | |
} | |
}); | |
cmdBuscar.addActionListener(new ActionListener() { | |
public void actionPerformed(ActionEvent e) { | |
buscarCadenas = new BuscaCadenaEnArchivos(); | |
resultados.setText( buscarCadenas.buscar(txtCriterio.getText(),txtDirectorio.getText(),txtTipoArchivo.getText())); | |
} | |
}); | |
} | |
public static void main(String arg[]){ | |
new GUIBuscaCoincidencias(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hola amigo como estas