Não entendi a parte do grava em binário o0, você só quer mudar a forma como está gravando, sem manipular os dados ou alterar o tipo?
Tipo assim, se o texto.txt tiver assim “Teste123” o texto2.txt vai ficar “Teste123” também né?
L
luizrocha13
Sim :lol: mais tipo um caracter tem 8 bits eu vou gravar somente 6 bits, isso eh soh pra saber como que eu faço para gravar em outro arquivo
drigo.angelo
Acho que vou fazer esse também, toda vez que vou fazer uma operação de IO tenho que recorrer a documentação da API, pois acabo esquecendo que métodos/objetos utilizar… xD
L
luizrocha13
Alguém Pls
drigo.angelo
Foi malz a demora.
Não se se é bem isso que você quer, tem que refatorar ainda, mas ta lendo e gravando :D
packageio;importjava.io.BufferedReader;importjava.io.File;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.FileReader;importjava.io.IOException;publicclassLeArquivo{publicstaticvoidmain(Stringargs[]){Strings=leArquivo();gravaArquivo(s);}privatestaticvoidgravaArquivo(Strings){charsc=File.separatorChar;Filearquivo=newFile(sc+"Temp"+sc+"texto2.txt");try{if(!arquivo.exists())arquivo.createNewFile();else{// Se existir o arquivo, deleta e cria outro novo.arquivo.delete();arquivo.createNewFile();}}catch(IOExceptione){e.printStackTrace();//Log here}FileOutputStreamf=null;try{f=newFileOutputStream(arquivo);f.write(s.getBytes());f.flush();}catch(FileNotFoundExceptione){e.printStackTrace();// Log here}catch(IOExceptione){e.printStackTrace();}finally{try{f.close();}catch(IOExceptione){e.printStackTrace();}}}privatestaticStringleArquivo(){charsc=File.separatorChar;StringBuilderconteudo=newStringBuilder();Stringl;try{FileReaderreader=newFileReader(sc+"Temp"+sc+"texto.txt");BufferedReaderbReader=newBufferedReader(reader);while((l=bReader.readLine())!=null)conteudo.append(l);}catch(FileNotFoundExceptione){e.printStackTrace();//Log here}catch(IOExceptione){e.printStackTrace();//Log here}returnconteudo.toString();}}
drigo.angelo
Espero ter ajudado, ao menos pra você ter uma base aí de #comofaz rsrs o FileOutPutStream grava os dados não exatamente em binário, mas grava os bytes, é o mínimo que o java trabalha, eu acho…
Java API:
A file output stream is an output stream for writing data to a File or to a FileDescriptor. Whether or not a file is available or may be created depends upon the underlying platform. Some platforms, in particular, allow a file to be opened for writing by only one FileOutputStream (or other file-writing object) at a time. In such situations the constructors in this class will fail if the file involved is already open.
FileOutputStream is meant for writing streams of raw bytes such as image data. For writing streams of characters, consider using FileWriter.
L
luizrocha13
Opa Valew ae, vou estudar esse Script e tenta adaptar o meu .
drigo.angelo
To tentando adaptar ele pra criar outra imagem, tipo assim;
imagem.jpg —> Le o arquivo —> Grava em binario —> imagem2.jpg
Mas não ta dando certo, vou continuar tentando aqui xD
drigo.angelo
Ainda não entendo çabosta… o FileInputStream lê os bytes e retorna um int :shock:
Já o FileOutputStream só aceita byte ou byte[]…:twisted: