Question

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.

Was it helpful?

Solution

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);
    }

}

}

OTHER TIPS

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);
    
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top