Ola galera, preciso detectar um erro aqui em tempo de execução só que não tenho como debugar.
Alguém sabe alguma forma simples que eu possa setar um sysout em um arquivo txt???
Tem que ser bem simples, pois tenho acesso somente a uma classe do projeto…
Certo fábio, no meu código abaixo… tem como escrever no mesmo arquivo?
publicvoidwriteToFile(Stringfilename,Stringtexto){BufferedOutputStreambufferedOutput=null;Filearquivo=newFile(filename);try{if(!arquivo.exists()){bufferedOutput=newBufferedOutputStream(newFileOutputStream(filename));}else{//usa mesmo arquivo}bufferedOutput.write(texto.getBytes());}catch(FileNotFoundExceptionex){ex.printStackTrace();}catch(IOExceptionex){ex.printStackTrace();}finally{try{if(bufferedOutput!=null){bufferedOutput.flush();bufferedOutput.close();}}catch(IOExceptionex){ex.printStackTrace();}}}publicstaticvoidmain(String[]args){newTeste().writeToFile("myFile.txt","vai");}
fabiomedeirosf
Tem sim…
no seu objeto de FileOutputStream, vc passa no construtor append como true, por exemplo:
File file = new File("arquivo.txt");
FileOutputStream fos new FileOutputStream(file, true);
Aí ele fará o que vc quer.
augustocolom
vou teste
augustocolom
fabiomedeirosf:
Tem sim…
no seu objeto de FileOutputStream, vc passa no construtor append como true, por exemplo:
File file = new File("arquivo.txt");
FileOutputStream fos new FileOutputStream(file, true);
Aí ele fará o que vc quer.
Tentei dessa forma e não deu
publicvoidwriteToFile(Stringfilename,Stringtexto)throwsFileNotFoundException{Filearquivo=newFile(filename);FileOutputStreamfos=newFileOutputStream(arquivo,true);BufferedOutputStreambufferedOutput=null;BufferedOutputStreambufferedOutput2=null;try{//se arquivo texto ainda não existir, cria e grava neleif(!arquivo.exists()){bufferedOutput=newBufferedOutputStream(newFileOutputStream(filename));bufferedOutput.write(texto.getBytes());}//se arquivo ja existe, só gravaelse{bufferedOutput2=newBufferedOutputStream(fos);bufferedOutput2.write(texto.getBytes());}}catch(FileNotFoundExceptionex){ex.printStackTrace();}catch(IOExceptionex){ex.printStackTrace();}finally{try{if(bufferedOutput!=null){bufferedOutput.flush();bufferedOutput.close();}}catch(IOExceptionex){ex.printStackTrace();}}}publicstaticvoidmain(String[]args)throwsFileNotFoundException{newTeste().writeToFile("logs.txt","vaasdasdi");}