如何使用 BufferedReader.readLine() 方法从输入流中读取 XML 格式的文本?
我有一个 xml 文件,我将其作为资源包含在我的 netbeans 项目中。
现在,我尝试使用输入流阅读器逐行阅读它:
static InputStream nudeMap = Main.class.getResourceAsStream("overlay_map_2007.txt");
static BufferedReader br = new BufferedReader(new InputStreamReader(nudeMap,"UTF-8"));
=>这遇到了错误:
Exception in thread "Thread-4" java.lang.NullPointerException
at java.io.Reader.<init>(Reader.java:61)
at java.io.InputStreamReader.<init>(InputStreamReader.java:80)
我检查了文件的编码,它确实是UTF-8,所以我不认为这是编码问题。我在这里没有经验,但我怀疑这可能是因为该文件实际上是 xml 格式的。第一行是:
<?xml version="1.0" encoding="UTF-8"?>
<gexf xmlns:viz="http:///www.gexf.net/1.1draft/viz" version="1.1" xmlns="http://www.gexf.net/1.1draft">
我的观点是,我不想为我对文件的使用编写解析器。您知道我如何将其作为纯旧文本文件读取而不会出现错误吗?谢谢!
[编辑]:明确地说:我想用 br.readLine(); 读取此文件不是用 Java 解析器!
I have an xml file which I included as a resource in my netbeans project.
Now, I try to read it line by line with an inputstream reader:
static InputStream nudeMap = Main.class.getResourceAsStream("overlay_map_2007.txt");
static BufferedReader br = new BufferedReader(new InputStreamReader(nudeMap,"UTF-8"));
=> this get met the error:
Exception in thread "Thread-4" java.lang.NullPointerException
at java.io.Reader.<init>(Reader.java:61)
at java.io.InputStreamReader.<init>(InputStreamReader.java:80)
I checked the encoding of the file and it is indeed UTF-8, so I don't think it is an encoding problem. I have no experience here, but I suspect it might come from the fact that the file is actually xml formatted. First lines are:
<?xml version="1.0" encoding="UTF-8"?>
<gexf xmlns:viz="http:///www.gexf.net/1.1draft/viz" version="1.1" xmlns="http://www.gexf.net/1.1draft">
My point is, I don't want to write a parser for the use I have of the file. Do you have any clue about how I could read it as a plain old text file, without errors? Thx!
[EDIT]: to make clear: I want to read this file with br.readLine(); not with a Java Parser!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在我看来,你的文本文件没有被发现。换句话说,我猜测
getResourceAsStream
返回null
,而这个 null 值导致您收到 NullPointerException。您的项目中的
overlay_map_2007.txt
文件位于何处?如果此文件不在“默认”包中,那么您需要“限定”资源的名称。例如,如果它位于文件夹
com.example.myproject
中,则资源名称将为com/example/myproject/overlay_map_2007.txt
。It looks to me like that your textfile isn't being found. In other words, I would guess that
getResourceAsStream
is returningnull
, and this null value is causing the NullPointerException you're getting.Where is the
overlay_map_2007.txt
file within your project?If this file isn't in the 'default' package, then you need to need to 'qualify' the name of the resource. For example, if it lies within a folder
com.example.myproject
, the resource name would becom/example/myproject/overlay_map_2007.txt
.我认为你甚至不需要走那么远。一个简单的 Google 搜索就得到了这个带有 DOM 解析器的优秀示例:
http://www.mkyong.com/java/how-to-read-xml-file-in-java-dom-parser/
反对 DOM,那就是 SAX:
<一个href="http://www.mkyong.com/java/how-to-read-xml-file-in-java-sax-parser/" rel="nofollow">http://www.mkyong.com/ java/如何在java-sax-parser中读取xml文件/
I don't think you'll even have to go that far. A simple Google search yielded this fine example with its DOM Parser:
http://www.mkyong.com/java/how-to-read-xml-file-in-java-dom-parser/
Have a thing against DOM, then there's SAX:
http://www.mkyong.com/java/how-to-read-xml-file-in-java-sax-parser/