0

我正在尝试在 Android 中做什么FileWriter.println()BufferedReader.readLine()做什么,但在 android 中,他们只允许使用这些方法将其写入 sdcard。

我需要将一些整数值写入文件,然后稍后逐行读取。我发现的一些文件操作方法需要指定读取数据的长度,这是不可能的。

谁能指出我正确的方法?最好有一个用法的例子......

问候

4

1 回答 1

0

您可以使用此路径“\sdcard\file_name”写入 SD 卡

并以这种方式阅读,

 InputStream instream = null;

    as

  InputStream instream = openFileInput("/sdcard/filename");

    // if file the available for reading
    if (instream != null) {
      // prepare the file for reading
      InputStreamReader inputreader = new InputStreamReader(instream);
      BufferedReader buffreader = new BufferedReader(inputreader);

      String line;

      // read every line of the file into the line-variable, on line at the time
      try {
        while (( line = buffreader.readLine()) != null) {
           Log.i(TAG, "line = "+line);
          }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    }

    // close the file again
    try {
        instream.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
于 2011-06-29T20:30:24.723 回答