2
respostas

Não tá entrando no arquivo.

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;

/**
 *
 * @author maryu
 */
public class TesteLeituraEscrita {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {

        //Fluxo de entrada de arqivo
        InputStream fis = System.in;
        Reader isr = new InputStreamReader(fis);
        BufferedReader br = new BufferedReader(isr);

        //Fluxo de saída do arquivo
        OutputStream fos = new FileOutputStream("texto3.txt");
        Writer osw = new OutputStreamWriter(fos);
        BufferedWriter bw = new BufferedWriter(osw);

        String linha = br.readLine();

        while (linha != null && linha.isEmpty()) {

            bw.write(linha);
            bw.newLine();
            linha = br.readLine();
        }
        br.close();
        bw.close();
    }
}
2 respostas

o erro está nessa condicional:

 while (linha != null && linha.isEmpty())

Desse jeito você está mandando ele entrar no laço enquanto a linha não for null e estiver vazia...

O que você quer na verdade é entrar no while enquanto a linha nao for null e não estiver vazia, então o certo é colocar uma exclamação pra inverter o resultado do metodo isEmpty().

 while (linha != null && 
            !linha.isEmpty())

Muito obrigada!

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software