O que está errado?

2 respostas
G
import <a href="http://java.io">java.io</a>.*;





class Exemplo09{


  public static void main (String[] args){


  String FileName = new String("TesteGravandoJava");


  File to_file = new File(FileName + ".txt");


  try{


    FileOutputStream to = new FileOutputStream(to_file);


    DataOutputStream dt = new DataOutputStream(to);





    for ( int x=0; x < 10; x = x + 1 )


      dt.writeBytes("X");


    }


    catch (IOException exc){


      System.out.println("Erro ao abrir o arquivo!");


    }


  }


}

2 Respostas

A

qual erro que ele está retornando?

F

O erro fatal era que vc não colocou o caminho, onde o arquivo deveria ser gravado. Aproveitei e coloquei algumas dicas no código.





import java.io.*;



class Exemplo09 {

public static void main(String[] args) {

//ERRADO

//String FileName = new String("TesteGravandoJava");



//Vc precisa passar o caminha do arquivo;

//String FileName = new String("c:\TesteGravandoJava");

//OU

String FileName = new String("/TesteGravandoJava");



//Procure não utilizar "_"(underline) em nomes de variáveis ou de classes

//File to_file = new File(FileName + ".txt");

//É mais elegante utilizar a sintaxe:

File toFile = new File(FileName + ".txt");



try {

FileOutputStream to = new FileOutputStream(toFile);

DataOutputStream dt = new DataOutputStream(to);



//o x=x+1 pode ser substituido por x++ ou por x+=1;

//for (int x = 0; x < 10; x = x + 1)



for (int x = 0; x < 10; x++)

dt.writeBytes("X");

}

catch (IOException e) {

//Procure imprimir a exceção para facilitar a identificação de um possível erro.

System.out.println("Erro ao abrir o arquivo: "+e.toString());

}

}

}





_________________

Atenciosamente,






Franklin Samir



www.portaljava.com

[ Editado por Franklin No dia [telefone removido] ]
Criado 25 de junho de 2002
Ultima resposta 25 de jun. de 2002
Respostas 2
Participantes 3