Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 保存数据后,我无法确定如何读入数据_Android_File - Fatal编程技术网

Android 保存数据后,我无法确定如何读入数据

Android 保存数据后,我无法确定如何读入数据,android,file,Android,File,问题是我使用的是FileOutputStream类的write方法。我读过的文档说这会向文件输出一个字节。在FileOutputStream类中找不到读取方法。 但是InputStreamReader中有一种读取方法。问题是,我读过的文档中说,这个class read函数通过将字节转换为char来返回char。这将改变数据。我应该如何读回数据 保存文件并似乎有效的代码 boolean Save() { String FILENAME = "hello_file"; Str

问题是我使用的是FileOutputStream类的write方法。我读过的文档说这会向文件输出一个字节。在FileOutputStream类中找不到读取方法。 但是InputStreamReader中有一种读取方法。问题是,我读过的文档中说,这个class read函数通过将字节转换为char来返回char。这将改变数据。我应该如何读回数据

保存文件并似乎有效的代码

boolean Save()
{
      String FILENAME = "hello_file";
      String string = "hello world!";
      cDate mAppoitments[];   


      try {
      FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE );

      int i;
      mAppoitments=cDates.GetUpperDates();
      for(i=0;i<cDates.getMaxAmount();i++)
      {
          i=mAppoitments[i].getMonth();
          fos.write( i  );
          i=mAppoitments[i].getDay();
          fos.write( i  );
          i=mAppoitments[i].getYear()-1900;
          fos.write( i  );            
      }

      mAppoitments=cDates.GetLowerDates();
      for(i=0;i<cDates.getMaxAmount();i++)
      {
          i=mAppoitments[i].getMonth();
          fos.write( i  );
          i=mAppoitments[i].getDay();
          fos.write( i  );
          i=mAppoitments[i].getYear()-1900;
          fos.write( i  );            
      }       
      fos.close();
      }
      // just catch all exceptions and return false
        catch (Throwable t) {
            return false;
        }

 return true;
  }
boolean保存()
{
String FILENAME=“hello_file”;
String=“你好,世界!”;
cDate映射点[];
试一试{
FileOutputStream fos=openFileOutput(文件名,Context.MODE\u PRIVATE);
int i;
mappoints=cDates.GetUpperDates();

对于(i=0;i如果您觉得更舒服,您可以将输出流包装在PrinterWriter中,将输入蒸汽读取器包装在BufferedReader中。然后,您可以写入和读取字符串。

只需将文件作为流打开:

// open the file for reading
InputStream instream = openFileInput(FILENAME);
// prepare the file for reading
InputStreamReader inputreader = new InputStreamReader(instream);
BufferedReader buffreader = new BufferedReader(inputreader);

你可以一行一行地读它,我想你在使用
I
作为迭代器和变量来存储你写的东西时会遇到一些问题。

我的规则是使用相同类型的流进行读写。因此,如果你使用
openFileOutput
打开一个文件进行写,请使用
openFileInput
打开输入流进行读取。由于方法
write(int)
将一个字节写入文件,因此可以安全地使用方法
read()
读取每个字节并将其分配给变量

但是循环中存在一个大问题-在循环中修改i,与索引无关:

      i=mAppoitments[i].getMonth(); // now i might be assigned with 12
      fos.write( i  ); // you write 12
      i=mAppoitments[i].getDay(); // now you look for mAppoitments[12].getDay()
      ....
使用其他变量将这些值写入文件,不要在循环中修改i。例如:

for(i=0;i<cDates.getMaxAmount();i++)
  {
      int j;
      j=mAppoitments[i].getMonth();
      fos.write( j  );
      j=mAppoitments[i].getDay();
      fos.write( j  );
      j=mAppoitments[i].getYear()-1900;
      fos.write( j  );            
  }

用于(i=0;我不明白您在这里尝试的是什么。您是在尝试读取刚刚保存的文件吗?逐行读取会导致一个大问题,因为数据不是保存为文本,所以他会逐字节保存。@MByD-所以只需跳过BufferReader和UnputReader,从inputStream中逐字节读取。这只是一个提示,提示您使用更多的“智能化”我同意,但这也应该提到。