+ All Categories
Home > Documents > EnunciaEjercicidos Ejercicios de Ficheros 1texto

EnunciaEjercicidos Ejercicios de Ficheros 1texto

Date post: 17-Jan-2016
Category:
Upload: ines-garcia
View: 9 times
Download: 0 times
Share this document with a friend
Description:
prácticas ficheros Java
44
EJERCICIOS DE FICHEROS: 1.- Crear un archivo de texto:
Transcript
Page 1: EnunciaEjercicidos Ejercicios de Ficheros 1texto

EJERCICIOS DE FICHEROS:

1.- Crear un archivo de texto:

Page 2: EnunciaEjercicidos Ejercicios de Ficheros 1texto
Page 3: EnunciaEjercicidos Ejercicios de Ficheros 1texto
Page 4: EnunciaEjercicidos Ejercicios de Ficheros 1texto

package creararchivotexto;import java.io.FileWriter;import java.io.IOException;import java.io.PrintWriter;import java.util.logging.Level;import java.util.logging.Logger;import javax.swing.JOptionPane;import javax.swing.JTextArea;

public class ControlArchivo { public void EscribirArchivo( String nombre, String texto) { FileWriter fw=null; PrintWriter out; try { fw=new FileWriter (nombre); out=new PrintWriter(fw); out.println(texto); JOptionPane.showMessageDialog(null, "Texto ingresado correctamente."); } catch (IOException ex) { Logger.getLogger(ControlArchivo.class.getName()).log(Level.SEVERE, null, ex); } finally{ try { if (fw!=null){ fw.close(); } } catch (IOException ex) { Logger.getLogger(ControlArchivo.class.getName()).log(Level.SEVERE, null, ex); } } }}package creararchivotexto;

import javax.swing.JFileChooser;import javax.swing.JOptionPane;

public class Crear extends javax.swing.JFrame { String nombrefich=""; ControlArchivo miarchivo= new ControlArchivo(); public Crear() { initComponents(); jPanel1.setVisible(true); jPanel2.setVisible(false); btnFin.setVisible(false); }

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

Page 5: EnunciaEjercicidos Ejercicios de Ficheros 1texto

int resp; resp=elegirFichero.showSaveDialog(jPanel1); if(resp==JFileChooser.APPROVE_OPTION){ JOptionPane.showMessageDialog(null, elegirFichero.getSelectedFile().toString()); nombrefich=elegirFichero.getSelectedFile().toString(); jPanel1.setVisible(false); jPanel2.setVisible(true); }else if (resp==JFileChooser.CANCEL_OPTION){ JOptionPane.showMessageDialog(null, "Se pulsó la opción Cancelar."); } }

private void btnCrearActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: miarchivo.EscribirArchivo(nombrefich, AreaTexto.getText()); jPanel2.setVisible(false); btnFin.setVisible(true); }

private void btnFinActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: System.exit(0); } ………. // Variables declaration - do not modify private javax.swing.JTextArea AreaTexto; private javax.swing.JButton btnCrear; private javax.swing.JButton btnFin; private javax.swing.JButton btnNombre; private javax.swing.JFileChooser elegirFichero; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JPanel jPanel1; private javax.swing.JPanel jPanel2; private javax.swing.JScrollPane jScrollPane1; // End of variables declaration }

Page 6: EnunciaEjercicidos Ejercicios de Ficheros 1texto

2.- Mostrar un archivo de texto:

Page 7: EnunciaEjercicidos Ejercicios de Ficheros 1texto
Page 8: EnunciaEjercicidos Ejercicios de Ficheros 1texto

package mostrararchivotexto;

import java.io.BufferedReader;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.io.PrintWriter;import java.util.logging.Level;import java.util.logging.Logger;import javax.swing.JOptionPane;import javax.swing.JTextArea;

