0

Based on the following example below.My class location on my computer is C:\Users\daniel\Desktop\Prototype2\src\examples\WavAppender.java

If I were to place the wav file in the same examples directory.How would I alter the below code?

import java.io.File;
import java.io.IOException;
import java.io.SequenceInputStream;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;

public class WavAppender {
public static void main(String[] args) {
    String wavFile1 = "D:\\wav1.wav";
    String wavFile2 = "D:\\wav2.wav";

    try {
        AudioInputStream clip1 = AudioSystem.getAudioInputStream(new File(wavFile1));
        AudioInputStream clip2 = AudioSystem.getAudioInputStream(new File(wavFile2));

        AudioInputStream appendedFiles = 
                        new AudioInputStream(
                            new SequenceInputStream(clip1, clip2),     
                            clip1.getFormat(), 
                            clip1.getFrameLength() + clip2.getFrameLength());

        AudioSystem.write(appendedFiles, 
                        AudioFileFormat.Type.WAVE, 
                        new File("D:\\wavAppended.wav"));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

}

Daniel
  • 199
  • 2
  • 4
  • 18

5 Answers5

0

That depends on where your program will be executed from, the execution directory. If it is the examples directory where also your .java file is then its simple:

new File("wavAppended.wav");
Icewind
  • 873
  • 4
  • 16
0

You can use absolute or relative path to get to your files.

Most people would argue that absolute are not necessary or even harmful as they break your program easily. My opinion is that they are easier to use and you can use them as an initial step to make sure your program works.

Anyway absolute version would be:

"C:\\Users\\daniel\\Desktop\\Prototype2\\src\\examples\\wav?.wav"

or

"C:/Users/daniel/Desktop/Prototype2/src/examples/wav?.wav"

And relative path would be

"examples\\wav?.wav"

or

"examples/wav?.wav"

since your root path is "C:/Users/daniel/Desktop/Prototype2/src/"

Eypros
  • 5,370
  • 6
  • 42
  • 75
0

You should use your files as resources as explained in another SO question here

Preferred way of loading resources in Java

Or you can use absolute or relative paths to your files.

Community
  • 1
  • 1
Antoniossss
  • 31,590
  • 6
  • 57
  • 99
0

put your files in the same directory as your program gets executed in and the file name can be simply

String wavFile1 = "wav1.wav";
BDRSuite
  • 1,594
  • 1
  • 10
  • 15
0

You can use relative path

private String filename = "./src/examples/wav1.wav";
olyv
  • 3,699
  • 5
  • 37
  • 67