2013-10-24 67 views
0

我想从USB读取一个Mp3文件并将数据写入不同的缓冲区。请帮助我,如果有任何好的方法来做到这一点。如何在Java中将一个Mp3文件写入多个字节数组?

我可以将整个数据读入一个字节数组。但是我想做一些事情,比如将第一个8字节写入第一个字节数组,然后将第8个字节写入第二个字节数组,等等直到数据结束。请尽可能提供示例代码。正如我可以很快掌握,如果我在我面前有任何例子。

下面是我的代码,我必须读取数据。

public class ReadFileInByteArrayWithFileInputStream { 
    static byte[] buffer1 ; 

    @SuppressWarnings("null") 

    public static void main() { 
     File file = new File("/mnt/media/wayfaring_stranger.mp3"); 
     FileInputStream fin = null; 
     FileOutputStream fout=null; 


     try 
     { 
      // create FileInputStream object 
      fin = new FileInputStream(file); 
      byte fileContent[] = new byte[(int) file.length()]; 
      // Reads up to certain bytes of data from this input stream into an 
      // array of bytes. 
      fin.read(fileContent); 
      // create string from byte array 
      String s = new String(fileContent); 
      System.out.println("File content: " + s); 

      buffer1 = new byte[8]; 



     } 

     catch (FileNotFoundException e) 
     { 
      System.out.println("File not found" + e); 
     } 
     catch (IOException ioe) { 
      System.out.println("Exception while reading file " + ioe); 
     } 
     finally { 

      // close the streams using close method 
      try 
      { 
       if (fin != null) { 
        fin.close(); 
       } 
      } 
      catch (IOException ioe) 
      { 
       System.out.println("Error while closing stream: " + ioe); 
      } 
     } 
    } 
} 
+0

仅供参考,Stackoverflow不是代码存储库,因此您不能要求代码/示例。 –

+1

file.length()返回抽象路径的长度。不要使用它 – Keerthivasan

+0

你的代码被打乱开始,因为你没有使用'fin.read(buffer)'(它可能没有读取所有数据)的返回值,然后你将这个任意二进制数据转换为字符串,当它不是文本*时。噢,'file.length()'部分也是如此... –

回答

0

首先,分裂一个大文件到缓冲区小会是令人难以置信的浪费内存。每个对象的开销与数据本身大小相同(或大于)。然而,将输入流分成多个块的想法本质上不成问题。

我认为你错过的概念是使用收集缓冲区,而不是每个人都有一个单独的变量。因此,您将不会有buffer1,buffer2,buffer3等等,其中每个变量都是byte[],您可以使用一个称为buffers(或其他)的List<byte[]>变量。

下面是一些代码做阅读:

public static List<byte[]> readBuffers(InputStream input, int maxBufferSize) 
     throws IOException { 
    List<byte[]> ret = new ArrayList<byte[]>(); 

    while (true) { 
     byte[] buffer = new byte[maxBufferSize];   
     int bufferRead = 0; 
     while (bufferRead < buffer.length) { 
      int chunk = input.read(buffer, bufferRead, buffer.length - bufferRead); 
      if (chunk == -1) { 
       // End of data. 

       // If there's no data in this buffer, our list is fine. Otherwise 
       // we need to create a smaller buffer and add that. 
       if (bufferRead != 0) { 
        byte[] finalBuffer = new byte[bufferRead]; 
        System.arraycopy(buffer, 0, finalBuffer, 0, bufferRead); 
        ret.add(finalBuffer); 
       } 
       return ret; 
      } 
     } 
     // Full buffer. Add it to the list, and start again. 
     ret.add(buffer); 
    } 
} 

注意如何:

  • 我们始终使用InputStream.read返回值。不要忽视它,因为不能保证它将读取与您在单个调用中要求的数据一样多的数据。
  • 我们不要尝试将数据转换为字符串。除非您真正读取最初为文本数据的字节,否则不应使用String构造函数 - 并且如果读取最初为文本数据的字节,则应指定编码。

或者,如果你实际上只是想在文件中每块传递给其他一些功能 - 哈希计算,例如 - 你不需要在内存中的整个文件都没有。相反,你会封装“块处理器”的想法并将其传递给方法。在上面的方法中添加到列表中的位置,您可以调用块处理器。

+0

谢谢你。我正试图按照你的方法。如果这对我有用,我会回来。 – SajidKhan

0
buffer = new byte[8]; 
        input.read(buffer); 
      //  input.read(buffer, offset, length); 
        Log.i("TvPlayerFunctionalTestApp", "buffer: "+ Arrays.toString(buffer)); 
        buffer1 = new byte[8]; 
        input.read(buffer1); 
+0

上述方法我用过。它给了我我需要的东西。刚刚测试过。 – SajidKhan

+0

这段代码被破坏 - 它*假定* read'调用将始终填充缓冲区。 –

相关问题