0

I'm really confused about why I get a 0kb file when I use that code showing below. As the instruction of Java website about this class, I should be worked. but... And I want to generate a sine wave and output its result in to a txt fill in double. That is the first step of my code, and I'm stuck in such simple problem. Maybe I was not pretty understand how to use class file and datastream as I learned from offical website.

public class audioplayThread implements Runnable {
private File file;
private FileOutputStream ops;
private BufferedOutputStream bos;
private DataOutputStream dos;
private double Omega;
private int f = 18*1000;
private double pi;
private int samplenumber = 84;      //Assume OFDM symbol has 64 real value and 
private static final int Encording = AudioFormat.ENCODING_PCM_16BIT; //Data size for each frame = 16 bytes
private static final int Sample_rate = 48000;                        //Sample rate = 48000 HZ
private static final int Channel = AudioFormat.CHANNEL_OUT_MONO;         //Set as single track
private static final int Buffersize = AudioTrack.getMinBufferSize(Sample_rate, Channel,Encording);
@Override
public void run() {
    // TODO Auto-generated method stub
    file = new File(Environment.getExternalStorageDirectory().getAbsoluteFile(),"mmm.txt");
    if(file.exists()){
        file.delete();
    }
    try {
        file.createNewFile();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    try {
        /*Create a datastream*/
        ops = new FileOutputStream(file);
        bos = new BufferedOutputStream(ops);
        dos = new DataOutputStream(bos);
        /*Set sine wave parameter*/
        pi = Math.PI;
        Omega = 2*pi*f/Sample_rate;

        /*Build instance for audiotrack*/
        //AudioTrack at = new AudioTrack(AudioManager.STREAM_MUSIC,Sample_rate, Channel, Encording, Buffersize, AudioTrack.MODE_STREAM);

        /*build sine wave*/
        double[] buffer = new double[samplenumber];
        for(int i=0;i<samplenumber;i++){
            buffer[i] = Math.sin(Omega*i);
            dos.writeDouble(buffer[i]);

        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

}

6
  • 1
    Have you tried closing the streams? You're buffering and never closing them, data may be lost v Commented Jun 8, 2016 at 20:40
  • uh...I am so foolish, how could I forgot close it. Thank you! One more question, Why I get those data in txt file is unreadable code ? Commented Jun 8, 2016 at 20:58
  • Because you're using 'DataOutputStream', which uses a binary format. Commented Jun 8, 2016 at 21:17
  • I suggest that you follow the Java naming conventions (camel case, first letter in lower case for variables and methods, upper case for types, and English word fragments correctly spelled). Commented Jun 8, 2016 at 21:18
  • Yes, I know that it use a binary format. So I want to try to use PrintWirter to finish this job, but I still not figure out where will be the output of method print. Or I can use datainputstream read that .txt file, but I want analyse those data use fft and matlab. Commented Jun 8, 2016 at 21:35

2 Answers 2

1

The problem is that you're not closing the streams when you're done. They are buffering the data and don't automatically flush their content when they are destroyed so all data is lost.

Sign up to request clarification or add additional context in comments.

Comments

0
  1. You're getting empty output because you're never closing the output stream. It is buffered, and it will stay buffered until you flush or close it.
  2. You're getting binary because you're calling writeDouble(). That's what it does. If you want strings, use a BufferedWriter or PrintWriter.
  3. All this:

    if(file.exists()){
        file.delete();
    }
    try {
        file.createNewFile();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    

    is not only unnecessary: it is wasteful. new FileOutputStream already does all that. You are doubling or tripling the overhead of creating a file; you are doing it non-atomically; and you are wasting both time and space. Remove it all.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.