.txt文件到阵列中使用Java [英] .txt file to arrays using Java
问题描述
我有一个包含文件信息的.txt文件(1400文件)。每个文件都有一个ID,标题,作者,区域和抽象。一个示例如下:
I have a .txt file containing document information (For 1400 documents). Each document has an ID, title, author, area and abstract. A sample looks like this:
.I 1
.T
experimental investigation of the aerodynamics of a
wing in a slipstream .
.A
brenckman,m.
.B
j. ae. scs. 25, 1958, 324.
.W
experimental investigation of the aerodynamics of a
wing in a slipstream .
[...]
the specific configuration of the experiment .
我想把每到这些奉献给每类5阵列。我无法插入标题和摘要一个阵列的位置,谁能告诉我有什么不对的code?
我所试图做的是后.T插入文本行到位置x被读取并当它找到一个。一个,当它发生时,增加位置1为它填补了下一个位置停止p>
try{
collection = new File (File location);
fr = new FileReader (collection);
br = new BufferedReader(fr);
String numDoc = " ";
int pos = 0;
while((numDoc=br.readLine())!=null){
if(numDoc.contains(".T")){
while((numDoc=br.readLine())!= null && !numDoc.contains(".A")){
Title[pos] = Title[pos] + numDoc;
pos++;
}
}
}
}
catch(Exception e){
e.printStackTrace();
}
的目标是有字符串的一行内的所有信息。任何帮助将大大AP preciated。
The goal is to have all the information within a single line of String. Any help would be greatly appreciated.
推荐答案
一个code演练总是有益的。在未来,你也许可以使用断点,但我想我知道为什么你要什么,我认为是一个空指针异常。
A code walkthrough is always helpful. In the future, you can probably use breakpoints, but I think I know why you're getting what I assume is a Null Pointer Exception.
while((numDoc=br.readLine())!=null){
if(numDoc.contains(".T")){
while((numDoc=br.readLine())!= null && !numDoc.contains(".A")){
外,一切看起来不错,在这个循环中是那里的事情开始去疯狂。
Outside, everything looks good, In this loop is where the things start going bonkers.
Title[pos] = Title[pos] + numDoc;
通过您所提供的输入,我们可以设置:
With your provided input, we would set:
标题[0]
为标题[0] +的空气动力学试验研究了
这只有标题[0]存在,我不认为它已被初始化,但。我们将通过正确地检测为空数组值首先解决这一问题。这要么是有关的东西不被初始化或运行时的空指针异常的编译器错误。关闭我的头顶,我想说的编译错误。
This works only if Title[0] exists, which I don't assume it has been initialized, yet. We'll address that issue first by correctly detecting for a null array value. This would either be a compiler error about something not being initialized or a run-time null pointer exception. Off the top of my head, I want to say compiler error.
所以不管怎么说,我们会处理有关空标题[POS]
So anyways, we'll address dealing with null Title[pos].
while((numDoc=br.readLine())!=null){
if(numDoc.contains(".T")){
while((numDoc=br.readLine())!= null && !numDoc.contains(".A")){
if(Title[pos] != null) {
Title[pos] = Title[pos] + numDoc;
}
else {
Title[pos] = numDoc;
}
pos++;
}
}
}
当我们做另外一个演练中,我们会得到下面的数组值
When we do another walkthrough, we'll get the following array values
标题[0] =的空气动力学试验研究
Title[0]=experimental investigation of the aerodynamics of a
第[1] =翼滑流。
如果这个预期,那么这是罚款。如果你想的标题在一起,那么您移动 POS ++
出while循环。
If this intended, then this is fine. If you wanted the titles together, then you move the pos++
out the while loop.
while((numDoc=br.readLine())!=null){
if(numDoc.contains(".T")){
while((numDoc=br.readLine())!= null && !numDoc.contains(".A")){
if(Title[pos] != null) {
Title[pos] = Title[pos] + " " + numDoc; // add a space between lines
}
else {
Title[pos] = numDoc;
}
}
pos++;
}
}
然后我们得到:
标题[0] =实验机翼的空气动力学的滑流调查。
Title[0]=experimental investigation of the aerodynamics of a wing in a slipstream .
您可能要修剪你的投入,但这应该涵盖,我可以看到潜在的错误。
You may want to trim your inputs, but this should cover both of the potential errors that I can see.
这篇关于.txt文件到阵列中使用Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!