Android-Buffering值可以最小化磁盘写入次数?

时间:2014-08-22 00:07:49

标签: android buffer text-files

在我Android application我正在阅读EEG headset的大脑数据值。然后将这些值写入Text File.

这个问题是这些值的产生每秒500次。因此,他们也被写入文本文件500次,这是我不想要的。

我只想让值每秒在文本文件中显示一次。我正在阅读buffering这样做。

我如何在我的案例中使用缓冲来解决问题?

下面是我当前的Android代码,也是我想要实现的粗略伪代码。

当前的Android代码:

用于将数据保存到文件的方法:

public void writeToFileRawData(String data) {

        // creating the file where the contents will be written to
        File file = new File(dir, fileNameRaw + ".txt");

        FileOutputStream os;

        try {

            boolean append = true;

            os = new FileOutputStream(file, append);

            String writeMe = data + "\n";

            os.write(writeMe.getBytes());

            os.close();
        } catch (FileNotFoundException e) {

            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        }

    }

在Headset值的处理程序中调用上述方法:

注意:在下面的代码中,“order”无关紧要,它只是用于格式化txt的方法。

final Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {

            // msg.what determines the type of each message
            switch (msg.what) {
            case TGDevice.MSG_RAW_DATA:

                // creating the string to be written to file
                String line2 = order(seconds2 + "") + order("" + msg.arg1)
                        + "\n";

                // write the string to file
                writeToFileRawData(line2);

                break;

我想要实现的粗略伪代码:

brainWaveRaw

time    voltage
xxx     yyyy
xxx     yyyy

[and there should be 500 of these per second]
(buffer these to minimize number of disk writes)

// initialize
timeOfLastRawWrite = timeNow
rawWriteStringBuffer = ''


rawEvent(raw (list of last one or few raw samples))
        eventTime = timeNow
        for every entry r in raw
                rawWriteStringBuffer.append(printf("%d %d\n", eventTime, r))
        if timeNow-timeOfLastRawWrite > one second
        write rawWriteStringBuffer to file
        rawWriteStringBuffer = ''
        timeOfLastRawWrite = timeNow



// e.g. if last set of raw values was [123, 456, 678], arrived at time
9876

9876 123
9876 456
9876 678

1 个答案:

答案 0 :(得分:1)

  1. 每次都不要打开文件。
  2. 使用BufferedOutputStream
  3. 您可能希望每隔几次刷新一次流。

    Handler mHandler; // member of your Activity class
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final String dir = ...;
        final String fileNameRaw = ...;
    
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                // msg.what determines the type of each message
    
                switch (msg.what) {
                case TGDevice.MSG_RAW_DATA:
    
                    // creating the string to be written to file
                    String line2 = order(seconds2 + "") + order("" + msg.arg1)
                            + "\n";
    
                    // write the string to file
                    writeToFileRawData(line2);
    
                    break;
                }          
            }
    
            // members of your custom Handler class
            private File mFile = new File(dir, fileNameRaw + ".txt");
            private BufferedOutputStream mOs = new BufferedOutputStream(new FileOutputStream(mFile, true));
            private  int mWriteCnt = 0;
    
            // moved this function from Activity to your custom Handler class as well
            private void writeToFileRawData(String data) {
                try {
                    mOs.write(data.getBytes());
                    mOs.write("\n".getBytes());
                    if (++mWriteCnt == 500) {
                        mOs.flush();
                        mWriteCnt = 0;
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }