Error writing and reading file in CSV

1

I have the following problem writing a CSV file. The file looks like this:

  

NULL

My File class

    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);
    }


}
    
asked by anonymous 17.11.2017 / 13:56

1 answer

2

But it's the expected behavior, is not it ?! You are writing null to the file. Just follow how you are calling methods within the Main class:

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

Your code should be executed 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);
    }
}
    
17.11.2017 / 17:04