PrintWriter - Dúvida

3 respostas
D
  1. Qual a diferença?

    out  = new PrintWriter(new FileWriter(teste.txt ));
    
    e
    
    out = new PrintWriter(teste.txt);
    

    Se ambas geram o arquivo “teste.txt”.

Agradeço desde já!

3 Respostas

_fs

JavaDoc

/**
     * Creates a new PrintWriter, without automatic line flushing, with the
     * specified file name.  This convenience constructor creates the necessary
     * intermediate {@link java.io.OutputStreamWriter OutputStreamWriter},
     * which will encode characters using the {@linkplain
     * java.nio.charset.Charset#defaultCharset default charset} for this
     * instance of the Java virtual machine.
     *
     * @param  fileName
     *         The name of the file to use as the destination of this writer.
     *         If the file exists then it will be truncated to zero size;
     *         otherwise, a new file will be created.  The output will be
     *         written to the file and is buffered.
     *
     * @throws  FileNotFoundException
     *          If the given string does not denote an existing, writable
     *          regular file and a new regular file of that name cannot be
     *          created, or if some other error occurs while opening or
     *          creating the file
     *
     * @throws  SecurityException
     *          If a security manager is present and {@link
     *          SecurityManager#checkWrite checkWrite(fileName)} denies write
     *          access to the file
     *
     * @since  1.5
     */
    public PrintWriter(String fileName) throws FileNotFoundException {
	this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName))),
	     false);
    }
marcosbrandao

A unica diferença eh o argumento que tu passa para o construtor.
Um eh um FileWriter e o outro uma String.
De resto eh igual.

LPJava

é o que marcos falou… isso seria redudante… ambos casos acima… criar o arquivo e vc ja pode escrever nele…

Criado 22 de maio de 2007
Ultima resposta 22 de mai. de 2007
Respostas 3
Participantes 4