Java 需要阅读文本文件的帮助吗
我是JAVA新手,我想读取文本文件并用XML编写。以下是我的输入:Java 需要阅读文本文件的帮助吗,java,xml,file-io,Java,Xml,File Io,我是JAVA新手,我想读取文本文件并用XML编写。以下是我的输入: R-J.Roe,J.Appl.Phys。2024年(1965年)第36页 输出,但为: <ref id="1"> <label>1</label> <citation-alternatives> <mixed-citation>R.-J. Roe, J. Appl.Phys. 3
<ref id="1">
<label>1</label>
<citation-alternatives>
<mixed-citation>R.-J. Roe, J. Appl.Phys. 36, 2024 (1965).</mixed-citation>
</citation-alternatives>
</ref>
在将strLine插入元素之前,请检查strLine.endsWith(“,”),如果是,请阅读下一行(依此类推)并附加到第一行。在将strLine插入元素之前,请检查strLine.endsWith(“,”),如果是,请阅读下一行(依此类推)并附加到第一条strLine。代码在读取包含额外换行符的记录时创建两条记录的原因是您使用换行符定义记录开始的时间 您需要清楚地定义记录开始的标记。 例如,可能所有记录都以数字开头,后跟句点。也许这更容易预测:它们都是以序数开始,然后是句号。利用此逻辑,我们可以移动您创建新元素的条件:
Element ref= doc.createElement("ref");
while( (strLine = br.readLine()) != null) {
if (strLine.equals(""))
continue;
int dotIndex = strLine.indexOf(".");
num = strLine.substring(0,dotIndex);
mix = strLine.substring(dotIndex+2,strLine.length());
if(refStart(strLine)) {
ref= doc.createElement("ref");
reflist.appendChild(ref);
}
//now decide how to parse the input - maybe it will be different depending on
//whether the line we just read starts a new record or continues one from
//the previous line.
}
public boolean refStart(String line) {
if(line.length()<2)
return false;
int dotIndex = strLine.indexOf(".");
if(dotIndex<=0 || dotIndex>5) //assuming largest value is 99999
return false;
String numString = strLine.substring(0,dotIndex);
for(int i=0; i<numString.length(); i++) {
if(!Character.isDigit(numString.charAt(i) )
return false;
}
return true;
}
Element ref=doc.createElement(“ref”);
而((strLine=br.readLine())!=null){
if(strLine.equals(“”)
继续;
int dotIndex=strLine.indexOf(“.”);
num=strLine.substring(0,点索引);
mix=strLine.substring(点索引+2,strLine.length());
if(参考起动(斯特林)){
ref=doc.createElement(“ref”);
reflist.appendChild(ref);
}
//现在决定如何解析输入-可能会根据具体情况有所不同
//我们刚刚读取的行是开始一条新记录,还是继续一条新记录
//上一行。
}
公共布尔引用开始(字符串行){
如果(line.length()代码在读取包含额外换行符的记录时创建两条记录的原因是您使用换行符定义记录开始的时间
您需要清楚地定义记录开始的标记。
例如,可能所有记录都以数字开头,后跟一个句点。可能更容易预测:它们都以顺序数字开头,后跟一个句点。利用此逻辑,我们可以移动新元素的创建:
Element ref= doc.createElement("ref");
while( (strLine = br.readLine()) != null) {
if (strLine.equals(""))
continue;
int dotIndex = strLine.indexOf(".");
num = strLine.substring(0,dotIndex);
mix = strLine.substring(dotIndex+2,strLine.length());
if(refStart(strLine)) {
ref= doc.createElement("ref");
reflist.appendChild(ref);
}
//now decide how to parse the input - maybe it will be different depending on
//whether the line we just read starts a new record or continues one from
//the previous line.
}
public boolean refStart(String line) {
if(line.length()<2)
return false;
int dotIndex = strLine.indexOf(".");
if(dotIndex<=0 || dotIndex>5) //assuming largest value is 99999
return false;
String numString = strLine.substring(0,dotIndex);
for(int i=0; i<numString.length(); i++) {
if(!Character.isDigit(numString.charAt(i) )
return false;
}
return true;
}
Element ref=doc.createElement(“ref”);
而((strLine=br.readLine())!=null){
if(strLine.equals(“”)
继续;
int dotIndex=strLine.indexOf(“.”);
num=strLine.substring(0,点索引);
mix=strLine.substring(点索引+2,strLine.length());
if(参考起动(斯特林)){
ref=doc.createElement(“ref”);
reflist.appendChild(ref);
}
//现在决定如何解析输入-可能会根据具体情况有所不同
//我们刚刚读取的行是开始一条新记录,还是继续一条新记录
//上一行。
}
公共布尔引用开始(字符串行){
如果(line.length()解析文件直到找到输入的结尾,请查找)。\n
或其他内容-然后创建字符串
。解析文件直到找到输入的结尾,请查找)。\n
或其他内容-然后创建字符串
。Thorn不起作用,我无法捕捉错误:(,但非常感谢您的努力。当我选择第二个输入来运行它时,在javax.swing.AbstractButton.fireActionPerformed(未知源代码)的Main.actionPerformed(Main.java:113)的java.lang.String.substring(未知源代码)处显示异常@Thorn不起作用,我无法捕获错误:(,但非常感谢您的努力。当我选择第二个输入来运行它时,在javax.swing.AbstractButton.fireActionPerformed(未知源代码)的Main.actionPerformed(Main.java:113)的java.lang.String.substring(未知源代码)中给出了异常
Element ref= doc.createElement("ref");
while( (strLine = br.readLine()) != null) {
if (strLine.equals(""))
continue;
int dotIndex = strLine.indexOf(".");
num = strLine.substring(0,dotIndex);
mix = strLine.substring(dotIndex+2,strLine.length());
if(refStart(strLine)) {
ref= doc.createElement("ref");
reflist.appendChild(ref);
}
//now decide how to parse the input - maybe it will be different depending on
//whether the line we just read starts a new record or continues one from
//the previous line.
}
public boolean refStart(String line) {
if(line.length()<2)
return false;
int dotIndex = strLine.indexOf(".");
if(dotIndex<=0 || dotIndex>5) //assuming largest value is 99999
return false;
String numString = strLine.substring(0,dotIndex);
for(int i=0; i<numString.length(); i++) {
if(!Character.isDigit(numString.charAt(i) )
return false;
}
return true;
}