public class ControlArchivo { public void EscribirArchivo( String nombre, String texto) { FileWriter fw=null; PrintWriter out; try { fw=new FileWriter (nombre); out=new PrintWriter(fw); out.println(texto); JOptionPane.showMessageDialog(null, "Texto ingresado correctamente."); } catch (IOException ex) { Logger.getLogger(ControlArchivo.class.getName()).log(Level.SEVERE, null, ex); } finally{ try { if (fw!=null){ fw.close(); } } catch (IOException ex) { Logger.getLogger(ControlArchivo.class.getName()).log(Level.SEVERE, null, ex); } } } public void MostrarArchivo( String nombre, JTextArea AreaTexto) { File f=new File(nombre); if (f.exists()){ FileReader fr = null; try { fr = new FileReader(nombre); BufferedReader bf=new BufferedReader(fr); String cad; while((cad=bf.readLine())!=null) {

Page 9: EnunciaEjercicidos Ejercicios de Ficheros 1texto

AreaTexto.append(cad); AreaTexto.append("\n"); } } catch (IOException ex) { Logger.getLogger(ControlArchivo.class.getName()).log(Level.SEVERE, null, ex); } finally { try { fr.close(); } catch (IOException ex) { Logger.getLogger(ControlArchivo.class.getName()).log(Level.SEVERE, null, ex); } } } else{ JOptionPane.showMessageDialog(null, "El archivo no existe."); } }}

package mostrararchivotexto;

import javax.swing.JFileChooser;import javax.swing.JOptionPane;

public class Mostrar extends javax.swing.JFrame { String nombrefich=""; ControlArchivo miarchivo= new ControlArchivo(); public Mostrar() { initComponents(); jPanel1.setVisible(true); jPanel2.setVisible(false); btnFin.setVisible(false); }

private void btnNombreActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: int resp; resp=elegirFichero.showOpenDialog(jPanel1); if(resp==JFileChooser.APPROVE_OPTION){ //JOptionPane.showMessageDialog(null, elegirFichero.getSelectedFile().toString()); jPanel1.setVisible(false); nombrefich=elegirFichero.getSelectedFile().toString(); jPanel2.setVisible(true); fichMostrado.setText(nombrefich);

Page 10: EnunciaEjercicidos Ejercicios de Ficheros 1texto

miarchivo.MostrarArchivo(nombrefich, AreaTexto); btnFin.setVisible(true);

}else if (resp==JFileChooser.CANCEL_OPTION){ JOptionPane.showMessageDialog(null, "Se pulsó la opción Cancelar."); }

}

private void btnFinActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: System.exit(0); }

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

// Variables declaration - do not modify private javax.swing.JTextArea AreaTexto; private javax.swing.JButton btnFin; private javax.swing.JButton btnNombre; private javax.swing.JFileChooser elegirFichero; private javax.swing.JTextField fichMostrado; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JPanel jPanel1; private javax.swing.JPanel jPanel2; private javax.swing.JScrollPane jScrollPane1; // End of variables declaration }

Page 11: EnunciaEjercicidos Ejercicios de Ficheros 1texto

3.- Contar vocales en un archivo de texto:

Page 12: EnunciaEjercicidos Ejercicios de Ficheros 1texto
Page 13: EnunciaEjercicidos Ejercicios de Ficheros 1texto
Page 14: EnunciaEjercicidos Ejercicios de Ficheros 1texto

