如何读取/写入超过200MB的文件 [英] How to read/write a file that is more than 200MB

查看:87
本文介绍了如何读取/写入超过200MB的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读并阅读了一些有关读写文件txt的指南,但是在我的项目中存在一些小问题,例如,我的文件大于200MB(是xml文件);无论如何,我想读取我的文件,一部分代码是这样的:

I have seen and read some guide about reading and writing file txt, but in my project there are some little problem,for example my file is more then 200MB(is a xml file); anyway I want to read my file, a part of code is this:

节点ID ="1653281475". lat ="44.499773" lon ="11.350111"版本="1"

node id="1653281475" lat="44.499773" lon="11.350111" version="1"

timestamp ="2012-02-29T20:15:15Z" changeset ="10831749" uid ="91650"用户="Alberto58"

timestamp="2012-02-29T20:15:15Z" changeset="10831749" uid="91650" user="Alberto58">

tag k="barrier" v="gate"/>
tag k="bicycle" v="yes"/>
tag k="foot" v="yes"/>
tag k="car" v="yes"/>

节点/ 其他节点: 节点ID ="16532843433"等等.........

node/ Other node: node id="16532843433" etc..........

我只想搜索节点ID为"1653281475"的节点,并仅修改标签k ="foot" v =是"变成k =英尺" v =否"包含在此节点中.

I want to search only the node id "1653281475" and modify only the tag k="foot" v="yes" into k="foot" v="no" that is contain in this node.

我该怎么做?

这是我第二天尝试读写xml文档(现在我使用的是654byte xml)employees.xml,我遵循了很多指南,但是我不知道该怎么做,例如,我遵循此指南:

It is the second days that I am trying to read and write my xml document(now I am using a little 654byte xml)employees.xml, I have follow so many guide but I don't understand how I have to do, for example I follow this guide:

http://www.journaldev.com/1198/java-sax-parser-example-tutorial-to-parse-xml-to-list-of-objects

因此,我在目录名称sax中创建了3个Java文件:Employee.java,MyHandler.java,XMLParserSAX.java(在女巫中有一些错误,代码与我链接的页面相同)

So I create 3 java file that are located in a directory name sax: Employee.java, MyHandler.java,XMLParserSAX.java(in witch there is some errors, the code is the same of the page that I have link)

当我使用命令(我在Ubuntu 12.04中)javac sax/*时,没有问题 但是,当我使用java sax.XMLParserSAX employee.xml时,有很多问题.首先,代码中" import com.journaldev.xml.Employee;" ->找不到错误. 另一个错误是相同的,但在其他地方,例如"MyHandler handler = new MyHandler();" ...

When I use the command(I am in Ubuntu 12.04) javac sax/* there is no problem but when I use java sax.XMLParserSAX employees.xml there are so many problem; first of all in the code there is an error at "import com.journaldev.xml.Employee;"-->cannot find symbol. And the other error are the same but in other place like "MyHandler handler = new MyHandler();"...

我认为问题在于XMLPArserSAX无法识别Employee类... 我该怎么办? =(

I think that the problem is that the the XMLPArserSAX don't recognizes the class Employee... What I have to do? =(

推荐答案

请勿将整个文件加载到内存中. SAX解析器正是用于这种用法.使用SAX解析器读取文件.读取每个标签时,请继续将其写入输出文件-直到看到node id="1653281475".当您看到此节点时,请对其进行修改并将其写入输出文件.然后继续将节点写入输出文件,直到结束.在此过程结束时,输出文件中将包含修改后的XML.

Do not load the whole file in memory. SAX parsers are made for exactly this kind of usage. Use a SAX parser to read the file. As you read each tag keep writing it to the output file - until you see node id="1653281475". When you see this node, modify it and write it to the output file. Then continue writing the nodes to output file till end. Output file will have the modified XML in it at the end of this process.

这篇关于如何读取/写入超过200MB的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