Android 使用readFloat()从文件中读取浮点值
我试图从txt文件中读取浮点值,但总是得到值0。我使用writeFloat在文件上写入值,使用readFloat读取值。如果我在计算机上读取,例如Matlab,我会得到正确的值,但如果我在代码上读取,则不会得到正确的值,其中读取值为0。我的代码怎么了 我添加了WRITE_EXTERNAL_存储和READ_EXTERNAL_存储权限,文件路径正确 写入值Android 使用readFloat()从文件中读取浮点值,android,file-io,readfile,Android,File Io,Readfile,我试图从txt文件中读取浮点值,但总是得到值0。我使用writeFloat在文件上写入值,使用readFloat读取值。如果我在计算机上读取,例如Matlab,我会得到正确的值,但如果我在代码上读取,则不会得到正确的值,其中读取值为0。我的代码怎么了 我添加了WRITE_EXTERNAL_存储和READ_EXTERNAL_存储权限,文件路径正确 写入值 try{ DataOutputStream dos = new DataOutputStream( new FileOutputStre
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);
}
你能把写在SD卡上的文件内容贴出来吗?只要检查一下你的代码就行了。值数组的初始化可能有问题?您可以添加该代码吗?谢谢您的问题,您的代码运行良好,因此它可以帮助我: