简体   繁体   English

如何在不每次关闭输出流对象的情况下向文件添加字符串

[英]How to add string to a file without closing the output stream objects every time

I will receive a large chunk of data (say 1000 data/second, each data has minimum size of 15 bytes). 我将收到大量数据(例如1000数据/秒,每个数据的最小大小为15字节)。 My earlier approach was to create a new outputstream object every time, specify the path and add the values to the file, all this is done in a separate thread. 我以前的方法是每次创建一个新的outputstream对象,指定路径并将值添加到文件,所有这些操作都是在单独的线程中完成的。 How ever I am still facing a performance hit. 我如何仍然会遇到性能问题。 I taught instead of writing the data to a file as 我教而不是像这样将数据写入文件

File dir = new 
File(android.os.Environment.getExternalStorageDirectory().getAbsolutePath() + DEBUG_FILE_PATH);
boolean b = dir.mkdirs();
try
{
    fileOutputStream = new FileOutputStream(new File(dir, FILE_NAME), 
    true);
    outputStreamWriter = new OutputStreamWriter(fileOutputStream);
}
catch (FileNotFoundException e)
{
    e.printStackTrace();
}

outputstreamwriter.append("some data").close(); 

I want to maintain the outputstreamwriter and other objects and use them to add the data to the outputstreamwriter buffer, and at the end of my application(when i close the app, may at onDestroy() method of activity). 我想维护outputstreamwriter和其他对象,并使用它们将数据添加到outputstreamwriter缓冲区中,并在我的应用程序结尾处(当我关闭应用程序时,可能处于onDestroy()活动方法中)。 I need to write the data to the file and then close all the open stream. 我需要将数据写入文件,然后关闭所有打开的流。

This approach works for me, but the buffer size for outputstreamwriter is 8kb only. 这种方法对我有用,但是outputstreamwriter的缓冲区大小仅为8kb。 Which is less compared to the amount of data that I am receiving. 与我收到的数据量相比,这要少得多。

How can i solve this ? 我该如何解决?

The vast majority of your performance hit is most probably in opening the file every single time you want to write a few bytes to it. 性能上的绝大多数损失很可能是每次您想向文件写入几个字节时打开文件。

So, if you just eliminate the opening and closing of the file all the time, you should be fine. 因此,如果您只是一直取消打开和关闭文件的操作,那应该没问题。

Just open the file once, keep writing data to it as the data arrives, and then close the file when your application closes. 只需打开文件一次,在数据到达时继续向其中写入数据,然后在应用程序关闭时关闭文件。

This way, using a buffered OutputStreamWriter will give you a performance benefit without having to worry about the size of its buffer: when its buffer is full, it will flush itself, transparently to you. 这样,使用缓冲的OutputStreamWriter将为您带来性能上的好处,而不必担心其缓冲区的大小:当其缓冲区已满时,它将对您透明地进行刷新。 You don't need to know anything about how it works and how large (or small) its buffer is. 您无需了解其工作原理以及缓冲区的大小(或大小)。

This solved my problem. 这解决了我的问题。 By this approach I have opened the file once when the app starts and, I am adding the 'n' values which I receive from the service into the file. 通过这种方法,我在应用启动时打开了文件一次,并将从服务接收到的“ n”值添加到文件中。 I am flushing the buffer(this writes the data to file). 我正在刷新缓冲区(这会将数据写入文件)。 Now even if I receive large data not more than 8kb(buffer max size) I can write it to file which is already opened. 现在,即使收到的最大数据不超过8kb(最大缓冲区大小),我也可以将其写入已打开的文件中。 Finally I am closing the streams when the app is closed. 最后,当应用程序关闭时,我将关闭流。

//Util class
public static File dir;
public static FileOutputStream fileOutputStream;
public static OutputStreamWriter outputStreamWriter;

//Different class, you can initialize it on your Application class or home activity
private void initializeFileWriteObjects()
{
    dir = new File(android.os.Environment.getExternalStorageDirectory().getAbsolutePath() + DEBUG_FILE_PATH);
    boolean b = dir.mkdirs();
    try
    {
        fileOutputStream = new FileOutputStream(new File(dir, FILE_NAME), true);
    }
    catch (FileNotFoundException e)
    {
        e.printStackTrace();
    }
    outputStreamWriter = new OutputStreamWriter(fileOutputStream);
}

//Util File
private static boolean writeToFile(final byte[] stream)
{
    //Convert String to hex, as I want this to be in hex
    final String stringData = byteArrayToHexString(stream);

    try
    {
        outputStreamWriter.append(stringData);
        outputStreamWriter.flush();
    }
    catch (IOException e)
    {
        e.printStackTrace();
        return false;
    }
    return true;
}

//When the app is closed.
@Override
protected void onDestroy()
{
    super.onDestroy();
    closeFileStream();
}

//This method is in same Util class, but called at onDestroy()
public static void closeFileStream()
{
    try
    {
        outputStreamWriter.close();
        fileOutputStream.close();
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM