在设备上保存Arraylist的方法?

时间:2013-12-13 11:25:17

标签: java android arraylist android-contacts

所以我有这个小应用程序你可以打电话给。 在此应用程序中,您将能够使用您的电话号码登录并从我们的服务器获取一个号码(这是未实施的)

根据“你是谁”,我们的服务器会动态给你一个号码。

这不是真正相关的,但它仍然与我的问题有关。

由于我们的服务器提供了号码,我无法使用Android CallLogs获得的信息制作“最近列表”。 (它可能会将A.E“妈妈”写入您的专业联系人名单等等)

所以我现在正在做的是,在每次调用之后,将信息(名称,数字,日期和持续时间)插入到数组列表中。

recentCalls.add(new Recent(aName, aDate, aNumber, "0"));

(0表示持续时间的秒数。仍未完成。)

它会暂时“保存”在应用程序中,当应用程序被完全杀死(或崩溃)时,一切都将被清空。

我考虑过在SharedPreferences中保存数组列表。

但是由于我们要将最近限制为100,这将太大了。

我读过这个:Android Storage Options 无论哪种方式,我仍然从手机本身获取所有通话记录,以进行测试。

我的问题是:

  • 如何安全有效地保存ArrayList
  • 如果我不打算使用ArrayList,我怎么能“格式化”ACTUAL手机的call_logs只显示从我的应用程序拨打的号码。 (为了通过我们的服务器调用“通过”,我们使用“ServerPhoneNumber”+“,”+“Inserted PhoneNumber” - 例如:123456789,987654321)
  • 我可以说检查前10个字符是否等于“123456789”?这是不可能的,因为它根据人的不同而改变了数量。

这是我的Java代码:

package com.example.dragonphone;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.ContentResolver;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.graphics.Color;
import android.graphics.LightingColorFilter;
import android.net.Uri;
import android.os.Bundle;
import android.os.Vibrator;
import android.preference.PreferenceManager;
import android.provider.BaseColumns;
import android.provider.CallLog;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;
import android.widget.Toast;

