Creating Output to File Using PrintStream Class

0

I'm having trouble trying to generate a file with the PrintStream class from the java.io package.

This application works as follows, I create an object of type 'Account' to store a 'ContaCorrente' or 'ContaPoupanca', and soon after the creation of each instance has to be generated a file output for each object, more precisely in the 'accounts.txt' file.

In the 'AccountRepository' class there is the implementation of the 'saved' method that will generate the text file, receiving as a parameter a List of the type Account. This list is received through a javaFX form event, which you can see in the 'Account Manipulator' class.

Let's go to the source code:

Class: AccountRepository.JAVA

public class RepositorioDeContas {

   public void salva(List<Conta> contas){
       PrintStream stream = null;

       try {
           stream = new PrintStream("contas.txt");
       } catch (FileNotFoundException e) {
           e.printStackTrace();
       }

       for(Conta conta : contas) {
           stream.println(conta.getTipo() + "," + conta.getNumero() + conta.getAgencia() + "," + conta.getTitular() + "," + conta.getSaldo());
       }
       stream.close();
   }    
}

Class: Account Handler.JAVA

public class ManipuladorDeContas {

    public void salvaDados(Evento evento){
        //Recebe e armazena em uma lista as contas recebidas do campo listaContas
        List<Conta> contas = evento.getLista("listaContas");

        //aqui salvaremos as contas em um arquivo
        RepositorioDeContas repositorio = new RepositorioDeContas();
        repositorio.salva(contas);          
    }   
}

I'm running the application and I'm not getting any errors, and even eclipse does not show me any inconsistencies, but when I create an 'Account' object the file 'accounts.txt' is not generated and no other type of output.

I would like to make it clear that I do not have access to the implementation details of the javaFX interface, since I am following CAJUM's fj-11 course, where I was provided with all the interface already installed.

If possible, please take a look at how I implemented the try / catch block in the saved method, Eclipse does not acknowledge any errors, but I think I'm using it improperly for lack of experience.

The material link can be found at the following link: CAELUM fj-11 java.io package

    
asked by anonymous 13.06.2018 / 03:34

0 answers