读/写内部存储android

时间:2013-05-02 13:16:08

标签: java android storage internal

我有一个关于android内部文件的问题..我试图将一些数据写入文件,然后再读回来,但似乎我无法将数据写入文件,除非我先将其转换为整数..无论如何我可以保存double或float值..我添加了我想在下面使用的代码:

FormatCluster formatCluster = ((FormatCluster)objectCluster.returnFormatCluster(ofFormats,"Calibrated")); 
if (formatCluster != null) {
//Obtain data for text view
calibratedDataArray[0] = formatCluster.mData;
calibratedUnits = formatCluster.mUnits;
A.setText("data: " + formatCluster.mData);

String filename = "myfile";
//String string = "Hello world!";
FileOutputStream outputStream;

try {
  outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
  outputStream.write((int)formatCluster.mData);//here I don't want to cast the value to integer
  outputStream.close();
} catch (Exception e) {
  e.printStackTrace();
}

//testing.setText)

double ch;
StringBuffer fileContent = new StringBuffer("");
FileInputStream fis;
try {
fis = context.openFileInput( filename );
try {
while( (ch = fis.read()) != -1)
testing.setText(fileContent.append(ch));
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}

1 个答案:

答案 0 :(得分:0)

使用DataInputStream和DataOutputStream为您的InputStream和OutputStream包络。这些类具有您需要的方法