Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用JarEntry编写声音文件无法正常工作_Java_Fileinputstream_Fileoutputstream - Fatal编程技术网

Java 使用JarEntry编写声音文件无法正常工作

Java 使用JarEntry编写声音文件无法正常工作,java,fileinputstream,fileoutputstream,Java,Fileinputstream,Fileoutputstream,好的,在我的程序中,我在程序的Jar中保存了各种资源,当程序运行时,它会将特定的文件传递给这个函数,这些文件会被写入用户计算机的HDD中。所有的东西都被写出来了,但只有图像是100%正确的。声音文件不是那么幸运 基本上,我无法正确地写入声音,它们的文件大小是正确的,但它们只包含一瞬间的音频,而不是完整的音频。我是不是遗漏了什么?我似乎做的每件事都是对的,但如果那是真的,我就不会在这里发帖了 我尽力用谷歌搜索这个问题,但失败了 任何关于这为什么不起作用的猜测都会令人惊讶!!:)由于JarEntry

好的,在我的程序中,我在程序的Jar中保存了各种资源,当程序运行时,它会将特定的文件传递给这个函数,这些文件会被写入用户计算机的HDD中。所有的东西都被写出来了,但只有图像是100%正确的。声音文件不是那么幸运

基本上,我无法正确地写入声音,它们的文件大小是正确的,但它们只包含一瞬间的音频,而不是完整的音频。我是不是遗漏了什么?我似乎做的每件事都是对的,但如果那是真的,我就不会在这里发帖了

我尽力用谷歌搜索这个问题,但失败了


任何关于这为什么不起作用的猜测都会令人惊讶!!:)

由于
JarEntry
扩展了
ZipEntry
,我建议不要依赖
ZipEntry.getSize()
方法,因为它返回-1。看

此外,通常在读取流时利用缓冲更为常见。在您的示例中,您将所有内容都放在字节数组中,因此我猜对于大文件,您可能会出现
OutOfMemoryError

下面是我要测试的代码:

public static void writeFile(String theFileName, String theFilePath)
{
    try {
        File currentFile = new File("plugins/mcMMO/Resources/"+theFilePath+theFileName);
        //System.out.println(theFileName);
        @SuppressWarnings("static-access")
        JarFile jar = new JarFile(plugin.mcmmo);
        JarEntry entry = jar.getJarEntry("resources/"+theFileName);
        InputStream is = jar.getInputStream(entry);
        byte[] buf = new byte[(int)entry.getSize()];
        is.read(buf, 0, buf.length);
        FileOutputStream os = new FileOutputStream(currentFile);
        os.write(buf);
        os.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
public static void writeFile(String theFileName, String theFilePath)
{
    try {
        File currentFile = new File("plugins/mcMMO/Resources/"+theFilePath+theFileName);
        @SuppressWarnings("static-access")
        JarFile jar = new JarFile(plugin.mcmmo);
        JarEntry entry = jar.getJarEntry("resources/"+theFileName);
        InputStream is = jar.getInputStream(entry);
        byte[] buf = new byte[2048];
        int nbRead;
        OutputStream os = new BufferedOutputStream(new FileOutputStream(currentFile));
        while((nbRead = is.read(buf)) != -1) {
            os.write(buf, 0, nbRead);
        }
        os.flush();
        os.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}