import java.io.*;
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!");
}
}
}
qual erro que ele está retornando?
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 1030935600 ]