문제

This is my first time asking a question but I hope I can get an answer. I am trying to copy an existing resource (ogg file) to another directory. I have had success with reading the file as an AudioInputStream and playing the file, but I cannot figure out how to write the file to another location. I am using vorbisspi, tritonus, jorbis, and jogg. (they all came with the vorbisspi library). Can someone please provide an example of a code to write an ogg file to another directory starting with the reading of the file as a resource? If you use any libraries, please specify. Thanks for your help.

도움이 되었습니까?

해결책

Change srcFile and dstFile according to your requirement. Increase buffer size (currently 1024) if required. import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream;

public class FileCopy {

public static void main(String[] args) {
    try {
        File srcFile = new File("/home/test001/sampleDir/sourcefile.wav");

        File dstFile = new File("/home/test001/sampleDir/dstFile.wav");
        FileInputStream in = new FileInputStream(srcFile);
        FileOutputStream out = new FileOutputStream(dstFile);

        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }

        in.close();
        out.close();

    } catch (Exception e) {
        System.out.println(e);
    }

}

}

다른 팁

Have you considered using the Java API: Files.copy(Path, Path)

Copying file from one location to another has nothing to do with formats, codecs etc.

Solution:

  1. Add Apache Commons IO library to your project.

  2. Add one line of code:

    FileUtils.copyFile(src, dst);
    
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top