public class ControlArchivo { public void EscribirArchivo( String nombre, String texto) { FileWriter fw=null; PrintWriter out; try { fw=new FileWriter (nombre); out=new PrintWriter(fw); out.println(texto); JOptionPane.showMessageDialog(null, "Texto ingresado correctamente."); } catch (IOException ex) { Logger.getLogger(ControlArchivo.class.getName()).log(Level.SEVERE, null, ex); } finally{ try { if (fw!=null){ fw.close(); } } catch (IOException ex) { Logger.getLogger(ControlArchivo.class.getName()).log(Level.SEVERE, null, ex); } } } public void MostrarArchivo( String nombre, JTextArea AreaTexto) { File f=new File(nombre); if (f.exists()){ FileReader fr = null; try { fr = new FileReader(nombre); BufferedReader bf=new BufferedReader(fr); String cad; while((cad=bf.readLine())!=null) { AreaTexto.append(cad); AreaTexto.append("\n"); } } catch (IOException ex) { Logger.getLogger(ControlArchivo.class.getName()).log(Level.SEVERE, null, ex); } finally { try { fr.close(); } catch (IOException ex) { Logger.getLogger(ControlArchivo.class.getName()).log(Level.SEVERE, null, ex); } } } else{

Page 15: EnunciaEjercicidos Ejercicios de Ficheros 1texto

JOptionPane.showMessageDialog(null, "El archivo no existe."); } } public int[] ContarVocales( String nombre) { int cont[]={0,0,0,0,0}; File f=new File(nombre); if (f.exists()){ FileReader fr = null; try { fr = new FileReader(nombre); int c=fr.read(); while(c!=-1) { switch(c) { case 'a':cont[0]++;break; case 'e':cont[1]++;break; case 'i':cont[2]++;break; case 'o':cont[3]++;break; case 'u':cont[4]++;break; case 'A':cont[0]++;break; case 'E':cont[1]++;break; case 'I':cont[2]++;break; case 'O':cont[3]++;break; case 'U':cont[4]++;break; } c=fr.read(); } } catch (IOException ex) { Logger.getLogger(ControlArchivo.class.getName()).log(Level.SEVERE, null, ex); } finally { try { fr.close(); } catch (IOException ex) { Logger.getLogger(ControlArchivo.class.getName()).log(Level.SEVERE, null, ex); } } } else{ JOptionPane.showMessageDialog(null, "El archivo no existe."); } return cont; }}

Page 16: EnunciaEjercicidos Ejercicios de Ficheros 1texto

package pkg3.contarvocales;

import javax.swing.JFileChooser;import javax.swing.JOptionPane;import javax.swing.JTextArea;

public class ContarVocales extends javax.swing.JFrame {

String nombrefich=""; ControlArchivo miarchivo= new ControlArchivo(); public ContarVocales() { initComponents(); jPanel1.setVisible(true); jPanel2.setVisible(false); btnFin.setVisible(false); }

private void btnNombreActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: int resp; resp=elegirFichero.showOpenDialog(jPanel1); if(resp==JFileChooser.APPROVE_OPTION){ //JOptionPane.showMessageDialog(null, elegirFichero.getSelectedFile().toString()); jPanel1.setVisible(false); nombrefich=elegirFichero.getSelectedFile().toString(); jPanel2.setVisible(true); fichMostrado.setText(nombrefich); miarchivo.MostrarArchivo(nombrefich, AreaTexto); //btnFin.setVisible(true);

}else if (resp==JFileChooser.CANCEL_OPTION){ JOptionPane.showMessageDialog(null, "Se pulsó la opción Cancelar."); } }

private void btnContarActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: int v[]=miarchivo.ContarVocales( nombrefich); contA.setText(Integer.toString(v[0])); contE.setText(Integer.toString(v[1])); contI.setText(Integer.toString(v[2])); contO.setText(Integer.toString(v[3])); contU.setText(Integer.toString(v[4])); btnFin.setVisible(true); }

private void btnFinActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: System.exit(0);

Page 17: EnunciaEjercicidos Ejercicios de Ficheros 1texto

}

public static void main(String args[]) {

……………………………………………………

} // Variables declaration - do not modify private javax.swing.JLabel A; private javax.swing.JTextArea AreaTexto; private javax.swing.JLabel E; private javax.swing.JLabel I; private javax.swing.JLabel O; private javax.swing.JLabel U; private javax.swing.JButton btnContar; private javax.swing.JButton btnFin; private javax.swing.JButton btnNombre; private javax.swing.JTextField contA; private javax.swing.JTextField contE; private javax.swing.JTextField contI; private javax.swing.JTextField contO; private javax.swing.JTextField contU; private javax.swing.JFileChooser elegirFichero; private javax.swing.JTextField fichMostrado; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JPanel jPanel1; private javax.swing.JPanel jPanel2; private javax.swing.JScrollPane jScrollPane1; // End of variables declaration }

Page 18: EnunciaEjercicidos Ejercicios de Ficheros 1texto

4.- Contar palabras en un archivo de texto:

Page 19: EnunciaEjercicidos Ejercicios de Ficheros 1texto
Page 20: EnunciaEjercicidos Ejercicios de Ficheros 1texto

