输入流不读取所有文件

我写的文件比原始文件小 2048 字节,是一个完美的副本。


byte [] buffer = new byte[2048];

int readBytes = inputstream.read(buffer, 0, 2048);


while(readBytes !=-1){  

    readBytes = inputstream.read(buffer, 0, 2048); 

    for(int i = 0;i<readBytes;i++){    

        int decimal = buffer[i];

        String binario = conversor.ConverterDecimalToBinario(decimal);

        int decimal1 = conversor.ConverterBinarioToDecimal(binario);

        outputstream.write(decimal1);//-----------------------------------------GRAVAÇÃO

    }//FIM DO FOR

}//FIM DO WHILE


米脂
浏览 122回答 2
2回答

慕姐8265434

在代码进入循环并放入缓冲区之前,最多读取 2048 个字节。在循环中用下一个(最多)2048 字节覆盖缓冲区之前,不会以任何方式处理这个(最多)2048。(见代码中的标记)本质上,第一个 2048 被跳过。byte [] buffer = new byte[2048];&nbsp;//reads&nbsp; &nbsp;2048 bytes.int readBytes = inputstream.read(buffer, 0, 2048);while(readBytes !=-1){&nbsp;&nbsp; &nbsp; //reads&nbsp; &nbsp;2048 bytes - overwrites buffer - furst chunk is skipped!&nbsp;&nbsp; &nbsp; readBytes = inputstream.read(buffer, 0, 2048);&nbsp;&nbsp; &nbsp; for(int i = 0;i<readBytes;i++){&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; int decimal = buffer[i];&nbsp; &nbsp; &nbsp; &nbsp; String binario = conversor.ConverterDecimalToBinario(decimal);&nbsp; &nbsp; &nbsp; &nbsp; int decimal1 = conversor.ConverterBinarioToDecimal(binario);&nbsp; &nbsp; &nbsp; &nbsp; outputstream.write(decimal1);//-----------------------------------------GRAVAÇÃO&nbsp; &nbsp; }//FIM DO FOR&nbsp;}//FIM DO WHILE对于不会覆盖的东西 - 在循环结束时读取下一个块:byte [] buffer = new byte[2048];&nbsp;//reads&nbsp; &nbsp;2048 bytes.int readBytes = inputstream.read(buffer, 0, 2048);while(readBytes !=-1){&nbsp;&nbsp; &nbsp; for(int i = 0;i<readBytes;i++){&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; int decimal = buffer[i];&nbsp; &nbsp; &nbsp; &nbsp; String binario = conversor.ConverterDecimalToBinario(decimal);&nbsp; &nbsp; &nbsp; &nbsp; int decimal1 = conversor.ConverterBinarioToDecimal(binario);&nbsp; &nbsp; &nbsp; &nbsp; outputstream.write(decimal1);//-----------------------------------------GRAVAÇÃO&nbsp; &nbsp; }//FIM DO FOR&nbsp;&nbsp; &nbsp; readBytes = inputstream.read(buffer, 0, 2048);&nbsp;&nbsp;}//FIM DO WHILE或者你可以做这样的事情(如果你喜欢这种赋值和比较的组合,那就是个人喜好了):byte [] buffer = new byte[2048];int readBytes;while(-1 != ( readBytes = inputstream.read(buffer, 0, 2048))){&nbsp;&nbsp; &nbsp; for(int i = 0;i<readBytes;i++){&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; int decimal = buffer[i];&nbsp; &nbsp; &nbsp; &nbsp; String binario = conversor.ConverterDecimalToBinario(decimal);&nbsp; &nbsp; &nbsp; &nbsp; int decimal1 = conversor.ConverterBinarioToDecimal(binario);&nbsp; &nbsp; &nbsp; &nbsp; outputstream.write(decimal1);//-----------------------------------------GRAVAÇÃO&nbsp; &nbsp; }//FIM DO FOR&nbsp;}//FIM DO WHILE

绝地无双

