Error writing and reading CSV file

Asked

Viewed 190 times

1

I have the following problem in writing a CSV file. The file appears this way:

NULL

My class of Archive

    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.Reader;

    public class Arquivo {


        private String conteudo;

        private BufferedReader leitor;


        public static void Gravar(String conteudo) {

            try{  

                File diretorio = new File("C:\\Users\\Desktop\\Teste_Excel\\teste");
                BufferedWriter escrever = new BufferedWriter(new FileWriter(diretorio + "arquivoNomes.txt",true));
                escrever.append(conteudo + "\n");
                escrever.newLine();
                escrever.flush();
                escrever.close();
            }  

            catch(IOException e){  
                System.out.println("Erro na gravação do arquivo: "+ e.getMessage());  
            }  
        }

        public String Ler(String caminho) {

            String texto = "";

            try {
                Reader arquivo = new InputStreamReader(new  FileInputStream(caminho), "UTF-8");
                leitor = new BufferedReader(arquivo); 

                 while((texto = leitor.readLine()) != null){
                    this.setConteudo(texto);
                    System.out.println(this.getConteudo());
                }

            }catch (Exception e) {
                System.out.println("Erro na leitura do arquivo: "+ e.getMessage());  
            }

            return this.conteudo;
        }

        public void setConteudo(String conteudo) {
            this.conteudo = conteudo;
        }

        public String getConteudo() {
            return this.conteudo;
        }

    }

My Main class

public class Main {

    public static void main(String[] args)  {

        Arquivo aq = new Arquivo();
        String caminho = "C:\\Users\\Desktop\\Teste_Excel\\p1.xlsx";
        String conteudo = aq.getConteudo();


        aq.Ler(caminho);
        aq.Gravar(conteudo);
    }


}
  • Have you tried saving the file in CSV to test if the error occurs as well?

  • @R.Santosc Yes the file appears with null in the cell.

  • Try changing this line BufferedWriter escrever = new BufferedWriter(new FileWriter(diretorio + "arquivoNomes.txt",true)); therefore BufferedWriter strW = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(diretorio + "arquivoNomes.txt",true), StandardCharsets.ISO_8859_1));

  • Appears with null.

  • Excel has its format, it is not simply read as a text file. One can use a library like the Apache POI to read and manipulate the file.

  • @Renan Really you are right, however the above script is not even worked for csv files.

  • Try to read the file xlsx thus: Workbook workbook = Workbook.getWorkbook(new File("C:\\testes\\teste.xls"));
 Sheet sheet = workbook.getSheet(0);
 int linhas = sheet.getRows();

  • @R.Santos am not using external libs like the jexcel api.

  • You can see if the reading is returning something?

  • @R.Santos Reading yes now recording continues to return null.

Show 5 more comments

1 answer

2


But it is the expected behavior, isn’t it?! You are writing null in the file. Just follow how you are calling the methods within the class Main:

  • He instigated an object Arquivo.
  • Created a variable caminho and gave her a value.
  • Got null when calling aq.getConteudo();, after all, the method Arquivo#Ler() that is responsible for modifying the value of the attribute Arquivo#conteudo was not called. Here, Main#conteudo is null.
  • Called aq.Ler(caminho); and did nothing with the return value (nor with the attribute conteudo).
  • Recorded the value of Main#conteudo in the file. Remember that he was null? :)

Your code should run like this:

public class Main {
    public static void main(String[] args)  {

        Arquivo aq = new Arquivo();
        String caminho = "C:\\Users\\Desktop\\Teste_Excel\\p1.xlsx";

        aq.Ler(caminho);
        String conteudo = aq.getConteudo();

        aq.Gravar(conteudo);
    }
}

Or else:

public class Main {
    public static void main(String[] args)  {

        Arquivo aq = new Arquivo();
        String caminho = "C:\\Users\\Desktop\\Teste_Excel\\p1.xlsx";

        String conteudo = aq.Ler(caminho);
        aq.Gravar(conteudo);
    }
}
  • 1

    Exactly! Thanks for the help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.