How to know Exactly the lines of a jlist in java?

0

As I said in another question answered I have a list that I use as a line counter and it works fine but now the problem is here when I open a file anybody tells me all the lines well but starting from 2 years ago I put them to write me too in the console and in the console write well from 1 but I do not understand why it does not do the same in the list. Part of my code here

        String archivo=new String(rutas.getText()); //obtiene el texto de la ruta especificada       
        File archivos = new File(archivo);
        editor.setText(null);
        int lotos = texto;//obtiene las lineas del archivo anterior
        int lineas = 0;//especifico las lineas desde 0
        try {
        BufferedReader leer = new BufferedReader(new FileReader(archivo));
        String linea = leer.readLine();
        while (linea != null) {
            modelolista.remove(modelolista.size() -lotos);//resto las lineas del anterior archivo
            editor.append(linea+"\n");
            linea = leer.readLine();
            lineas++;//incremento asta llegar a la ultima linea
            modelolista.addElement(lineas);//añade las lineas incrementadas
            System.out.println(lineas);//imprime en la consola las lineas
            }
        }
        catch (Exception ex) {
            Logger.getLogger(ed.class.getName()).log(Level.SEVERE, null, ex);
        }

Thanks for the help

    
asked by SirFmgames 25.11.2018 в 22:59
source

1 answer

0

My suggestion is that you undock your code. You have everything together and mixed. Use this code to read your text file and in another algorithm enter the lines of interest '

  public Stream<String> leerAchivoExterno(String rutaArchivo) {
    Stream<String> lineas = null;

    try {
        lineas = Files.lines(Paths.get(rutaArchivo), Charset.forName("UTF-8"));
    } catch (IOException ex) {
        Logger.getLogger(ArchivoImpl.class.getName()).log(Level.SEVERE, null, ex);
    }

    return lineas;
}'.

Then you can apply this algorithm to show the values extracted from the file and any other operation of interest.

// listaGrafica es un javax.swing.JList<String>
    Stream<String> lineas = Stream.of("avion","carro"); // Aqui va el algoritmo que lee las lineas del archivo. Pero por simplicidad agregue manualmente los valores
    DefaultListModel dlm = new DefaultListModel(); // Se crea una variable que almacene los valores de interés especializados para los jlist

    lineas.forEach(dlm::addElement); // se agrega cada valor
    listaGrafica.setModel(dlm); // y el modelo se le agrega al jlist

 // A partir de aqui solo muestro los valores. Puedes mejorarlo según tus requerimientos.
    System.out.println("lineas: "+listaGrafica.getModel().getSize());
    IntStream.range(0, listaGrafica.getModel().getSize())
             .forEach(indice ->{
                 System.out.println("linea = ("+indice+") valor = "+listaGrafica.getModel().getElementAt(indice));
             });
    
answered by 25.11.2018 / 23:38
source