将文本文件扫描到一个数组中并省略一个指定行
Scanning a text file into an array and omitting one specified line
我是初学者,需要一些帮助。我正在尝试将文本文件逐行扫描到数组中,但省略了一行。我的文本文件是
我是
你是
他是
她是
是
我想创建一个方法来扫描它并将元素放入一个数组中,其中一行有例外(通过输入字符串作为方法的参数来选择)。然后擦除原始文本文件并在那里打印创建的数组(不包括删除的那一行)。对不起,我懒得解释了。
我试过这个:
public static void deleteLine(String name, String line) throws IOException {
String sc = System.getProperty("user.dir") + new File("").separator;
FileReader fr = new FileReader(sc + name + ".txt");
Scanner scan = new Scanner(fr);
int n = countLines(name); // a well working method returning the number if lines in the file (here 5)
String[] listArray = new String[n-1];
for (int i = 0; i < n-1; i++) {
if (scan.hasNextLine() && !scan.nextLine().equals(line))
listArray[i] = scan.nextLine();
else if (scan.hasNextLine() && scan.nextLine().equals(line))
i--;
else continue;
}
PrintWriter print = new PrintWriter(sc + name + ".txt");
print.write("");
for (int i = 0; i < n-2; i++) {
print.write(listArray[i] + "\n");
}
print.close()
}
当我输入:deleteLine("all_names","you are")(all_names是文件名)时,我得到一个错误"Line not found"。我确定问题出在 for 循环中,但我不知道为什么这不起作用。 :(
//已解决//
这段代码终于起作用了。谢谢解答!
public static void deleteLine(String name, String line) throws IOException{
String sc = System.getProperty("user.dir") + new File("").separator;
FileReader fr = null;
fr = new FileReader(sc+name+".txt");
Scanner scan = new Scanner(fr);
int n = LineCounter(name);
String[] listArray = new String[n-1];
for (int i = 0; i < n-1; i++) {
if (scan.hasNextLine()) {
String nextLine = scan.nextLine();
if (!nextLine.equals(line)) {
listArray[i] = nextLine;
}
else i--;
}
}
PrintWriter print = new PrintWriter(sc+name+".txt");
print.write("");
for(int i=0;i<n-1;i++){
print.write(listArray[i]+System.lineSeparator());
}
print.close();
}
看看您是如何比较 String 对象的。您应该使用 equals
方法来比较字符串的 content。使用 ==
和 !=
等运算符比较字符串 对象 是否相同。
现在在正确使用 equals
之后,看看您是如何使用 nextLine
的。检查其 Javadoc
您在比较时阅读了两次行 scan.nextLine() ,因此您 运行 超出了行。
用这个或类似的循环替换你的循环
for (int i = 0; i < n; i++) {
if (scan.hasNextLine()) {
String nextLine = scan.nextLine();
if (nextLine.equals(line)) {
listArray[i] = nextLine;
}
}
}
我觉得 LineCounter(name) 有效,因为您没有在其中放置“.txt”。尝试从 Filereader 和 Printwriter 对象中的文件名中删除“.txt”扩展名,看看它是否有效。通常在 windows 中,扩展名不是文件名的一部分。
这里有一个替代的(更简单的)解决方案,使用更容易理解的代码来做你想做的事。 (我认为)
也避免了多次
循环,但使用单个 Java 8 流来过滤。
public static void deleteLine(String name, String line) throws IOException {
List<String> lines = Files.readAllLines(Paths.get(name));
lines = lines.stream().filter(v -> !v.equals(line)).collect(Collectors.toList());
System.out.println(lines);
// if you want the String[] - but you don't need it
String[] linesAsStringArr = new String[lines.size()];
linesAsStringArr = lines.toArray(linesAsStringArr);
// write the file using our List<String>
Path out = Paths.get("output.txt"); // or another filename you dynamically create
Files.write(out, lines, Charset.forName("UTF-8"));
}
我是初学者,需要一些帮助。我正在尝试将文本文件逐行扫描到数组中,但省略了一行。我的文本文件是
我是
你是
他是
她是
是
我想创建一个方法来扫描它并将元素放入一个数组中,其中一行有例外(通过输入字符串作为方法的参数来选择)。然后擦除原始文本文件并在那里打印创建的数组(不包括删除的那一行)。对不起,我懒得解释了。
我试过这个:
public static void deleteLine(String name, String line) throws IOException {
String sc = System.getProperty("user.dir") + new File("").separator;
FileReader fr = new FileReader(sc + name + ".txt");
Scanner scan = new Scanner(fr);
int n = countLines(name); // a well working method returning the number if lines in the file (here 5)
String[] listArray = new String[n-1];
for (int i = 0; i < n-1; i++) {
if (scan.hasNextLine() && !scan.nextLine().equals(line))
listArray[i] = scan.nextLine();
else if (scan.hasNextLine() && scan.nextLine().equals(line))
i--;
else continue;
}
PrintWriter print = new PrintWriter(sc + name + ".txt");
print.write("");
for (int i = 0; i < n-2; i++) {
print.write(listArray[i] + "\n");
}
print.close()
}
当我输入:deleteLine("all_names","you are")(all_names是文件名)时,我得到一个错误"Line not found"。我确定问题出在 for 循环中,但我不知道为什么这不起作用。 :(
//已解决//
这段代码终于起作用了。谢谢解答!
public static void deleteLine(String name, String line) throws IOException{
String sc = System.getProperty("user.dir") + new File("").separator;
FileReader fr = null;
fr = new FileReader(sc+name+".txt");
Scanner scan = new Scanner(fr);
int n = LineCounter(name);
String[] listArray = new String[n-1];
for (int i = 0; i < n-1; i++) {
if (scan.hasNextLine()) {
String nextLine = scan.nextLine();
if (!nextLine.equals(line)) {
listArray[i] = nextLine;
}
else i--;
}
}
PrintWriter print = new PrintWriter(sc+name+".txt");
print.write("");
for(int i=0;i<n-1;i++){
print.write(listArray[i]+System.lineSeparator());
}
print.close();
}
看看您是如何比较 String 对象的。您应该使用 equals
方法来比较字符串的 content。使用 ==
和 !=
等运算符比较字符串 对象 是否相同。
现在在正确使用 equals
之后,看看您是如何使用 nextLine
的。检查其 Javadoc
您在比较时阅读了两次行 scan.nextLine() ,因此您 运行 超出了行。
用这个或类似的循环替换你的循环
for (int i = 0; i < n; i++) {
if (scan.hasNextLine()) {
String nextLine = scan.nextLine();
if (nextLine.equals(line)) {
listArray[i] = nextLine;
}
}
}
我觉得 LineCounter(name) 有效,因为您没有在其中放置“.txt”。尝试从 Filereader 和 Printwriter 对象中的文件名中删除“.txt”扩展名,看看它是否有效。通常在 windows 中,扩展名不是文件名的一部分。
这里有一个替代的(更简单的)解决方案,使用更容易理解的代码来做你想做的事。 (我认为)
也避免了多次 循环,但使用单个 Java 8 流来过滤。
public static void deleteLine(String name, String line) throws IOException {
List<String> lines = Files.readAllLines(Paths.get(name));
lines = lines.stream().filter(v -> !v.equals(line)).collect(Collectors.toList());
System.out.println(lines);
// if you want the String[] - but you don't need it
String[] linesAsStringArr = new String[lines.size()];
linesAsStringArr = lines.toArray(linesAsStringArr);
// write the file using our List<String>
Path out = Paths.get("output.txt"); // or another filename you dynamically create
Files.write(out, lines, Charset.forName("UTF-8"));
}