安卓 : Reading File

标签 android file

我正在尝试从文件中获取最后 10 行,但无法获取。

我有两个 Activity : 首先,我想将文本从 EditText 写入文件。 在第二个 Activity 中,我尝试读取存储的数据并将其写入 textView

public class Date_Location extends Activity {

ImageView imageView;
EditText editTextDate, editTextLocation, editTextEdit;

private static final String TAG = Date_Location.class.getName();
private static final String FILENAME = "myFile.txt";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.date_location);

    editTextDate = (EditText) findViewById(R.id.editText1);
    editTextLocation = (EditText) findViewById(R.id.editText2);
    editTextEdit = (EditText) findViewById(R.id.editText3);
    imageView = (ImageView) findViewById(R.id.next);
}

public void goNext(View view) {
    String Date = editTextDate.getText().toString();
    String Location = editTextLocation.getText().toString();
    String Comment = editTextEdit.getText().toString();

    writeToFile(Date);
    writeToFile(Location);
    writeToFile(Comment);

    Intent intent = new Intent(this, Detail_Data.class);
    startActivity(intent);
    Date_Location.this.finish();
}

private void writeToFile(String data) {
    String newline = "\r\n";
    try {

        OutputStreamWriter oswName = new OutputStreamWriter(openFileOutput(
                FILENAME, Context.MODE_APPEND));
        oswName.write(newline);
        oswName.write(data);
        oswName.close();
    } catch (IOException e) {
        Log.e(TAG, "File write failed: " + e.toString());
    }
}
}

我的第二个 Activity 在下面

public class Detail_Data extends Activity {

TextView textView1;
ImageView imageView;
private static final String FILENAME = "myFile.txt";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.detail_data);

    textView1 = (TextView) findViewById(R.id.textView1);
    imageView = (ImageView) findViewById(R.id.imageView2);

    String date = readFromFile();
    textView1.setText(date);

}

private String readFromFile() {
    String ret = "";

    try {
        InputStream inputStream = openFileInput(FILENAME);
        ArrayList<String> bandWidth = new ArrayList<String>();

        if (inputStream != null) {
            InputStreamReader inputStreamReader = new InputStreamReader(
                    inputStream);
            BufferedReader bufferedReader = new BufferedReader(
                    inputStreamReader);
            String receiveString = "";
            StringBuilder stringBuilder = new StringBuilder();

            while ((receiveString = bufferedReader.readLine()) != null) {
                 stringBuilder.append(receiveString+'\n');
                 bandWidth.add(receiveString);
                 if (bandWidth.size() == 10)
                  bandWidth.remove(0);
                }
            ret = stringBuilder.toString();
            inputStream.close();
        }
    } catch (FileNotFoundException e) {
        Log.i("File not found", e.toString());
    } catch (IOException e) {
        Log.i("Can not read file:", e.toString());
    }
    return ret;
}

public void goNext(View view) {
    imageView.setColorFilter(0xFFFF3D60, PorterDuff.Mode.MULTIPLY);

    Intent intent = new Intent(this, Agreement.class);
    startActivity(intent);
    Detail_Data.this.finish();
}
}

如果有人有任何想法,请帮助我。我也尝试过其他解决方案,但我也没有获得最后 10 条记录。我得到的不是最后 10 个数据,而是所有写入文件的记录。

最佳答案

首先,如果你在SD卡上写入文件,请确保你已经在AndroidManifest.xml中添加了uses-permission标签

<uses-permission Android:name="Android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

其次,不要忘记 flush()

oswName.write(data);
oswName.flush();
oswName.close();

然后,您的 readFromFile() 方法有问题, 从 while 循环中删除这一行

stringBuilder.append(receiveString+'\n');

并在 while 循环之后添加它

for(String str : bandWidth)
    stringBuilder.append(str + "\n");

readFromFile() 应该如下所示

private String readFromFile() {
    String ret = "";

    try {
        InputStream inputStream = openFileInput(FILENAME);
        ArrayList<String> bandWidth = new ArrayList<String>();

        if (inputStream != null) {
            InputStreamReader inputStreamReader = new InputStreamReader(
                    inputStream);
            BufferedReader bufferedReader = new BufferedReader(
                    inputStreamReader);
            String receiveString = "";
            StringBuilder stringBuilder = new StringBuilder();

            while ((receiveString = bufferedReader.readLine()) != null) {
                 bandWidth.add(receiveString);
                 if (bandWidth.size() == 10)
                  bandWidth.remove(0);
                }

            for(String str : bandWidth)
                stringBuilder.append(str + "\n");

            ret = stringBuilder.toString();
            inputStream.close();
        }
    } catch (FileNotFoundException e) {
        Log.i("File not found", e.toString());
    } catch (IOException e) {
        Log.i("Can not read file:", e.toString());
    }
    return ret;
}

关于安卓 : Reading File,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19424742/

相关文章:

android - com.android.dex.DexException : Multiple dex files define Lcom/google/android/gms/analytics/internal/Command$1;

android - 适用于 Android 和 iPhone 的文本转语音库

python - 从文件号获取文件对象

file - 数据记录器和数据采集系统/软件使用哪些流行/好的文件格式?

android - File.renameTo 返回 false

android - 日志写入过多会降低 Android 应用程序性能吗?

android - 使用 Appium 和 WebdriverIO 测试移动应用程序 : "No route found for/session"

android - 为android中的特定api级别编译c++

c++ - getLine() 返回换行符且没有数据

file - 将 DumpResponse 写入文件后解压缩