public class Tabs extends Activity implements OnClickListener, OnLongClickListener{
    TabHost th;
    TabSpec specs;
    TextView numberfield;
    ListView recents;
    public String string,number;
    private List<Recent> recentCalls = new ArrayList<Recent>();
    public int counter;
    Button n1,n2,n3,n4,n5,n6,n7,n8,n9,n0,nstar,nhash,sms,contact,call,clear,clearhistory,getinfo;
    //ImageView call, clear;
    public Vibrator vib; 
    //public String phoneNumber;
    String date = new SimpleDateFormat("dd-MM-yyyy").format(new Date());



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tabs);
        th = (TabHost)findViewById(R.id.tabhost);
        numberfield = (TextView) findViewById(R.id.etNumberField);
        n1 = (Button) findViewById (R.id.bNumber1);
        n2 = (Button) findViewById (R.id.bNumber2);
        n3 = (Button) findViewById (R.id.bNumber3);
        n4 = (Button) findViewById (R.id.bNumber4);
        n5 = (Button) findViewById (R.id.bNumber5);
        n6 = (Button) findViewById (R.id.bNumber6);
        n7 = (Button) findViewById (R.id.bNumber7);
        n8 = (Button) findViewById (R.id.bNumber8);
        n9 = (Button) findViewById (R.id.bNumber9);
        nstar = (Button) findViewById (R.id.bNumberStar);
        n0 = (Button) findViewById (R.id.bNumber0);
        nhash = (Button) findViewById (R.id.bNumberHash);
        call = (Button) findViewById (R.id.bCall);
        sms = (Button) findViewById (R.id.bSMS);
        clear = (Button) findViewById (R.id.bClear);
        contact = (Button) findViewById (R.id.bContact);
        recents = (ListView) findViewById (R.id.recentList);
        clearhistory = (Button) findViewById (R.id.bClearHistory);
        getinfo = (Button) findViewById (R.id.bGetCallDetails);


        populateRecentList();
        populateListView();
        registerClickCallback();

        th.setBackgroundColor(Color.rgb(202, 233, 252));
        //n1.getBackground().setColorFilter(new LightingColorFilter(0x000033, 0x000099));
        sms.getBackground().setColorFilter(new LightingColorFilter(0xFFFF66, 0xFFFF00));
        vib = (Vibrator) getSystemService(VIBRATOR_SERVICE);



        n1.setOnClickListener(this);
        n2.setOnClickListener(this);
        n3.setOnClickListener(this);
        n4.setOnClickListener(this);
        n5.setOnClickListener(this);
        n6.setOnClickListener(this);
        n7.setOnClickListener(this);
        n8.setOnClickListener(this);
        n9.setOnClickListener(this);
        nstar.setOnClickListener(this);
        n0.setOnClickListener(this);
        n0.setOnLongClickListener(this);
        nhash.setOnClickListener(this);
        call.setOnClickListener(this);
        clear.setOnClickListener(this);
        clear.setOnLongClickListener(this);
        sms.setOnClickListener(this);
        contact.setOnClickListener(this);
        clearhistory.setOnClickListener(this);
        getinfo.setOnClickListener(this);

        th.setup();
        specs = th.newTabSpec("tag1");
        specs.setContent(R.id.Recents);
        specs.setIndicator("Recent Calls");
        th.addTab(specs);

        specs = th.newTabSpec("tag2");
        specs.setContent(R.id.Keypad);
        specs.setIndicator("Keypad");
        th.addTab(specs);

        specs = th.newTabSpec("tag3");
        specs.setContent(R.id.Sms);
        specs.setIndicator("SMS");
        th.addTab(specs);

        specs = th.newTabSpec("tag4");
        specs.setContent(R.id.Ratings);
        specs.setIndicator("Rates");
        th.addTab(specs);

        specs = th.newTabSpec("tag5");
        specs.setContent(R.id.Account);
        specs.setIndicator("Account");
        th.addTab(specs);


    }

    private void populateRecentList() {
         //TODO Auto-generated method stub
        recentCalls.add(new Recent("Zach", "01-12-2013", "064555246", "600"));
        recentCalls.add(new Recent("Adam", "11-12-2013", "00355563315","510"));
        recentCalls.add(new Recent("John", "03-12-2013", "00955587", "100"));
        recentCalls.add(new Recent("Jorge", "15-10-2013" , "445559585", "60"));

    }
    private void populateListView() {
        // TODO Auto-generated method stub
        ArrayAdapter<Recent> adapter = new MyRecentAdapter();
        ListView list = (ListView) findViewById(R.id.recentList);
        list.setAdapter(adapter);

    }
    private class MyRecentAdapter extends ArrayAdapter<Recent>{
        public MyRecentAdapter(){
            super(Tabs.this, R.layout.recents_view, recentCalls);

        }


    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        //Make sure we have a view to work with
        View itemView = convertView;
        if(itemView == null)
        {
            itemView = getLayoutInflater().inflate(R.layout.recents_view, parent,false);
        }
        Recent currentCall =  recentCalls.get(position);

        TextView nameText = (TextView) itemView.findViewById(R.id.tvRecentName);
        nameText.setText(currentCall.getName());

        TextView numberText = (TextView) itemView.findViewById(R.id.tvRecentNumber);
        numberText.setText(currentCall.getPn());

        TextView dateText = (TextView) itemView.findViewById(R.id.tvRecentDate);
        dateText.setText("" + currentCall.getDate());

        TextView durationText = (TextView) itemView.findViewById(R.id.tvRecentDuration);
        durationText.setText("" + currentCall.getDuration());

        return itemView;
        //      return super.getView(position, convertView, parent);

    }

}
    private void registerClickCallback() {
        // TODO Auto-generated method stub
        ListView list = (ListView) findViewById(R.id.recentList);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View viewClicked, int position,
                    long id) {
                // TODO Auto-generated method stub
                Recent clickedCall = recentCalls.get(position);
                String name = clickedCall.getName();
                numberfield.setText(clickedCall.getPn());
                counter = numberfield.getText().toString().length();
                String message = "Calling " + name;
                Context context = getApplicationContext();
                int duration = Toast.LENGTH_SHORT;

                Toast toast = Toast.makeText(context, message, duration);
                toast.show();
                call();
            }



        });

    }

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        switch(arg0.getId()){
        case R.id.bNumber1:
            numberfield.setText(numberfield.getText() + "1");
            addCheck();
            vib.vibrate(25);
            break;
        case R.id.bNumber2:
            numberfield.setText(numberfield.getText() + "2");
            addCheck();
            vib.vibrate(25);
            break;

        case R.id.bNumber3:
            numberfield.setText(numberfield.getText() + "3");
            addCheck();
            vib.vibrate(25);
            break;
        case R.id.bNumber4:
            numberfield.setText(numberfield.getText() + "4");
            addCheck();
            vib.vibrate(25);
            break;
        case R.id.bNumber5:
            numberfield.setText(numberfield.getText() + "5");
            addCheck();
            vib.vibrate(25);
            break;
        case R.id.bNumber6:
            numberfield.setText(numberfield.getText() + "6");
            addCheck();
            vib.vibrate(25);
            break;
        case R.id.bNumber7:
            numberfield.setText(numberfield.getText() + "7");
            addCheck();
            vib.vibrate(25);
            break;
        case R.id.bNumber8:
            numberfield.setText(numberfield.getText() + "8");
            addCheck();
            vib.vibrate(25);
            break;
        case R.id.bNumber9:
            numberfield.setText(numberfield.getText() + "9");
            addCheck();
            vib.vibrate(25);
            break;
        case R.id.bNumberStar:
            numberfield.setText(numberfield.getText() + "*");
            addCheck();
            vib.vibrate(25);
            break;
        case R.id.bNumber0:
            numberfield.setText(numberfield.getText() + "0");
            addCheck();
            vib.vibrate(25);
            break;
        case R.id.bNumberHash:
            numberfield.setText(numberfield.getText() + "#");
            addCheck();
            vib.vibrate(25);
            break;

        case R.id.bClear:
            String number = numberfield.getText().toString();
            if(number.length() > 0){
                String newNumber = number.substring(0, number.length()-1);
                        numberfield.setText(newNumber);
                        deleteCheck();
                        vib.vibrate(25);
            }else{
                Context context = getApplicationContext();
                CharSequence text = "The numbers are already cleared.";
                int duration = Toast.LENGTH_SHORT;

                Toast toast = Toast.makeText(context, text, duration);
                toast.show();
            }

            break;
        case R.id.bCall:
            call();

            break;
        case R.id.bContact:
             Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
              startActivityForResult(intent, 1);   
            break;
        case R.id.bClearHistory:
            recentCalls.clear();
            CharSequence text = "Your recent list has been cleared.";
            int duration = Toast.LENGTH_SHORT;

            Toast toast = Toast.makeText(this, text, duration);
            toast.show();
            th.setCurrentTabByTag("tag1");
            break;
        case R.id.bSMS:
            th.setCurrentTabByTag("tag3");
            CharSequence text2 = "This function is still under construction..";
            int duration2 = Toast.LENGTH_LONG;

            Toast toast2 = Toast.makeText(this, text2, duration2);
            toast2.show();
            break;
        case R.id.bGetCallDetails:
                getCallDetails();
                break;



        }
    }
    private void deleteCheck() {
        // TODO Auto-generated method stub
        counter --;
        if(counter < 14){
            numberfield.setTextSize(25);  //Set text size when amount goes lower.
        }
        if(counter >= 14 && counter < 16){
            numberfield.setTextSize(20);  //Set text size when amount goes lower.
        }
        if(counter >= 16 && counter < 18){
            numberfield.setTextSize(18);
        }
        if(counter >= 18 && counter < 20){
            numberfield.setTextSize(16);
        }
    }
    private void addCheck() {

        // TODO Auto-generated method stub
        counter++;
        if(counter >= 14){
            numberfield.setTextSize(20);  //Set text size when amount goes higher.
            //numberfield.setMaxHeight(10);
        }
        if(counter >= 16){
            numberfield.setTextSize(18);  //Set text size when amount goes higher.
        }
        if(counter >= 18){
            numberfield.setTextSize(16);  //Set text size when amount goes higher.
        }
        if(counter >= 20){
            numberfield.setTextSize(14);  //Set text size when amount goes higher.
        }

        if(counter < 14){
            numberfield.setTextSize(25);  //Set text size when amount goes lower.
        }
        if(counter >= 14 && counter < 16){
            numberfield.setTextSize(20);  //Set text size when amount goes lower.
        }
        if(counter >= 16 && counter < 18){
            numberfield.setTextSize(18);
        }
        if(counter >= 18 && counter < 20){
            numberfield.setTextSize(16);
        }


    }
    private void getCallDetails() {
        StringBuilder sb = new StringBuilder();
        Uri contacts = CallLog.Calls.CONTENT_URI;
        Cursor managedCursor = this.getContentResolver().query(contacts, null, null, null,CallLog.Calls.DATE + " DESC LIMIT 100");
        int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
        int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE);
        int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);
        int duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION);
        sb.append("Call Details :");
        while (managedCursor.moveToNext()) {



            String phNumber = managedCursor.getString(number);
            String callType = managedCursor.getString(type);
            String callDate = managedCursor.getString(date);
            String callDayTime = new Date(Long.valueOf(callDate)).toString();
            // long timestamp = convertDateToTimestamp(callDayTime);
            String callDuration = managedCursor.getString(duration);
            String dir = null;
            int dircode = Integer.parseInt(callType);
            switch (dircode) {
            case CallLog.Calls.OUTGOING_TYPE:
                dir = "OUTGOING";
                break;

            case CallLog.Calls.INCOMING_TYPE:
                dir = "INCOMING";
                break;

            case CallLog.Calls.MISSED_TYPE:
                dir = "MISSED";
                break;
            }
            sb.append("\nPhone Number:--- " + phNumber + " \nCall Type:--- " + dir + " \nCall Date:--- " + callDayTime + " \nCall duration in sec :--- " + callDuration);
            sb.append("\n----------------------------------");


        }
        managedCursor.close();
        System.out.println(sb);
    }