package pkg4.contarpalabras;

import java.io.BufferedReader;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.io.PrintWriter;import java.util.StringTokenizer;import java.util.logging.Level;import java.util.logging.Logger;import javax.swing.JOptionPane;import javax.swing.JTextArea;

public class ControlArchivo { public void EscribirArchivo( String nombre, String texto) { FileWriter fw=null; PrintWriter out; try { fw=new FileWriter (nombre); out=new PrintWriter(fw); out.println(texto); JOptionPane.showMessageDialog(null, "Texto ingresado correctamente."); } catch (IOException ex) { Logger.getLogger(ControlArchivo.class.getName()).log(Level.SEVERE, null, ex); } finally{ try { if (fw!=null){ fw.close(); } } catch (IOException ex) { Logger.getLogger(ControlArchivo.class.getName()).log(Level.SEVERE, null, ex); } } } public void MostrarArchivo( String nombre, JTextArea AreaTexto) { File f=new File(nombre); if (f.exists()){ FileReader fr = null; try { fr = new FileReader(nombre); BufferedReader bf=new BufferedReader(fr); String cad; while((cad=bf.readLine())!=null) {

Page 21: EnunciaEjercicidos Ejercicios de Ficheros 1texto

AreaTexto.append(cad); AreaTexto.append("\n"); } } catch (IOException ex) { Logger.getLogger(ControlArchivo.class.getName()).log(Level.SEVERE, null, ex); } finally { try { fr.close(); } catch (IOException ex) { Logger.getLogger(ControlArchivo.class.getName()).log(Level.SEVERE, null, ex); } } } else{ JOptionPane.showMessageDialog(null, "El archivo no existe."); } } public int[] ContarVocales( String nombre) { int cont[]={0,0,0,0,0}; File f=new File(nombre); if (f.exists()){ FileReader fr = null; try { fr = new FileReader(nombre); int c=fr.read(); while(c!=-1) { switch(c) { case 'a':cont[0]++;break; case 'e':cont[1]++;break; case 'i':cont[2]++;break; case 'o':cont[3]++;break; case 'u':cont[4]++;break; case 'A':cont[0]++;break; case 'E':cont[1]++;break; case 'I':cont[2]++;break; case 'O':cont[3]++;break; case 'U':cont[4]++;break; } c=fr.read(); } } catch (IOException ex) { Logger.getLogger(ControlArchivo.class.getName()).log(Level.SEVERE, null, ex); } finally { try {

Page 22: EnunciaEjercicidos Ejercicios de Ficheros 1texto

fr.close(); } catch (IOException ex) { Logger.getLogger(ControlArchivo.class.getName()).log(Level.SEVERE, null, ex); } } } else{ JOptionPane.showMessageDialog(null, "El archivo no existe."); } return cont; } public int ContarPalabras( String nombre) { int cont=0; File f=new File(nombre); if (f.exists()){ FileReader fr = null; try { fr = new FileReader(nombre); BufferedReader bf= new BufferedReader(fr); StringTokenizer st; String linea,palabra; while((linea=bf.readLine())!=null) { st = new StringTokenizer(linea); while (st.hasMoreTokens()) { palabra=st.nextToken(); cont++; } } //System.out.println("Total palabras= "+cont); return cont; } catch (IOException ex) { Logger.getLogger(ControlArchivo.class.getName()).log(Level.SEVERE, null, ex); } finally { try { fr.close(); } catch (IOException ex) { Logger.getLogger(ControlArchivo.class.getName()).log(Level.SEVERE, null, ex); } } } else{ JOptionPane.showMessageDialog(null, "El archivo no existe."); }

Page 23: EnunciaEjercicidos Ejercicios de Ficheros 1texto

return cont; } }

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

import javax.swing.JFileChooser;import javax.swing.JOptionPane;

public class ContarPalabras extends javax.swing.JFrame {

String nombrefich=""; ControlArchivo miarchivo= new ControlArchivo(); public ContarPalabras() { initComponents(); jPanel1.setVisible(true); jPanel2.setVisible(false); btnFin.setVisible(false); }

private void btnNombreActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: int resp; resp=elegirFichero.showOpenDialog(jPanel1); if(resp==JFileChooser.APPROVE_OPTION){ //JOptionPane.showMessageDialog(null, elegirFichero.getSelectedFile().toString()); jPanel1.setVisible(false); nombrefich=elegirFichero.getSelectedFile().toString(); jPanel2.setVisible(true); fichMostrado.setText(nombrefich); miarchivo.MostrarArchivo(nombrefich, AreaTexto); //btnFin.setVisible(true);

}else if (resp==JFileChooser.CANCEL_OPTION){ JOptionPane.showMessageDialog(null, "Se pulsó la opción Cancelar."); } }

private void btnContarActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: int v=miarchivo.ContarPalabras( nombrefich); contador.setText(Integer.toString(v)); btnFin.setVisible(true);

Page 24: EnunciaEjercicidos Ejercicios de Ficheros 1texto

}

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

private void btnFinActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: System.exit(0); }

