Ver Mensaje Individual
  #1 (permalink)  
Antiguo 25/06/2014, 01:58
Avatar de maracuyeaa
maracuyeaa
 
Fecha de Ingreso: febrero-2014
Mensajes: 14
Antigüedad: 10 años, 3 meses
Puntos: 0
Buscar un contacto en un txt con java

Hola buenas a todos!!
Estoy haciendo un ejercicio de añadir contactos en un txt en Java. Al añadir el contacto en txt bien, pero cuando lo voy a buscar no me sale me dice directamente que no se encuentra el archivo, va directamente al else y no se porque, me podéis ayudar? Llevo mucho tiempo atascada aquí.

Código Java:
Ver original
  1. package projdbc1;
  2.    
  3. import java.io.BufferedReader;
  4. import java.io.BufferedWriter;
  5. import java.io.DataInputStream;
  6. import java.io.File;
  7. import java.io.FileInputStream;
  8. import java.io.FileNotFoundException;
  9. import java.io.FileReader;
  10. import java.io.FileWriter;
  11. import java.io.IOException;
  12. import java.io.InputStreamReader;
  13. import java.io.PrintWriter;
  14. import java.sql.ResultSet;
  15. import java.sql.SQLException;
  16.  
  17.  
  18.  
  19.  
  20.     public class Main {
  21.        
  22.         private static String nombre;
  23.         private static String telefono;
  24.         private static ResultSet resultado;
  25.         private static String linea;
  26.         private static java.io.BufferedWriter bufferedWriter;
  27.         static File fichero=new File("/home/Programacion/Agenda.txt");
  28.                      
  29.        
  30.         private static Contactos nuevoContacto;
  31.         public static void main(String[] args) {
  32.            
  33.              BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  34.           int opcion= 0;
  35.            
  36.             do {
  37.                 imprimeMenu();
  38.                 try {
  39.                    
  40.                 //opciones de menu
  41.                  opcion = Integer.parseInt(br.readLine());
  42.                     switch (opcion) {
  43.                         case 1:
  44.                             altaContacto();                        
  45.                             break;
  46.                         case 2:
  47.                             buscaContacto();
  48.                             break;
  49.                         case 3:
  50.                             listaContacto();
  51.                             break;
  52.                         case 4:
  53.                             System.out.println("Gracias por utilizar este servicio....");
  54.                             break;
  55.                            
  56.                         default:
  57.                             System.out.println("Tienes que seleccionar una opción del menú.");
  58.                             break;
  59.                     }
  60.                 } catch (IOException ex) {
  61.                     System.out.println("Excepción. Tienes que seleccionar una opción del menú.");
  62.                      opcion = '0';
  63.                 }
  64.             } while (!(opcion == '4'));
  65.        
  66.            
  67.         }
  68.      
  69.       //se visualiza el menu
  70.         public static void imprimeMenu() {
  71.             System.out.println("1. Crea contacto");
  72.             System.out.println("2. Busca contacto");
  73.             System.out.println("3. Listar contactos");
  74.             System.out.println("4. SALIR");
  75.         }
  76.        
  77.        
  78.      public static void altaContacto() {
  79.             BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  80.                    
  81.        
  82.            try {
  83.             bufferedWriter = new BufferedWriter(new FileWriter("Agenda.txt"));
  84.            
  85.                    
  86.             System.out.println("Nombre contacto:");
  87.             nombre = br.readLine();
  88.                    
  89.             System.out.println("Teléfono contacto:");
  90.             telefono = br.readLine();
  91.            
  92.             //sale pregunta
  93.             System.out.println("Contacto insertado gracias.¿Quieres hacer otra cosa?");
  94.            
  95.             //asi aparece en el txt
  96.             bufferedWriter.append(nombre);         
  97.              bufferedWriter.flush();
  98.              bufferedWriter.append("@"+ telefono);
  99.              bufferedWriter.flush();
  100.                                    
  101.           } catch (IOException e) {
  102.            
  103.              e.printStackTrace(); }
  104.          
  105.        
  106.      }
  107.  
  108.        
  109.            
  110.          
  111.        public static void buscaContacto() throws IOException {
  112.             System.out.println("Inserta el texto a buscar:");
  113.             try {  
  114.                    
  115.                    if(fichero.exists()){  
  116.                        
  117.                       BufferedReader br= new BufferedReader(new FileReader("Agenda.txt"));  
  118.                        System.out.println("Leyendo Fichero");  
  119.                        
  120.                       //Lee el fichero linea a linea hasta llegar a la ultima
  121.                        while((linea= br.readLine())!=null) {  
  122.                        //Imprime la linea leida    
  123.                        System.out.println(linea);                
  124.                        }  
  125.                        System.out.println("*Fin Leer Fichero");  
  126.                        
  127.                        br.close();  
  128.                      }else{  
  129.                        System.out.println("Fichero No Existe");  
  130.                      }  
  131.                } catch (Exception ex) {  
  132.                      
  133.                     System.out.println(ex.getMessage());  
  134.                }  
  135.              }  
  136.        
  137.        
  138.        
  139. //nos sale la lista de contactos de nuestro agenda.txt
  140.        
  141.         public static void listaContacto() {
  142.             System.out.println("Listados de contactos:");
  143.            
  144.                 try {
  145.                 while (resultado.next()) {
  146.                
  147.                  
  148.                 String nombre = resultado.getString("nombre");
  149.                 String telefono = resultado.getString("telefono");
  150.                
  151.                 //se imprime el nombre y el telefono
  152.                 System.out.println(nombre + " " + telefono);
  153.                 }
  154.                 } catch (SQLException ex) {
  155.                 System.out.println("cinco " + ex.getMessage());
  156.                 }
  157.                 }
  158.  
  159.        
  160.  
  161.        
  162.         public static void setNombre(String nombre) {
  163.             Main.nombre = nombre;
  164.         }
  165.        
  166.         public static String setNombre() {
  167.            
  168.             return nombre;
  169.         }
  170.        
  171.            
  172.  
  173.  
  174.         public static void setTelefono(String telefono) {
  175.             Main.telefono = telefono;
  176.         }
  177.  
  178.         public static String getTelefono() {
  179.             return telefono;
  180.         }
  181.  
  182.         public static ResultSet getResultado() {
  183.             return resultado;
  184.         }
  185.  
  186.         public static void setResultado(ResultSet resultado) {
  187.             Main.resultado = resultado;
  188.         }
  189.  
  190.         public static Contactos getNuevoContacto() {
  191.             return nuevoContacto;
  192.         }
  193.  
  194.         public static void setNuevoContacto(Contactos nuevoContacto) {
  195.             Main.nuevoContacto = nuevoContacto;
  196.         }
  197.  
  198.        
  199.     }