【问题标题】:Output file is getting large size than input (original) file输出文件比输入(原始)文件大
【发布时间】:2017-02-27 08:28:55
【问题描述】:

我正在读取文件并写入相同的文件,但问题是下载的文件比输入的原始文件大 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");

【问题讨论】:

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

标签: java android file-handling


【解决方案1】:
  1. 不要使用ObjectOutputStream。只需使用 FileOutputStream 或包裹在其周围的 BufferedOutputStream

  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 美元。

【讨论】:

  • 谢谢先生,但现在文件比原始文件少了 1kb。
  • 所以你没有关闭out,或者你没有使用这个代码,或者你还在使用你自己的一些复制代码。
  • 先生,如何使用多线程来分割文件。请帮助我,我不知道更多关于多线程的知识。谢谢!!
  • @MANOJGAYAKWAD 您的问题(或此答案)中没有关于多线程的内容。如果您有新问题,请将其作为新问题提出。不在对不同问题的答案发表评论。 (a) 没有人会在那里看到它,并且 (b) 这是一个问答网站,而不是一个论坛。
【解决方案2】:

我认为您应该使用 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

【讨论】:

  • 我试过先生,但它在 ByteArrayOutputStream 中的抛出错误不能应用于(java.io.FileOutputStream)。请你能帮我如何修改这条线,因为我比较新鲜,我很困惑。谢谢
  • 为什么?他应该使用FileOutputStream 而不是在ByteArrayOutputStream 上浪费时间和空间。
  • ByteArrayOutputStream byteArrayOutputStream = getByteStreamMethod(); try(OutputStream outputStream = new FileOutputStream("thefilename")) { byteArrayOutputStream.writeTo(outputStream); }
  • 你还没有回答我的问题。 ByteArrayOutputStream 既浪费时间又浪费空间。你的新代码也充满了错误。
  • 我假设如果一个人使用中间缓冲区是他需要的手段。例如,用于处理信息。我在这个问题的背景下提供了一个答案。书面代码有效 - 我检查过。在上一版中我在代码中添加了 try-catch 指令。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-06
  • 1970-01-01
  • 2018-06-15
  • 1970-01-01
  • 1970-01-01
  • 2012-01-17
相关资源
最近更新 更多