简体   繁体   English

为什么在使用datainputstream,java,android时会得到0kb的文件?

[英]Why I get a 0kb file when I use datainputstream, java, android?

I'm really confused about why I get a 0kb file when I use that code showing below. 我对使用下面显示的代码时为什么得到0kb文件感到非常困惑。 As the instruction of Java website about this class, I should be worked. 作为关于该类的Java网站的指导,我应该被工作。 but... And I want to generate a sine wave and output its result in to a txt fill in double. 但是...而且我想生成一个正弦波并将其结果输出为txt中的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();
    }
}

} }

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. 它们正在缓冲数据,并且销毁它们时不会自动刷新其内容,因此所有数据都将丢失。

  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() . 您正在获取二进制文件,因为您正在调用writeDouble() That's what it does. 这就是它的作用。 If you want strings, use a BufferedWriter or PrintWriter . 如果需要字符串,请使用BufferedWriterPrintWriter
  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. new FileOutputStream已经完成了所有这些工作。 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. 全部删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么在Java中读取datainputstream时出现EOF异常? - Why do I get EOF exception while reading a datainputstream in java? 我应该使用 DataInputStream 还是 BufferedInputStream - Should I use DataInputStream or BufferedInputStream 为什么在尝试读取DataInputStream时出现EOFException? - Why am I getting EOFException when trying to read DataInputStream? android:将捕获照片0kb保存在文件夹中 - android: save capture photo 0kb in the folder Proftpd-上传后0kb的文件 - Proftpd - 0kb file after upload bluemix对​​象存储文件已成功上传,但是使用Java在对象存储上上传了0kb文件 - bluemix object storage file uploading successfully, but 0kb file is uploading on object storage using java 文件成功上传,但是使用jsch sftp java在远程服务器上上传了0kb文件 - file uploading successfully, but 0kb file is uploading on remote server using jsch sftp java java,为什么在空格时DataInputStream拆分String - java, why DataInputStream split String when whitespace 在android/java中调用createNewFile()时,为什么会出现:java.io.IOException: No such file or directory - When calling createNewFile() in android/java, why do I get: java.io.IOException: No such file or directory Java DataInputStream到图像文件 - Java DataInputStream to image file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM