[英]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.
它们正在缓冲数据,并且销毁它们时不会自动刷新其内容,因此所有数据都将丢失。
writeDouble()
. writeDouble()
。 That's what it does. BufferedWriter
or PrintWriter
. BufferedWriter
或PrintWriter
。 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.