2017-02-27 112 views
0

我正在读取文件并写入相同的文件,但问题是下载的文件比输入的原始文件大2kb。输出文件比输入(原始)文件大尺寸

一些代码段

@Override 
    public void run() { 
     try { 
     BufferedInputStream bis; 
      ArrayList<byte[]> al =new ArrayList<byte[]>(); 

     File file = new File(Environment.getExternalStorageDirectory(), "test.mp3"); 
     byte[] bytes = new byte[2048]; 

     bis = new BufferedInputStream(new FileInputStream(file)); 
     OutputStream os = socket.getOutputStream(); 
      int read ; 

      int fileSize = (int) file.length(); 
      int readlen=1024; 

       while (fileSize>0) { 
        if(fileSize<1024){ 
         readlen=fileSize; 
         System.out.println("Hello........."); 
        } 
        bytes=new byte[readlen]; 
        read = bis.read(bytes, 0, readlen); 
        fileSize-=read; 

        al.add(bytes); 

       } 

      ObjectOutputStream out1 = new ObjectOutputStream(new FileOutputStream(Environment.getExternalStorageDirectory()+"/newfile.mp3")); 

      for(int ii=1;ii<al.size();ii++){ 
        out1.write(al.get(ii)); 
       // out1.flush(); 
      } 
      out1.close(); 
      File file1 = new File(Environment.getExternalStorageDirectory(), "newfile.mp3"); 
+1

不要先将它们放在数组列表中。只需在同一个循环中直接将字节写入FileOutputSteam即可。 – greenapps

回答

0
  1. 不要使用ObjectOutputStream。只需使用围绕它的FileOutputStreamBufferedOutputStream即可。

  2. 在Java中复制流的正确方法如下:

    byte[] buffer = new byte[8192]; // or more, or even less, anything > 0 
    int count; 
    while ((count = in.read(buffer)) > 0) 
    { 
        out.write(buffer, 0, count); 
    } 
    out.close(); 
    

    注意,你并不需要一个缓冲输入的大小,而你并不需要读取整个输入在写任何输出之前。

    希望我每次发布此贴都有1美元。

+0

谢谢先生,但现在文件比原始文件少了1kb。 –

+0

因此,您并未关闭'out',或者您没有使用此代码,或者您仍在使用一些自己的复制代码。 – EJP

+0

先生,如何使用多线程分割file.please帮助我,我不知道更多关于多线程。谢谢! –

-1

我认为你应该使用ByteArrayOutputStream不是一个ObjectOutputStream。我相信这不是一个原始代码,但代码的各个部分放在不同的程序中,否则它是没有意义的。 例如,如果您想要从文件中流式传输某些数据,请处理此数据,然后将数据写入另一个文件。

BufferedInputStream bis = null; 
    ByteArrayOutputStream al = new ByteArrayOutputStream(); 
    FileOutputStream out1 = null; 
    byte[] bytes; 
    try { 
     File file = new File("testfrom.mp3"); 
     bis = new BufferedInputStream(new FileInputStream(file)); 
     int fileSize = (int) file.length(); 
     int readLen = 1024; 
     bytes = new byte[readLen]; 
     while (fileSize > 0) { 
      if (fileSize < readLen) { 
       readLen = fileSize;    
      }   
      bis.read(bytes, 0, readLen); 
      al.write(bytes, 0, readLen); 
      fileSize -= readLen; 
     } 
     bis.close();    
    } catch (IOException e){ 
     e.printStackTrace(); 
    } 

    //proceed the data from al here 
    //... 
    //finish to proceed 

    try { 
     out1 = new FileOutputStream("testto.mp3"); 
     al.writeTo(out1); 
     out1.close();   
    } catch (IOException e){ 
     e.printStackTrace(); 
    } 

不要忘记使用try-catch指令它需要

http://codeinventions.blogspot.ru/2014/08/creating-file-from-bytearrayoutputstrea.html

+0

我试过先生,但是它的抛出错误-ByteArrayOutputStream不能应用于(java.io.FileOutputStream)。请你能帮我修改这条线,因为我很新鲜,我很困惑。谢谢 –

+0

为什么?他应该使用'FileOutputStream'而不是在'ByteArrayOutputStream'上浪费时间和空间。 – EJP

+0

'ByteArrayOutputStream byteArrayOutputStream = getByteStreamMethod(); try(OutputStream outputStream = new FileOutputStream(“thefilename”)){ byteArrayOutputStream.writeTo(outputStream); }' – user3811082