简体   繁体   English

读/写内部存储android

[英]read/write internal storage android

I have a question regarding internal files in android.. I tried to write some data into a file and then read it back however, it seems like I can't write data to a file unless I cast it to an integer first.. is there anyway that I can save double or float values.. I added the code I'm trying to use below: 我有一个关于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();
}

Envelope your InputStream and OutputStream with a DataInputStream and a DataOutputStream. 用DataInputStream和DataOutputStream包围InputStream和OutputStream。 These classes have the method you need 这些类具有您需要的方法

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM