IPC之文件共享的使用

2016-10-20  本文已影响0人  chenmingzhi

共享文件也是进程间通信的方式,两个进程通过读写同一个文件来交换数据。其局限性在于如果是并发读写时,内容有可能不是最新的,应该尽量避免这种情况或者使用线程同步在限制多个线程的写操作。

使用方法:

/**
* 写
**/
private void persistToFile(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                User user = new User(1,"hello world",false);//User实现了Serializable接口
                String printTxtPath = getApplicationContext().getFilesDir().getAbsolutePath();
                File dir = new File(printTxtPath + "/user");
                if(!dir.exists()){
                    dir.mkdirs();
                }
                File cachedFile = new File(dir.getAbsolutePath()+"/user.txt");
                ObjectOutputStream objectOutputStream = null;
                try {
                    objectOutputStream = new ObjectOutputStream(new FileOutputStream(cachedFile));
                    objectOutputStream.writeObject(user);
                    Log.d(TAG,"存储的user:" + user.toString());

                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        if (objectOutputStream != null) {
                            objectOutputStream.flush();
                            objectOutputStream.close();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }
/**
* 读
**/
  private void recoverFromFile(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                User user = null;
                String printTxtPath = getApplicationContext().getFilesDir().getAbsolutePath()+"/user";
                File cachedFile = new File(printTxtPath + "/user.txt");
                if(cachedFile.exists()){
                    ObjectInputStream objectInputStream = null;
                    try {
                        objectInputStream = new ObjectInputStream(new FileInputStream(cachedFile));
                        user = (User) objectInputStream.readObject();
                        Log.d(TAG,"读取到的user:" + user.toString());
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (StreamCorruptedException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    }finally {
                        try {
                            if(objectInputStream!=null){
                                objectInputStream.close();
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }).start();
    }
上一篇下一篇

猜你喜欢

热点阅读