import java.io.BufferedInputStream;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.io.FileNotFoundException;import java.util.*;import java.nio.charset.StandardCharsets;import java.io.File;//===========================================================================================================================*/class conversor {&nbsp; &nbsp;/**&nbsp; &nbsp; * Converte decimal em binário&nbsp; &nbsp; * */&nbsp; &nbsp;public static String ConverterDecimalToBinario(int numeroDecimal){&nbsp; &nbsp; &nbsp; &nbsp;String binario = "";&nbsp; &nbsp; &nbsp; &nbsp;String bin7 = "0000000";&nbsp; &nbsp; &nbsp; &nbsp;String bin6 = "000000";&nbsp; &nbsp; &nbsp; &nbsp;String bin5 = "00000";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Completa números binários pequenos, para ficar com 8 bits&nbsp; &nbsp; &nbsp; &nbsp;String bin4 = "0000";&nbsp; &nbsp; &nbsp; &nbsp;String bin3 = "000";&nbsp; &nbsp; &nbsp; &nbsp;String bin2 = "00";&nbsp; &nbsp; &nbsp; &nbsp;String bin1 = "0";&nbsp; &nbsp; &nbsp; &nbsp;if (numeroDecimal==0){binario = "00000000";}&nbsp; &nbsp;//Se entra 0 decimal sai 8 zeros(em binário)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Converte decimal em binário&nbsp; &nbsp; &nbsp; &nbsp;while(numeroDecimal > 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;binario = (numeroDecimal % 2) + binario;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;numeroDecimal /= 2;&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; int stringLength = binario.length();&nbsp; &nbsp; &nbsp; //tamanho do número sem completar os zero dos 8 bits&nbsp; &nbsp; &nbsp; &nbsp; if(stringLength==1){binario = bin7+binario; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if(stringLength==2){binario = bin6+binario; } //concatena os zeros que faltam&nbsp; &nbsp; &nbsp; &nbsp; if(stringLength==3){binario = bin5+binario; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if(stringLength==4){binario = bin4+binario; }&nbsp; &nbsp; &nbsp; &nbsp; if(stringLength==5){binario = bin3+binario; }&nbsp; &nbsp; &nbsp; &nbsp; if(stringLength==6){binario = bin2+binario; }&nbsp; &nbsp; &nbsp; &nbsp; if(stringLength==7){binario = bin1+binario; }return binario;}&nbsp;//-------------------------------------------------&nbsp; &nbsp;/**&nbsp; &nbsp; * Converte binario em decimal&nbsp; &nbsp; * */&nbsp; &nbsp;public static int ConverterBinarioToDecimal(String numeroBinario){&nbsp; &nbsp; &nbsp; &nbsp;int sum = 0;&nbsp; &nbsp; &nbsp; &nbsp;int size = numeroBinario.length();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //converte binário em decimal&nbsp; &nbsp; &nbsp; &nbsp;char c;&nbsp; &nbsp; &nbsp; &nbsp;for(int i = 0; i < size; i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;c = numeroBinario.charAt(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (c == '0') continue;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sum += Math.pow(2, (size-(i+1)));&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp;return sum;}}//----------------------------------------------------FIM DO CONVERSOR---------------------------------------------------&nbsp; &nbsp; &nbsp; &nbsp; public class teste {&nbsp; &nbsp; public static void main(String[] args)throws java.io.IOException {int contadorx = 0;int terminou = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; InputStream inputstream;&nbsp; OutputStream outputstream;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;inputstream = new BufferedInputStream(new FileInputStream("C:\\Users\\klebe\\Desktop\\tatuagem.png"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;outputstream = new FileOutputStream("C:\\Users\\klebe\\Desktop\\deucerto.png");&nbsp;//--------------------------------------------TAMANHO DO ARQUIVO--------------------------------------------&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; File f = new File ("C:\\Users\\x\\Desktop\\tatuagem.png");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Long tamanhodoarquivo1 = f.length();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double tamanhodoarquivo = (double)tamanhodoarquivo1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;double conttamanhodoarquivo=0;//-------------------------------------FIM TAMANHO DO ARQUIVO----------------------------------------int x = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; byte [] buffer = new byte[2048];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int readBytes = inputstream.read(buffer, 0, 2048);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while(readBytes !=-1){&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; readBytes = inputstream.read(buffer, 0, 2048);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for(int i = 0;i<readBytes;i++){&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int decimal = buffer[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String binario = conversor.ConverterDecimalToBinario(decimal);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int decimal1 = conversor.ConverterBinarioToDecimal(binario);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;outputstream.write(decimal1);//-----------------------------------------GRAVAÇÃO}//FIM DO FOR}//FIM DO WHILE&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;conttamanhodoarquivo=conttamanhodoarquivo+1;//-------------------------------------------------------------------------------------------------------------&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// if (outputstream != null){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//&nbsp; outputstream.close();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//&nbsp; inputstream.close();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// }//FIM DO IF&nbsp; &nbsp; }//FIM DO PUBLIC STATIC VOID MAIN}//FIM DA CLASS PRINCIPAL
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java