How do I enter a txt file inside a JtextArea java

2

I understand that it is with a cycle but I have been trying this and it does not come out, it leaves the window and the text area with the button but it does not come out what is in my file txt import javax.swing. ; import java.awt.event. ; import java.io.File; import java.io.FileReader; import java.io.BufferedReader;

import java.io.BufferedReader; import java.io.FileReader; import java.io.FileNotFoundException; import java.io.IOException; import java.io. *; import java.awt.event.ActionListener;

public class See extends JFrame {     // private Link link;     private JButton consult;     private JTextArea textArea;     private JScrollPane scrollpane1;

public Consultar(){
    super("Consulta");
    setLayout(null);
    textArea = new JTextArea();
    scrollpane1=new JScrollPane(textArea);
    scrollpane1.setBounds(40,40,300,200);
    add(scrollpane1);

    consultar = new JButton("Cosultar");
    consultar.setBounds(40,260,100,30);
    add(consultar);
}           

public static void main(String args[]) {
    File archivo;
    try{
        archivo = new File("/Users/mahlul/Desktop/Programa/ArchivoNuevo.txt");
        if (archivo.createNewFile()){
            System.out.println("");
        } 
    }catch(Throwable e){

    }
    Consultar consultarl = new Consultar();
    consultarl.setBounds(0,0,400,380);
    consultarl.setVisible(true);
}

}

    
asked by arcangel abbatu 04.11.2018 в 05:28
source

2 answers

0

It must be something like this

String codigo = new String(), path = "la ruta de tu archivo";
            File archivo = new File(path);

            FileReader fr = null;
            BufferedReader entrada = null;
            try {
                fr = new FileReader(path);
                entrada = new BufferedReader(fr);

                while(entrada.ready()){
                    codigo += entrada.readLine();
                }

            }catch(IOException e) {
                e.printStackTrace();
            }finally{
                try{                    
                    if(null != fr){   
                        fr.close();     
                    }                  
                }catch (Exception e2){ 
                    e2.printStackTrace();
                }
            }
miTextArea.setText(codigo);

Because in your example, I only see that you read it and if it does not exist, believe it, try what I sent you

    
answered by 04.11.2018 / 06:01
source
0

Hi, thanks. For helping me, I got 2 errors.

public class See extends JFrame {     // private Link link;     private JButton consult;     private JTextArea textArea;     private JScrollPane scrollpane1;

public Consultar(){
    super("Consulta");
    setLayout(null);
    textArea = new JTextArea();
    scrollpane1=new JScrollPane(textArea);
    scrollpane1.setBounds(40,40,300,200);
    add(scrollpane1);

    consultar = new JButton("Cosultar");
    consultar.setBounds(40,260,100,30);
    add(consultar);
}

    public static void main(String args[]){   
    Consultar consultarl = new Consultar();
    consultarl.setBounds(0,0,400,380);
    consultarl.setVisible(true);

    String codigo = new String(), path = "C:/UsersLuzmaDesktop/SegundoParcial/ArchivoNuevo.txt";
    File archivo = new File(path);

    FileReader fr = null;
    BufferedReader entrada = null;
    try {
        fr = new FileReader(path);
           entrada = new BufferedReader(fr);

            while(entrada.ready()){
                codigo += entrada.readLine();
            }
        }catch(IOException e) {
            e.printStackTrace();
        }finally{
            try{                    
                if(null != fr){   
                    fr.close();     
                }                  
            }catch (Exception e2){ 
                e2.printStackTrace();
            }
        }
}
miTextArea.setText(codigo);

}

    
answered by 04.11.2018 в 18:14