public static void main(String args[]) {

. . . . . . . . . . } // Variables declaration - do not modify private javax.swing.JTextArea AreaTexto; private javax.swing.JLabel NumPalabras; private javax.swing.JButton btnContar; private javax.swing.JButton btnFin; private javax.swing.JButton btnNombre; private javax.swing.JTextField contador; private javax.swing.JFileChooser elegirFichero; private javax.swing.JTextField fichMostrado; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JPanel jPanel1; private javax.swing.JPanel jPanel2; private javax.swing.JScrollPane jScrollPane1; // End of variables declaration }

Page 25: EnunciaEjercicidos Ejercicios de Ficheros 1texto

5.- Hacer una aplicación con un menú que incluya:

Crear archivo. Mostrar archivo. Contar vocales. Contar palabras.

Page 26: EnunciaEjercicidos Ejercicios de Ficheros 1texto
Page 27: EnunciaEjercicidos Ejercicios de Ficheros 1texto
Page 28: EnunciaEjercicidos Ejercicios de Ficheros 1texto

package pkg6.menutexto;

public class menuTexto extends javax.swing.JFrame {

String nombrefich=""; ControlArchivo miarchivo= new ControlArchivo(); public menuTexto() { initComponents(); }

private void CrearActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: Crear c=new Crear(); c.setVisible(true); }

private void MostrarActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: Mostrar m=new Mostrar(); m.setVisible(true); }

private void ContarVocalesActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: ContarVocales v=new ContarVocales(); v.setVisible(true); }

private void ContarPalabrasActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: ContarPalabras p=new ContarPalabras(); p.setVisible(true); }

private void salirActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: System.exit(0); }

public static void main(String args[]) { . . . . . . . . . }

// Variables declaration - do not modify private javax.swing.JMenu Archivo; private javax.swing.JMenuItem ContarPalabras;

Page 29: EnunciaEjercicidos Ejercicios de Ficheros 1texto

private javax.swing.JMenuItem ContarVocales; private javax.swing.JMenuItem Crear; private javax.swing.JMenuItem Mostrar; private javax.swing.JMenu Operaciones; private javax.swing.JMenu Salir; private javax.swing.JMenuBar barraMenus; private javax.swing.JMenuItem salir; // End of variables declaration }

Page 30: EnunciaEjercicidos Ejercicios de Ficheros 1texto

6.- Hacer una aplicación que diseñe un programa que copie un archivo de texto en otro, la información se pasará encriptada, por cada palabra hacer la transformación siguiente:

Palabra origen:

Palabra destino:

Le damos la vuelta excepto a la primera y última letra. Sólo se hará con palabras de longitud mayor a 4.

Palabra origen:

Palabra destino:

p a t a t a

p t a t a a

c a r p e t a

c t e p r a a

Page 31: EnunciaEjercicidos Ejercicios de Ficheros 1texto

A continuación muestra el archivo encriptado.

Page 32: EnunciaEjercicidos Ejercicios de Ficheros 1texto

import java.io.BufferedReader;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.io.PrintWriter;import java.util.Random;import java.util.StringTokenizer;import java.util.logging.Level;import java.util.logging.Logger;import javax.swing.JOptionPane;import javax.swing.JTextArea;

