[英]How to read text from an inputstream, formatted as XML, with a BufferedReader.readLine() method?
I have an xml file which I included as a resource in my netbeans project. 我有一个xml文件,该文件作为资源包含在我的netbeans项目中。
Now, I try to read it line by line with an inputstream reader: 现在,我尝试使用inputstream读取器逐行读取它:
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. 我检查了文件的编码,它的确是UTF-8,所以我认为这不是编码问题。 I have no experience here, but I suspect it might come from the fact that the file is actually xml formatted. 我在这里没有经验,但是我怀疑这可能是由于该文件实际上是xml格式。 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(); [编辑]:要明确:我想使用br.readLine()读取此文件; not with a Java Parser! 不能使用Java解析器!
It looks to me like that your textfile isn't being found. 在我看来,找不到您的文本文件。 In other words, I would guess that getResourceAsStream
is returning null
, and this null value is causing the NullPointerException you're getting. 换句话说,我猜想getResourceAsStream
返回null
,而这个null值导致您得到NullPointerException。
Where is the overlay_map_2007.txt
file within your project? 您项目中的overlay_map_2007.txt
文件在哪里?
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 be com/example/myproject/overlay_map_2007.txt
. 例如,如果它位于文件夹com.example.myproject
,则资源名称将为com/example/myproject/overlay_map_2007.txt
。
I don't think you'll even have to go that far. 我认为您甚至不必走那么远。 A simple Google search yielded this fine example with its DOM Parser: 一个简单的Google搜索通过其DOM Parser产生了一个很好的例子:
http://www.mkyong.com/java/how-to-read-xml-file-in-java-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: 对DOM有一些看法,然后就是SAX:
http://www.mkyong.com/java/how-to-read-xml-file-in-java-sax-parser/ http://www.mkyong.com/java/how-to-read-xml-file-in-java-sax-parser/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.