我正在尝试从 txt 文件中读取浮点值,但我总是得到值 0。我使用 writeFloat() 将值写入文件并使用 readFloat() 读取。如果我在计算机上阅读(例如 Matlab),我会得到正确的值,但如果我阅读我的代码,我会得到 0 作为读取值。我的代码有什么问题?
我添加了 WRITE_EXTERNAL_STORAGE 和 READ_EXTERNAL_STORAGE 权限并且文件的路径是正确的。
写入值
try{
DataOutputStream dos = new DataOutputStream( new FileOutputStream(filename,true));
for (int i=0; i<100; i++)
dos.writeFloat(value[i]);
dos.close();
}
catch(Exception e){;}
读取值
String strFilePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/MyApp/Values.txt";
try
{
FileInputStream fin = new FileInputStream(strFilePath:);
DataInputStream dis = new DataInputStream(fin);
float f;
for (int i=0; i<100; i++) {
f = dis.readFloat();
Log.d("TAG", "float " + Float.toString(f));
}
dis.close();
}
catch(FileNotFoundException fe)
{
Log.d("ERROR", "FileNotFoundException : " + fe);
}
catch(IOException ioe)
{
Log.d("ERROR","IOException : " + ioe);
}