gpt4 book ai didi

java FileReader,逐行读取文本

转载 作者:行者123 更新时间:2023-12-02 13:16:17 28 4
gpt4 key购买 nike

我有两个类。首先是 TEXT 类:这里我读取了一个 6 行的文本文件。我只想逐行阅读,但这可行。但我想从第三行开始,并跳过最后一行,我只希望以

这是文本文件代码。

<?xml version="1.0" encoding="iso-8859-1"?>
<ICONS ERROR="false" USERNAME="WAZ" FORMAT="FLAT" RECORDS="3">
<icon ID="55" NAM="A" />
<icon ID="87" NAM="B" />
<icon ID="53" NAM="C" />
</ICONS>

这是文件读取器的代码:

    package packagechain;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import java.util.stream.Stream;

public class Text {

String fileName;
FileReader fr;
BufferedReader in;
Stream<String> lines;
Iterator<String> l;
boolean hasLine;

public Text() throws FileNotFoundException{
fileName = "E:/test30.xml";
fr = new FileReader(fileName);
in = new BufferedReader(fr);
lines = in.lines();
l = lines.iterator();
hasLine = true;
}

public String nextline() {
String nl;

if(l.hasNext()) {
nl = l.next();
//System.out.println(""+nl);

}
else {
System.out.println("No new line!");
hasLine = false;
nl=null;
}
return nl;
}
}

这是我可以从文本文件中编辑我想要的字符串的代码,我使用“子字符串”并且这有效。但如果到达最后一行,特定子字符串没有值,则会出现错误......

如果我删除文本文件中的第 1 行和第 2 行以及最后一行,则会出现错误:“线程“主”中的异常 java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:12 在 java.lang.String.substring(String.java:1963) 在 packagepackagechain.test4.main(test4.java:18)"

如果我在文本文件中添加第 1 行和第 2 行以及最后一行,则会出现错误..

错误:线程“main”中的异常 java.lang.NullPointerException 在 packagepackagechain.test4.main(test4.java:16)

and here is the code:

package packagechain;

import java.io.FileNotFoundException;

public class test4 {

public static void main(String[] args) {
Text m;
String s;

try {
m = new Text();
while(m.hasLine) {
s = m.nextline();

String r = s.substring(10,12);

System.out.println(r);

}
} catch (FileNotFoundException e) {
System.out.println("File not found!");
}


}
}

最佳答案

您的 Text 类仅读取第一行,并且您的主类为每次迭代实例化一个新的 Text 对象。您的 Text 类可以使用lines 方法来读取所有文件行,然后迭代它们并打印每一行。

package packagechain;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Iterator;
import java.util.stream.Stream;

public class Text {
String fileName;
FileReader fr;
BufferedReader in;
Stream<String> lines;
Iterator<String> l;
boolean hasLine;

public Text() throws FileNotFoundException{
fileName = "....4-line.txt";
fr = new FileReader(fileName);
in = new BufferedReader(fr);
lines = in.lines();
l = lines.iterator();
hasLine = true;
}

public String nexline() {

if(l.hasNext()) {
String nl = l.next();
System.out.println("Next line; "+nl);
return nl
}
else {
System.out.println("No new line!");
hasLine = false;
return null;
}
}
}

主类:

package packagechain;

public class MainProgram {

public static void main(String[] args) {

Text m;
String s;
try {
m = new Text();
while(m.hasLine) {
s = m.nexline();
//EXAMPLE: for each line, print a substring starting from its third character
if(s != null) System.out.println(s.substring(2));
//Here I edit in future these Line , so it's important that i get line by line from my other class, becaus I Have to edit each line itself!
}
} catch (FileNotFoundException e) {
System.out.println("File not found!");
}

}
}

关于java FileReader,逐行读取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43758731/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com