我想从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);
}
}
}
}
仅供参考,Stackoverflow不是代码存储库,因此您不能要求代码/示例。 –
file.length()返回抽象路径的长度。不要使用它 – Keerthivasan
你的代码被打乱开始,因为你没有使用'fin.read(buffer)'(它可能没有读取所有数据)的返回值,然后你将这个任意二进制数据转换为字符串,当它不是文本*时。噢,'file.length()'部分也是如此... –