private void call() {
        // TODO Auto-generated method stub
        if(number.length() > 0){
        try {
               Intent callIntent = new Intent(Intent.ACTION_CALL);
                String dsPhoneNumber = "+34965063314,"; // Dynamic number
                    //965063064 
                string = numberfield.getText().toString().trim();                      
                   number = "tel:" + dsPhoneNumber + string;
                callIntent.setData(Uri.parse(number));
                startActivity(callIntent);
                //recentCalls.add(new Recent(aName, aDate, aNumber, "0"));






            } catch (ActivityNotFoundException activityException) {
                 Log.e("helloandroid dialing example", "Call failed");


            }
        }else {
            Context context = getApplicationContext();
            CharSequence text = "Please insert a phone number or choose a contact.";
            int duration = Toast.LENGTH_SHORT;

            Toast toast = Toast.makeText(context, text, duration);
            toast.show();
        }

    }
    @Override
    public boolean onLongClick(View arg0) {
        // TODO Auto-generated method stub
        switch(arg0.getId()){
        case R.id.bClear:
        if(counter != 0){
            counter = 0;
            numberfield.setTextSize(25);
        numberfield.setText("");
        vib.vibrate(100);}
        else{
            Context context = getApplicationContext();
            CharSequence text = "The numbers are already cleared.";
            int duration = Toast.LENGTH_SHORT;

            Toast toast = Toast.makeText(context, text, duration);
            toast.show();
        }
        break;
        case R.id.bNumber0:

            numberfield.setText(numberfield.getText() + "+");
            addCheck();
            vib.vibrate(25);
            break;
        }
        return true;

    }
    public void onActivityResult(int reqCode, int resultCode, Intent data) {

        if(resultCode == RESULT_OK && data != null) {
            Uri uri = data.getData();

            Cursor cursor=this.getContentResolver().query(uri, null, null, null, null);

            while (cursor.moveToNext()) { 
                String contactId = cursor.getString(cursor.getColumnIndex( 
                        ContactsContract.Contacts._ID)); 
                String hasPhone = cursor.getString(cursor.getColumnIndex( 
                        ContactsContract.Contacts.HAS_PHONE_NUMBER)); 
                if (Integer.parseInt(cursor.getString( cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) { 
                    // You now have the number so now query it like this
                    Cursor phones = getContentResolver().query( 
                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
                            null, 
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, 
                            null, null); 
                    while (phones.moveToNext()) { 
                        String phoneNumber = phones.getString( 
                                phones.getColumnIndex( 
                                        ContactsContract.CommonDataKinds.Phone.NUMBER));  
                        numberfield.setText(phoneNumber);
                        counter = numberfield.getText().toString().length();
                        /*if(counter == 0){

                                Context context = getApplicationContext();
                                //CharSequence text = counter;
                                int duration = Toast.LENGTH_SHORT;

                                Toast toast = Toast.makeText(context, counter, duration);
                                toast.show();

                        }*/
                    } 
                    phones.close(); 


                }
            }

        }
    }
}

提前致谢!

修改

private void registerClickCallback() {
        // TODO Auto-generated method stub
        ListView list = (ListView) findViewById(R.id.recentList);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View viewClicked, int position,
                    long id) {
                // TODO Auto-generated method stub
                Recent clickedCall = recentCalls.get(position);
               // String name = clickedCall.getName();
                //numberfield.setText(clickedCall.getPn());
                counter = numberfield.getText().toString().length();
               // String message = getResources().getString(R.string.toastCalling) + " " + name + " " + getResources().getString(R.string.toastCalling2); 
                Context context = getApplicationContext();
                int duration = Toast.LENGTH_SHORT;

                //Toast toast = Toast.makeText(context, message, duration);
                //toast.show();
                call();
            }



        });

    }

