1

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

我有两个活动:首先,我想将文本从 EditText 写入文件。在第二个活动中,我尝试读取存储的数据并将其写入 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());
    }
}
}

我的第二个活动如下

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 个数据,而是写入文件中的所有记录。

4

3 回答 3

1

首先,如果您在 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;
}
于 2013-10-17T11:13:10.137 回答
0

打开输入/输出流后,使用该方法:

import java.io.FileInputStream;
import java.io.FileOutputStream;

public class Files {

    public static String readStringFile(FileInputStream fis) throws java.io.IOException {
        StringBuffer fileContent = new StringBuffer("");

        byte[] buffer = new byte[1024];

        while (fis.read(buffer) != -1) {
            fileContent.append(new String(buffer));
        }

        return fileContent.toString();
    }

    public static void writeStringFile(FileOutputStream fos, String text) throws java.io.IOException {
        fos.write(text.getBytes());
    }
}

首先,使用您想要的文件名创建 FileInputStream 或 FileOutputStream,然后调用上述方法。请注意,这些方法仅适用于读取和写入字符串。

于 2013-10-17T10:56:38.440 回答
0

仅当列表大小 = 10 时,您将每一行存储到列表并删除 0 位置

因此,第一步将所有文件存储在列表中:

反而

while ((receiveString = bufferedReader.readLine()) != null) {
             stringBuilder.append(receiveString+'\n');
             bandWidth.add(receiveString);
             if (bandWidth.size() == 10)
              bandWidth.remove(0);
            }

while ((receiveString = bufferedReader.readLine()) != null) {
             stringBuilder.append(receiveString+'\n');
             bandWidth.add(receiveString);                
            }

将最后 10 行复制到新列表后。

例如,如果您有:

List<String> bandWidth = new ArrayList<String>(Arrays.asList("a1", "a2", "a3","a4", "a5", "a6","a7", "a8", "a9","a10", "a11", "a12"));

比与subList

List<String> bandWidth10rows= bandWidth.subList(bandWidth.size()-10, bandWidth.size());

它将最后 10 个列表项复制到新列表。

完全应该是这样的:

while ((receiveString = bufferedReader.readLine()) != null) {
             stringBuilder.append(receiveString+'\n');
             bandWidth.add(receiveString);                
            }
List<String> bandWidthLastTenRows= bandWidth.subList(bandWidth.size()-10, bandWidth.size()); 
于 2013-10-17T11:00:28.823 回答