0

I created a class (LerEscreverArquivo) that reads the contents of a text file, does a treatment on the read data, prints on the screen the result of the treatment done and writes in a text file the information.

The data reading and writing on the screen is working. The problem happens at the time of writing the values ​​in the text file.

The script only writes the last record it writes to the file. The previous records it ignores. It follows the code and the images of the problem.

public class LerEscreverArquivo {

    private static final String NomeArquivoEntrada = "E:\\DesafioProgramacao\\matriculasSemDV.txt";
    private static final String NomeArquivoSaida = "E:\\DesafioProgramacao\\matriculasComDV.txt";

    public static void main(String[] args) {

        FileReader     fr = null;
        BufferedReader br = null;
        BufferedWriter bw = null;
        FileWriter     fw = null;

        try {

            //input file
            fr = new FileReader(NomeArquivoEntrada);
            br = new BufferedReader(fr);

            String sCurrentLine;
            System.out.println("Início do arquivo.");

            while ((sCurrentLine = br.readLine()) != null) {

                if (!sCurrentLine.isEmpty()) {

                    int Total = 0;
                    int contador = 5;
                    int resto;

                    for (int i = 0; i < sCurrentLine.length(); i++) {

                        int j = Character.digit(sCurrentLine.charAt(i), 10);

                        Total = Total + (j * contador);
                        contador = contador - 1;
                    }

                    resto = Total / 16;
                    String decimal = Integer.toHexString(resto);
                    String DigitoCod=sCurrentLine + "-" + decimal;

                    //output file 
                    fw = new FileWriter(NomeArquivoSaida);
                    bw = new BufferedWriter(fw);
                    bw.write(DigitoCod);
                    System.out.println(DigitoCod);                  
                }
            }

            System.out.println("Fim do arquivo.");

        } catch (IOException eReader) {
            eReader.printStackTrace();
        } finally {
            try {
                if (br != null) {
                     br.close();
                }
                if (fr != null) {
                    fr.close();
                }
                if(bw != null) {
                    bw.close();
                }
                if(fw != null) {
                    fw.close();
                }
            } catch (IOException exeReader) {
                exeReader.printStackTrace();
            }
        }
    }
}
1
  • 1
    You should be using try-with-resources. And you don't need to explicitly close your FileReader or FileWriter - closing the BufferedReader and BufferedWriter will close them already. Commented Apr 20, 2018 at 14:17

1 Answer 1

2

You are initializing the FileWriter in each iteration of the loop

            fw = new FileWriter(NomeArquivoSaida);
            bw = new BufferedWriter(fw);
            bw.write(DigitoCod);

So basically your file is started fresh by removing the previous contents. Try moving the following two lines above the loop and your problem should be solved.

 fw = new FileWriter(NomeArquivoSaida);
 bw = new BufferedWriter(fw);

EDIT

Working code is following:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class LerEscreverArquivo{

    private static final String NomeArquivoEntrada = "matriculasSemDV.txt";
    private static final String NomeArquivoSaida = "matriculasComDV.txt";

    public static void main(String[] args) {

        FileReader fr = null;
        BufferedReader br = null;
        BufferedWriter bw = null;
        FileWriter fw = null;
        try {

            // input file
            fr = new FileReader(NomeArquivoEntrada);
            br = new BufferedReader(fr);

            fw = new FileWriter(NomeArquivoSaida);
            bw = new BufferedWriter(fw);

            String sCurrentLine = "";
            System.out.println("Início do arquivo.");

            while ((sCurrentLine = br.readLine()) != null) {

                if (!sCurrentLine.isEmpty()) {

                    int Total = 0;
                    int contador = 5;
                    int resto;

                    for (int i = 0; i < sCurrentLine.length(); i++) {

                        int j = Character.digit(sCurrentLine.charAt(i), 10);

                        Total = Total + (j * contador);
                        contador = contador - 1;
                    }

                    resto = Total / 16;
                    String decimal = Integer.toHexString(resto);
                    String DigitoCod = sCurrentLine + "-" + decimal;

                    // output file
                    bw.write(DigitoCod);
                    System.out.println(DigitoCod);
                }
            }

            System.out.println("Fim do arquivo.");

        } catch (IOException eReader) {
            eReader.printStackTrace();
        } finally {
            try {
                if (br != null) {
                    br.close();
                }
                if (fr != null) {
                    fr.close();
                }
                if (bw != null) {
                    bw.close();
                }
                if (fw != null) {
                    fw.close();
                }
            } catch (IOException exeReader) {
                exeReader.printStackTrace();
            }
        }
    }
}
2
  • Md Johirul Islam, I moved the lines out of the loop and even then the script only writes the last record into the txt file. Can you edit the code and send me? Commented Apr 20, 2018 at 16:02
  • Md Johirul Islam, Thanks! Commented Apr 20, 2018 at 16:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.