这是我目前的通话方式。 (注释掉了东西,因为它不再像旧代码了。)

基本上每当我点击通话时,

case R.id.bCall:   
call();

通话功能:

private void call() {
        // TODO Auto-generated method stub
        if(numberfield.length() > 0){
        try {
               Intent callIntent = new Intent(Intent.ACTION_CALL);
                String dsPhoneNumber = "+34965063314,"; 
                // Dynamic number
                    //965063064 
             String string = numberfield.getText().toString().trim();                      
                 number = "tel:" + dsPhoneNumber + string;
                callIntent.setData(Uri.parse(number));
                startActivity(callIntent);
                //recentCalls.add(new Recent(aName, aDate, aNumber, "0"));






            } catch (ActivityNotFoundException activityException) {
                 Log.e("helloandroid dialing example", "Call failed");


            }
        }else {
            Context context = getApplicationContext();
            CharSequence text = getResources().getText(R.string.keypadViewToastEmptyNumber);
            int duration = Toast.LENGTH_SHORT;

            Toast toast = Toast.makeText(context, text, duration);
            toast.show();
        }

    }

需要将其插入“列表”并在“最近”选项卡上显示。 我花了几个小时试图解决这个问题,没有进展。提前谢谢!

编辑#2

 public View getView(int position, View convertView, ViewGroup parent){
        //Make sure we have a view to work with
        View itemView = convertView;
        if(itemView == null)
        {
            itemView = getLayoutInflater().inflate(R.layout.recents_view, parent,false);
        }
        Recent currentCall =  recentCalls.get(position);

        TextView nameText = (TextView) itemView.findViewById(R.id.tvRecentName);
        nameText.setText(currentCall.getName());

        TextView numberText = (TextView) itemView.findViewById(R.id.tvRecentNumber);
        numberText.setText(currentCall.getPn());

        TextView dateText = (TextView) itemView.findViewById(R.id.tvRecentDate);
        dateText.setText("" + currentCall.getDate());

        TextView durationText = (TextView) itemView.findViewById(R.id.tvRecentDuration);
        durationText.setText("" + currentCall.getDuration());

        return itemView;
        //      return super.getView(position, convertView, parent);

    }

}

