简体   繁体   English

Java FileWriting和Raspberry Pi

[英]Java FileWriting and Raspberry Pi

I have been making a Java program for a Raspberry PI and I have reached a problem. 我一直在为Raspberry PI开发Java程序,但遇到了问题。

The program reads data from a device and saves it on an XML file using DOM. 该程序从设备读取数据,然后使用DOM将其保存在XML文件中。 My problem is that as we add information to the XML it becomes incrementally slower to write more information. 我的问题是,当我们将信息添加到XML时,写入更多信息变得越来越慢。 We're talking about as soon as it has something as insignificant as 10 entries it becomes a huge bottleneck so much so that it eventually takes longer to write the XML than the frequency to read the data off the device. 我们正在谈论的是,一旦它具有10个条目之类的微不足道的东西,它就会成为巨大的瓶颈,以至于最终写入XML所需的时间要比从设备读取数据的时间长。

I'm thinking the problem is that it rewrites the whole file with every new writing it needs to do. 我在想问题是它需要用每一次新的写入来重写整个文件。 So I read the whole XML, add the entry I want and write it all back. 因此,我阅读了整个XML,添加了我想要的条目并将其写回。 I need a way to just add the new entry on the file without it reading all the data. 我需要一种只在文件上添加新条目而不读取所有数据的方法。

I looked for a XML API that allowed this but can't find it. 我寻找了一个允许这样做但找不到的XML API。

So my questions are: 所以我的问题是:

  • I was told DOM is very slow so is this a problem because I'm using DOM? 有人告诉我DOM非常慢,所以这是一个问题,因为我正在使用DOM? Or is this just a result of slow reading rates from the Raspberry PI. 还是这是Raspberry PI读取速度慢的结果。

  • Can you recomend a very light weight XML API that allows just adding and reading specific sections of the XML file? 您是否可以推荐一个非常轻量的XML API,该API仅允许添加和读取XML文件的特定部分? Is that what is meant when it's said that Stax stream XML Files? 当说Stax流XML文件时,这是什么意思?

  • I was rewriting everything to json (because I was told it's much faster) but while doing so I'm wondering if my problem won't persist because I will inevitably read the whole file and write the whole file again. 我正在将所有内容重写为json(因为被告知它要快得多),但是这样做的时候我想知道我的问题是否不会持续,因为我不可避免地会读取整个文件并再次写入整个文件。

  • What is the most lightweight way to append data to a text file? 将数据追加到文本文件的最轻巧的方法是什么? The data will always be added at the end of the file so i'm wondering if just using plain old text files wouldn't be the best solution? 数据将始终添加在文件的末尾,所以我想知道是否仅使用纯旧文本文件不是最佳解决方案?

If I understand correctly, you're trying to update an XML file every time some event happens. 如果我理解正确,那么您将在每次发生某些事件时尝试更新 XML文件。 That is, when an event happens, you: 也就是说,当事件发生时,您:

  • Read the XML file into a DOM. 将XML文件读入DOM。
  • Modify some element of the DOM. 修改DOM的某些元素。
  • Write the XML file to file. 将XML文件写入文件。

This isn't playing to XML's strengths, and you would be better off using a file format that lends itself to random access writes. 这没有发挥XML的优势,而使用适合随机访问写入的文件格式会更好。 Look into JDBM2, or even a lightweight SQL database like H2. 研究JDBM2,甚至是像H2这样的轻量级SQL数据库。

If you really must have an XML file, you could consider reading it in once at the start of your program, keeps the DOM in memory, writing to file (if changed) on a schedule, in a separate thread to the thread updating it. 如果确实需要一个XML文件,则可以考虑在程序开始时一次读取它,将DOM保留在内存中,按计划写入文件(如果已更改),并在单独的线程中进行更新。

If you take this approach, use synchronized methods to ensure that the DOM isn't modified during a file write. 如果采用这种方法,请使用synchronized方法来确保在文件写入期间不修改DOM。

I can attest to the speed of JSon with the raspberry pi. 我可以用树莓派证明JSon的速度。 Even reading in the entire file and writing out in JSon will be much faster than DOM. 即使读入整个文件并用JSon写出也比DOM快得多。

My application reads in >500 JSon strings, and writes about the same amount back in less than a second. 我的应用程序读取了超过500个JSon字符串,并在不到一秒钟的时间内写了差不多相同的数量。 If you do not need to update data, then simply opening a file for writing with the tags "ab" will allow you to append to the file, not overwriting the exiting data. 如果您不需要更新数据,则只需打开一个带有“ ab”标签的文件即可添加到文件中,而不会覆盖现有数据。

事实证明,对于我正在做的事情而言,XML编写实际上足够快,并且正在减慢整个系统的速度并造成瓶颈,这是我正在写数据库,但是当没有互联网时,连接超时约为2分钟,因此线程将开始堆积,等待超时结束,这样它们便可以将数据写入XML文件。

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

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