Java, how could we write a file and save it from a

2019-03-06 05:16发布

问题:

I would need to write in a local file from a bean.

I have studied:

How do I create a file and write to it in Java?

https://howtodoinjava.com/java-8/java-8-write-to-file-example/

And I have tried the code:

package beans;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.Stateless;


@Stateless
public class ConviertePuntosNota {

    public String convertidor(int evaluacion) {

        try {

            List<String> lines = Arrays.asList("ConviertePuntosNota::convertidor::el usuario 9ntrodujo: " + evaluacion);
            Path file = Paths.get("log.txt");
            Files.write(file, lines, Charset.forName("UTF-8"));


            return evaluacion >= 5 ? "Apto" : "No apto";
        } catch (IOException ex) {
            Logger.getLogger(ConviertePuntosNota.class.getName()).log(Level.SEVERE, null, ex);
        }
        return "ERROR";
    }
}

We would expect a file called log.txt in the same directoy as the bean is located.

Here wee see that in the hierarchy it does not show up:

Also if we try to search it in the project's directoy it does not appear:

In addition I tried to LOG the absolute path where it is being saved, adding:

Logger.getLogger(ConviertePuntosNota.class.getName()).log(Level.WARNING, null, file.toAbsolutePath().toString());

After Files.write, and we see an empty log:

Also it does not spawns on the hierarchy:

And it is not being found in the project:

Coud you help me please, thank you

Other post I have read before asking this question:

FileIO from an stateless session bean