Ver Mensaje Individual
  #2 (permalink)  
Antiguo 15/12/2006, 20:17
Avatar de ah_puch
ah_puch
 
Fecha de Ingreso: agosto-2005
Ubicación: Ecatepec, México
Mensajes: 180
Antigüedad: 18 años, 8 meses
Puntos: 3
Re: Problemas para abrir un archivo

hola amigo si estas utilizando java Swing por que no lo intentas con el JFileChooser espero te sea util aqui te dejo un ejemplo sencillo sobre JFileChooser:

Código PHP:
import java.io.*;
import java.text.*;
import java.lang.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;

public class 
mi_file{
    public static 
void main(String Args[]){
        new 
XServer().Make();
    }
}

class 
XServer extends JFrame implements ActionListener{
    
String ruta;    
    
JButton cmd_Abrir = new JButton("Abrir Archivo");
    
JButton cmd_Guardar = new JButton("Guardar Archivo");
    
JTextArea Txt_Info = new JTextArea();
    
JPanel Mi_Panel = new JPanel();
    
Container coloca getContentPane();
    public 
void Make(){
        
        
Mi_Panel.setLayout(null);
        
        
cmd_Abrir.setBounds(80,1,120,25);
            
cmd_Abrir.setVisible(true);
            
Mi_Panel.add(cmd_Abrir);
        
        
cmd_Guardar.setBounds(185,1,140,25);
            
cmd_Guardar.setVisible(true);
            
Mi_Panel.add(cmd_Guardar);
        
        
        
Txt_Info.setBounds(1,30,390,390);
            
Txt_Info.setVisible(true);
            
Mi_Panel.add(Txt_Info);
        
        
coloca.add(Mi_Panel);
        
        
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
setSize(400,400);
        
setVisible(true);
        
        
cmd_Abrir.addActionListener(this);
        
cmd_Guardar.addActionListener(this);
    
    }

    public 
void actionPerformed(ActionEvent e){    
    
    
/************************Menu Abrir*****************************/
        
if(e.getSource()==cmd_Abrir){
            
JFileChooser dialog = new JFileChooser();
            
Container parent =cmd_Abrir.getParent();
            
String linea;
            
int mychoose;
                
dialog.setSize(400,400);
                
mychoose dialog.showOpenDialog(parent);
                if(
mychoose == JFileChooser.APPROVE_OPTION){
                    
ruta String.valueOf(dialog.getSelectedFile().getAbsoluteFile());
                    
File MyFile = new File (ruta); 
                    try{
                        
BufferedReader input = new BufferedReader(new
                        
FileReader(ruta));
                        while ((
linea=input.readLine())!=null){
                            
Txt_Info.setText(String.valueOf(Txt_Info.getText()) + "\n" +linea);
                            
linea ="";
                          }
                    } catch(
IOException ex){ System.out.println("No se pudo abrir el archivo");}  
                  }
        }
    
/*******************************************| Menu Guardar |********************************************/    
    
        
if(e.getSource()==cmd_Guardar){
            
JFileChooser dialog = new JFileChooser();
            
Container parent cmd_Guardar.getParent();
            
int mychoose;
            
dialog.setSize(400,400);
            
mychoose dialog.showSaveDialog(parent);
            if(
mychoose == JFileChooser.APPROVE_OPTION){
                if (
ruta!=""){
                    
ruta String.valueOf(dialog.getSelectedFile().getAbsoluteFile());
                }
            
                
File MyFile = new File (ruta); 
            
                try{
                       
FileWriter writer=new FileWriter(ruta,true);
                       
writer.write(String.valueOf(Txt_Info.getText()));
                       
writer.close();
                 } catch(
IOException ex){ System.out.print("Error");}
              }
        }
    }

__________________
La programacion no es un trabajo, es un Arte
http://www.purodev.blogspot.com