public class ControlArchivo { public void EscribirArchivo( String nombre, String texto) { FileWriter fw=null; PrintWriter out; try { fw=new FileWriter (nombre); out=new PrintWriter(fw); out.println(texto); JOptionPane.showMessageDialog(null, "Texto ingresado correctamente."); } catch (IOException ex) { Logger.getLogger(ControlArchivo.class.getName()).log(Level.SEVERE, null, ex); } finally{ try { if (fw!=null){ fw.close(); } } catch (IOException ex) { Logger.getLogger(ControlArchivo.class.getName()).log(Level.SEVERE, null, ex); } } } public void MostrarArchivo( String nombre, JTextArea AreaTexto) { File f=new File(nombre); if (f.exists()){ FileReader fr = null; try { fr = new FileReader(nombre); BufferedReader bf=new BufferedReader(fr); String cad; while((cad=bf.readLine())!=null) {

Page 33: EnunciaEjercicidos Ejercicios de Ficheros 1texto

AreaTexto.append(cad); AreaTexto.append("\n"); } } catch (IOException ex) { Logger.getLogger(ControlArchivo.class.getName()).log(Level.SEVERE, null, ex); } finally { try { fr.close(); } catch (IOException ex) { Logger.getLogger(ControlArchivo.class.getName()).log(Level.SEVERE, null, ex); } } } else{ JOptionPane.showMessageDialog(null, "El archivo no existe."); } } public int[] ContarVocales( String nombre) { int cont[]={0,0,0,0,0}; File f=new File(nombre); if (f.exists()){ FileReader fr = null; try { fr = new FileReader(nombre); int c=fr.read(); while(c!=-1) { switch(c) { case 'a':cont[0]++;break; case 'e':cont[1]++;break; case 'i':cont[2]++;break; case 'o':cont[3]++;break; case 'u':cont[4]++;break; case 'A':cont[0]++;break; case 'E':cont[1]++;break; case 'I':cont[2]++;break; case 'O':cont[3]++;break; case 'U':cont[4]++;break; } c=fr.read(); } } catch (IOException ex) { Logger.getLogger(ControlArchivo.class.getName()).log(Level.SEVERE, null, ex); } finally { try {

Page 34: EnunciaEjercicidos Ejercicios de Ficheros 1texto

fr.close(); } catch (IOException ex) { Logger.getLogger(ControlArchivo.class.getName()).log(Level.SEVERE, null, ex); } } } else{ JOptionPane.showMessageDialog(null, "El archivo no existe."); } return cont; } public int ContarPalabras( String nombre) { int cont=0; File f=new File(nombre); if (f.exists()){ FileReader fr = null; try { fr = new FileReader(nombre); BufferedReader bf= new BufferedReader(fr); StringTokenizer st; String linea,palabra; while((linea=bf.readLine())!=null) { st = new StringTokenizer(linea); while (st.hasMoreTokens()) { palabra=st.nextToken(); cont++; } } //System.out.println("Total palabras= "+cont); } catch (IOException ex) { Logger.getLogger(ControlArchivo.class.getName()).log(Level.SEVERE, null, ex); } finally { try { fr.close(); } catch (IOException ex) { Logger.getLogger(ControlArchivo.class.getName()).log(Level.SEVERE, null, ex); } } } else{ JOptionPane.showMessageDialog(null, "El archivo no existe.");

Page 35: EnunciaEjercicidos Ejercicios de Ficheros 1texto

} return cont; } public static String mixpalabras(String inicio){ char[] vec1 = inicio.toCharArray(); char[] vec2 = inicio.toCharArray(); ; int j = vec1.length-1; for(int i=1;i<j;i++) { vec2[vec1.length -i-1] = vec1[i]; } String nuevapalabra = String.valueOf(vec2); return nuevapalabra; }

public void Encriptar( String origen, String destino) { File f=new File(origen); if (f.exists()){ FileReader fr = null; FileWriter fw=null; try { fr = new FileReader(origen); BufferedReader bf= new BufferedReader(fr); fw = new FileWriter(destino); PrintWriter out=new PrintWriter(fw); StringTokenizer st; String linea,palabra,encriptada; while((linea=bf.readLine())!=null) { System.out.println(linea); st = new StringTokenizer(linea); while (st.hasMoreTokens()) { palabra=st.nextToken(); encriptada=mixpalabras(palabra); out.print(encriptada+" "); System.out.println(palabra+" "+encriptada); } out.print("\n"); } out.flush(); out.close();

Page 36: EnunciaEjercicidos Ejercicios de Ficheros 1texto

} catch (IOException ex) { Logger.getLogger(ControlArchivo.class.getName()).log(Level.SEVERE, null, ex); } finally { try { fr.close(); } catch (IOException ex) { Logger.getLogger(ControlArchivo.class.getName()).log(Level.SEVERE, null, ex); } } } else{ JOptionPane.showMessageDialog(null, "El archivo no existe."); } } }

import javax.swing.JFileChooser;import javax.swing.JOptionPane;

public class Encriptar extends javax.swing.JFrame {

String nombrefichOrigen=""; String nombrefichDestino=""; ControlArchivo miarchivo= new ControlArchivo(); public Encriptar() { initComponents(); Panel1.setVisible(true); btnNombreDestino.setVisible(false); Panel2.setVisible(false); Panel3.setVisible(false); //btnFin.setVisible(false); }

private void btnEncriptarActionPerformed(java.awt.event.ActionEvent evt) { miarchivo.Encriptar(nombrefichOrigen, nombrefichDestino); Panel3.setVisible(true); fichMostrado2.setText(nombrefichDestino); miarchivo.MostrarArchivo(nombrefichDestino, AreaTexto2); System.out.println("origen;¡: "+nombrefichOrigen+" destino: "+nombrefichDestino); }

Page 37: EnunciaEjercicidos Ejercicios de Ficheros 1texto

private void btnNombreDestinoActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: int resp; resp=elegirFichero.showSaveDialog(Panel1); if(resp==JFileChooser.APPROVE_OPTION){ btnNombreOrigen.setVisible(false); nombrefichDestino=elegirFichero.getSelectedFile().toString(); Panel1.setVisible(false); Panel2.setVisible(true); fichMostrado.setText(nombrefichOrigen); miarchivo.MostrarArchivo(nombrefichOrigen, AreaTexto); //btnFin.setVisible(true);

}else if (resp==JFileChooser.CANCEL_OPTION){ JOptionPane.showMessageDialog(null, "Se pulsó la opción Cancelar."); } }

private void btnNombreOrigenActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: int resp; resp=elegirFichero.showOpenDialog(Panel1); if(resp==JFileChooser.APPROVE_OPTION){ //JOptionPane.showMessageDialog(null, elegirFichero.getSelectedFile().toString()); nombrefichOrigen=elegirFichero.getSelectedFile().toString(); btnNombreDestino.setVisible(true); }else if (resp==JFileChooser.CANCEL_OPTION){ JOptionPane.showMessageDialog(null, "Se pulsó la opción Cancelar."); } }

private void btnFinActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: System.exit(0); }

public static void main(String args[]) {

. . . . . . .

} // Variables declaration - do not modify private javax.swing.JTextArea AreaTexto; private javax.swing.JTextArea AreaTexto1; private javax.swing.JTextArea AreaTexto2; private javax.swing.JPanel Panel1; private javax.swing.JPanel Panel2; private javax.swing.JPanel Panel3; private javax.swing.JButton btnEncriptar;

Page 38: EnunciaEjercicidos Ejercicios de Ficheros 1texto

private javax.swing.JButton btnEncriptar1; private javax.swing.JButton btnFin; private javax.swing.JButton btnNombreDestino; private javax.swing.JButton btnNombreOrigen; private javax.swing.JFileChooser elegirFichero; private javax.swing.JTextField fichMostrado; private javax.swing.JTextField fichMostrado1; private javax.swing.JTextField fichMostrado2; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JPanel jPanel3; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JScrollPane jScrollPane2; private javax.swing.JScrollPane jScrollPane3; // End of variables declaration }


Recommended