【问题标题】:A way to save Arraylist on device?一种在设备上保存 Arraylist 的方法?
【发布时间】:2013-12-13 11:25:16
【问题描述】:

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

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

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

由于我们的服务器提供了号码,因此我无法使用从 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 等已更改,“新的”应该是什么?所以基本上阅读

【问题讨论】:

  • 如果您真的想将数据写入文件,为什么不将我们的数据存储为 json 格式。它也非常容易解析,您可以使用 Gson。我会使用 sqllite 数据库,这对我来说似乎最合乎逻辑,然后您可以查询最近的 100 个数字。

标签: java android arraylist android-contacts


【解决方案1】:

我会做以下事情:

  • java.io.File 获取到本地 (/data/data/f.q.c.n/files/recents) 文件
  • 向它打开一个(缓冲的)FileOutputStream
  • ArrayList&lt;Recent&gt;的元素写入文件
    • 将名称写入字节数组 (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");
    }

}

注意:

  • 您不会简化字节数组的写入和读取。我自己做的,但为了简洁起见没有包括它(它已经足够长了)
  • 缺少 Data[Input|Output]Stream 的导入
  • 您可能不想更改异常的大小
  • 您可以将ints 缩小为小数字

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

【讨论】:

  • 嘿,谢谢你的回答,我正在尝试/写它,因为我们说话。我会及时通知你,很确定这是正确的答案。谢谢@StackOverflowException!
  • 嗨,我已经尝试了一段时间,查找了一些教程.. 但我不知道如何将我的 ArrayList 更改为字节。能给我举个小例子吗?
  • 我明白了。您是否有可能有用的链接?我找不到任何有用的东西。 stackoverflow.com/questions/5618978/…这对我有一点帮助,但不是完全。
  • 谢谢!我现在就测试一下。
  • @Paramone:这样你应该能够有效地写数千条最近的消息。
猜你喜欢
  • 2015-09-29
  • 1970-01-01
  • 2013-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-30
  • 2020-04-17
  • 2021-11-08
相关资源
最近更新 更多