所以这是“最近”标签的视图。 .getDate / .getDuration等被改变了,“新的”应该是什么?所以基本上读了

1 个答案:

答案 0 :(得分:2)

我将做的是以下内容:

  • 获取java.io.File到本地(/data/data/f.q.c.n/files/recents)文件
  • 打开(缓冲)FileOutputStream到它
  • ArrayList<Recent>的元素写入文件
    • 将名称写为字节数组(String.getBytes
    • 将日期写为long(自纪元以来)
    • 将数字写为字符串(因为可以有不同的格式)
    • 将持续时间写为int
    • 请注意,longint是固定大小的,因此可以轻松阅读
    • 您可以在字节数组前加一个int,表示它的大小

然后,当然,你在启动时反向做同样的事情,重建那个ArrayList。

有人将此方法称为受控二进制序列化。当然,你可以让它更容易,但它并不是很难做到,你摆脱了所有反射(这可能会在早期的Android设备上有可怕的性能)。这是我能想到的最快(最顺畅)的方法。

我没有选择标准序列化,因为它很糟糕(反射和元数据吨)。 请注意,SharedPreferences已成功(ab-)用于存储比您更大的数据。

实施例

<强> IO.java

import java.io.IOException;

public interface IO {

    void write(DataOutputStream out) throws IOException;

    void read(DataInputStream in) throws IOException;

}

<强> Recent.java

import java.io.IOException;
import java.util.Date;

public class Recent implements IO {

    private String name, number;
    private Date date;
    private int duration; // in milliseconds

    public Recent(String name, String number, Date date, int duration) {
        this.name = name;
        this.number = number;
        this.date = date;
        this.duration = duration;
    }

    public Recent() {
        this(null, null, null, 0);
    }

    @Override
    public void write(DataOutputStream out) throws IOException {
        byte[] nameData = name.getBytes("UTF-8");
        out.writeInt(nameData.length);
        out.write(nameData);

        byte[] numberData = number.getBytes("UTF-8");
        out.writeInt(numberData.length);
        out.write(numberData);

        out.writeLong(date.getTime());

        out.writeInt(duration);
    }

    @Override
    public void read(DataInputStream in) throws IOException {
        int nameDataLength = in.readInt();
        if (nameDataLength > 100000) throw new IllegalStateException("Name shouldn't be this long: " + nameDataLength);
        byte[] nameData = new byte[nameDataLength];
        in.readFully(nameData);
        name = new String(nameData, "UTF-8");

        int numberDataLength = in.readInt();
        if (numberDataLength > 100000) throw new IllegalStateException("Number shouldn't be this long: " + nameDataLength);
        byte[] numberData = new byte[numberDataLength];
        in.readFully(numberData);
        number = new String(numberData, "UTF-8");

        date = new Date(in.readLong());

        duration = in.readInt();
    }

}

<强> RecentsList.java

import java.io.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class RecentsList implements IO {

    private List<Recent> recents;

    public RecentsList() {
        recents = new ArrayList<Recent>();
    }

    public void addRecent(Recent recent) {
        recents.add(recent);
    }

    @Override
    public void write(DataOutputStream out) throws IOException {
        out.writeInt(recents.size());
        for (Recent r : recents) {
            r.write(out);
        }
    }

    @Override
    public void read(DataInputStream in) throws IOException {
        int size = in.readInt();
        recents = new ArrayList<Recent>(size);
        for (int i = 0; i < size; i++) {
            Recent r = new Recent();
            r.read(in);
            recents.add(r);
        }
    }

    public static void main(String[] args) throws IOException {
        RecentsList test = new RecentsList();

        // build test data
        for (int i = 0; i < 100; i++) {
            String iString = Integer.toString(i);
            Recent r = new Recent(iString, iString, new Date(), i);
            test.addRecent(r);
        }

        // write
        File out = new File("/home/till/recents.bin");
        DataOutputStream dataOut = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(out)));
        test.write(dataOut);
        dataOut.close();

        // read
        RecentsList read = new RecentsList();
        DataInputStream dataIn = new DataInputStream(new BufferedInputStream(new FileInputStream(out)));
        read.read(dataIn);
        dataIn.close();

        System.out.println("read now contains same data as test if everything went ok");
    }

}

注意:

  • 您不会简化写入和读取字节数组。我自己这样做但是没有把它包括在内(它已经足够长了)
  • 缺少数据[输入|输出]流的导入
  • 您可能不想更改例外的大小
  • 您可以缩小int s以获取小数字

如果您有任何问题/疑问,请随时发表评论!