Read file by blocks How to optimize it? see the end of the post

0

Good morning dear community. I am a platform application development student and I have encountered a problem in class that brings me some headaches, to see if someone can help me or give me a clue.

I have the following file (sudokus.txt):

If you notice, it always follows the same pattern, it starts with% and then we have the first number of the row, in the example image = 7, it would be the level of difficulty, the second value would be a string that would be a description of the difficulty = Medium (Level 7), then it jumps of line and we have that all the line is a chain that would be the problem and the following the solution.

If you realize, we are talking about an application that talks about sudokus, I leave an image of the model.

package model;

import java.util.HashSet;
import java.util.Set;

public class Sudoku implements java.io.Serializable {

    private Integer id;
    private Integer nivel;
    private String descripcion;
    private String problema;
    private String solucion;
    private Set historials = new HashSet(0);

    public Sudoku() {
    }

    /**
     * @param nivel
     * @param descripcion
     * @param problema
     * @param solucion
     */
    public Sudoku(Integer nivel, String descripcion, String problema, String solucion) {
        this.nivel = nivel;
        this.descripcion = descripcion;
        this.problema = problema;
        this.solucion = solucion;
    }

    /**
     * @param nivel
     * @param descripcion
     * @param problema
     * @param solucion
     * @param historials
     */
    public Sudoku(Integer nivel, String descripcion, String problema, String solucion, Set historials) {
        this.nivel = nivel;
        this.descripcion = descripcion;
        this.problema = problema;
        this.solucion = solucion;
        this.historials = historials;
    }

    public Integer getId() {
        return this.id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getNivel() {
        return this.nivel;
    }

    public void setNivel(Integer nivel) {
        this.nivel = nivel;
    }

    public String getDescripcion() {
        return this.descripcion;
    }

    public void setDescripcion(String descripcion) {
        this.descripcion = descripcion;
    }

    public String getProblema() {
        return this.problema;
    }

    public void setProblema(String problema) {
        this.problema = problema;
    }

    public String getSolucion() {
        return this.solucion;
    }

    public void setSolucion(String solucion) {
        this.solucion = solucion;
    }

    public Set getHistorials() {
        return this.historials;
    }

    public void setHistorials(Set historials) {
        this.historials = historials;
    }



    @Override
    public String toString() {
        //TODO Care with historial
        return "Sudoku [id=" + id + ", nivel=" + nivel + ", descripcion=" + descripcion + ", problema=" + problema
                + ", solucion=" + solucion + ", historials=" + historials + "]";
    }
}

The problem basically is that I have to split the file as if by blocks, where every 3 rows would start a new Sudoku object.

I'll go through the file and everything, but I'm stuck as soon as I have to split it and create the file.

I have tried some things and the concept is clear, but I do not want to do a 3 module with an accountant because it is ugly and if there is a blank line the program would generate an error, etc.

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import globals.Constants;
import model.Sudoku;;

public class TrasladoORM {

    public static void main(String[] args) {
        Sudoku sudokuToAdd;
        try (BufferedReader br = new BufferedReader(new FileReader(Constants.FileNames.SUDOKU_LIST.getName()))) {
            String line;
            List<Sudoku> SudokuList = new ArrayList<>();

            while ((line = br.readLine()) != null) {
                //partir por % para empezar nuevo objeto
                //guardar el: [0] nivel [1] descripcion
                //siguiente line: [0] problema
                //siguiente line: [0] solucion
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Code in use (possibly the dirtiest thing I've ever done):

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import exception.SudokuException;
import globals.Constants;
import model.Sudoku;
import persistence.SudokuDAO;;

public class TrasladoORM {

    public static void main(String[] args) {
        SudokuDAO dao = new SudokuDAO();
        try (BufferedReader br = new BufferedReader(new FileReader(Constants.FileNames.SUDOKU_LIST.getName()))) {
            String line;
            List<Sudoku> sudokuList = new ArrayList<>();
            int contadorSudoku = -1, contadorLinea = 0;
            boolean problem = true;
            String[] splitedLine;
            while ((line = br.readLine()) != null) {
                if(contadorLinea % 3 == 0) {
                    contadorSudoku++;
                    splitedLine = line.split(" ");
                    sudokuList.add(new Sudoku());
                    sudokuList.get(contadorSudoku).setNivel(Integer.parseInt(splitedLine[1]));
                    sudokuList.get(contadorSudoku).setDescripcion(splitedLine[2]+splitedLine[3]+splitedLine[4]);
                }else {
                    if(problem) {
                        sudokuList.get(contadorSudoku).setProblema(line);
                        problem = false;
                    }else {
                        sudokuList.get(contadorSudoku).setSolucion(line);
                        problem = true;
                    }
                }
                contadorLinea++;
            }
            for (Sudoku s : sudokuList) {
                try {
                    dao.insertarSudoku(s);
                } catch (SudokuException e) {
                    System.err.println(e.getMessage());
                }
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

output:

    
asked by Raikish 28.02.2018 в 03:30
source

1 answer

0

Look, I am sharing an improvement to your solution so that you see that you do not have to use any flag to know if it is a solution or problem, with the line meter is more than enough. So the code improves a bit in readability. I hope it serves you.

public static void main(String[] args) {
    SudokuDAO dao = new SudokuDAO();
    try (BufferedReader br = new BufferedReader(new FileReader(Constants.FileNames.SUDOKU_LIST.getName()))) {
        String line;
        List<Sudoku> sudokuList = new ArrayList<>();
        int contadorLinea = 0;
        String[] splitedLine;
        Sudoku tempSudoku = null;
        while ((line = br.readLine()) != null) {
            contadorLinea++;

            if (contadorLinea == 1) {
                tempSudoku = new Sudoku();

                splitedLine = line.split(" ");
                tempSudoku.setNivel(Integer.parseInt(splitedLine[1]));
                tempSudoku.setDescripcion(splitedLine[2]+splitedLine[3]+splitedLine[4]);

                sudokuList.add(tempSudoku);
            } else if (contadorLinea == 2) {
                tempSudoku.setProblema(line);
            } else if (contadorLinea == 3) {
                tempSudoku.setSolucion(line);
                contadorLinea = 0;
            }
        }

        for (Sudoku s : sudokuList) {
            try {
                dao.insertarSudoku(s);
            } catch (SudokuException e) {
                System.err.println(e.getMessage());
            }
        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Notice that the last else if (contadorLinea == 3) is not necessary, but I put it to make it clearer what is done.

    
answered by 28.02.2018 / 21:24
source