Java 读取、扫描、修改文本文件,并将修改后的文本放入新文件
我尝试读取一个文本文件并尝试修改它。我从StackOverflow得到了很多讨论,以下是内容: 10250号 第10251号 作品1026 0 EndRow 我想要的已修改文本文件: 0号 和1 作品0 EndRow 我读了一些关于它的讨论主题,然后得出结论,我必须使用Java 读取、扫描、修改文本文件,并将修改后的文本放入新文件,java,.net,string,text,Java,.net,String,Text,我尝试读取一个文本文件并尝试修改它。我从StackOverflow得到了很多讨论,以下是内容: 10250号 第10251号 作品1026 0 EndRow 我想要的已修改文本文件: 0号 和1 作品0 EndRow 我读了一些关于它的讨论主题,然后得出结论,我必须使用.hasNextLine方法检查每一行。代码如下: import java.io.*; import java.util.Scanner; public class MainConvert { /** * @nahdiya
.hasNextLine
方法检查每一行。代码如下:
import java.io.*;
import java.util.Scanner;
public class MainConvert {
/**
* @nahdiya
*/
public static void main(String[] args) {
try {
File readNet = new File("testttt.net");
FileReader readFileNet = new FileReader(readNet);
BufferedReader reader = new BufferedReader(readFileNet);
Scanner scan = new Scanner("testttt.net");
PrintWriter fileConvert = new PrintWriter("convertNet.txt");
while (scan.hasNextLine()) {
String check = scan.next();
String checkLine = scan.nextLine();
if (checkLine.contains("NO 1025")) {
if(checkLine.contains("NO 1025")) {
fileConvert.println("AND "+check );
} else if (checkLine.contains("OP 1026")) {
fileConvert.println("OP"+check);
} else {
fileConvert.println(checkLine);}
}
}
}
reader.close();
fileConvert.close();
} catch(Exception ex) {
ex.printStackTrace();
}
}
当我尝试运行该类时,输出消息如下所示:
java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown Source)
at fileConvertSave.MainConvert.main(MainConvert.java:21)
问题是:
PrintWriter fileConvert = new PrintWriter("convertNet.txt");
这条线有什么问题?我只想修改testttt.net文件<代码>文件转换必须创建为新文件。有什么问题吗?编辑:查看底部的完整解决方案: 产生错误消息的原始问题是扫描仪试图在不存在的行上执行nextLine(),原因是:
String check = scan.next();
String checkLine = scan.nextLine();
当你打电话时:
while( scan.hasNextLine() )
还有下一行。然后您可以拨打:
scan.next();
scan.nextLine()
在这一点上,可能不再有“下一行”可用。然后您可以拨打:
scan.next();
scan.nextLine()
和繁荣
删除线路
String check = scan.next();
应该有用
编辑:
这是问题所有其他部分的解决方案。。。这基本上是一个完整的重写你所拥有的,所以请阅读所有的代码,了解它的功能,并尝试理解它!如果有疑问,请在提问前先阅读文档
BufferedReader reader = null;
PrintWriter writer = null;
try {
reader = new BufferedReader(new FileReader("testttt.txt"));
writer = new PrintWriter("convertNet.txt");
// setup the pattern that we want to find and replace in the input:
// NB> this is a regex (or regular expression)
// it means, find [space] then either 1025 or 1026 then [space]
String patternToMatch = " (1025|1026) ";
String inputLine = null;
// while there are lines to read, read them one at a time:
while ((inputLine = reader.readLine()) != null) {
// create the outputLine which is the input line with our pattern
// " 1025 " or " 1026 " replaced by just a single space:
String outputLine = inputLine.replaceFirst(patternToMatch, " ");
// log the transformation for debugging purposes:
System.out.println(inputLine + " -> " + outputLine);
// write the outputLine to the output file:
writer.println(outputLine);
}
}
catch (FileNotFoundException ex) {
System.out.println("file was not found: " + ex);
}
catch (IOException ex ) {
System.out.println("io error: " + ex);
}
finally {
try {
if( reader != null ) reader.close();
if ( writer != null ) writer.close();
} catch (IOException ex) {
System.out.println("error closing file " + ex);
}
}
请注意,即使出现异常,finally块也会很好地清理。还有一种更新的方法可以做到这一点,它可以使代码缩短一点,称为try with resources:
您的字符串看起来不一致,如果有更多类似这样的字符串和Bufferreader来读取行,我建议您使用正则表达式,虽然我没有使用正则表达式,但这是我想到的
public static void main(String[] args) {
try {
File readNet = new File("testttt.net");
FileReader readFileNet = new FileReader(readNet);
BufferedReader reader = new BufferedReader(readFileNet);
PrintWriter fileConvert = new PrintWriter("convertNet.txt");
String r = null;
while ((r = reader.readLine()) != null) {
System.out.println(r);
if (r.equals("NO 1025 1")) {
fileConvert.println(r.replace(r, "AND 1"));
} else if (r.contains("1025 0")) {
fileConvert.println(r.replaceAll("1025", ""));
} else if (r.contains("1026")) {
fileConvert.println(r.replaceAll("1026", ""));
} else {
fileConvert.println(r);
}
}
reader.close();
fileConvert.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
祝你好运,我希望这对你有帮助。我不认为那条线路有问题。请确认这是第21行string check=scan.next();字符串checkLine=scan.nextLine();问题在于它能起作用!谢谢,但是…convertNet.txt仍然不包含任何内容。它仍然是一个空白的…文件。但是,我将字符串line=reader.readline()写入文件,它就可以工作了。它包含与testttt.net相同的内容,这不是我想要的结果…好的,您得到的原始错误是由于扫描仪造成的,但是我现在用注释重写了整个内容,以满足您的原始要求,并解释它是如何工作的。请阅读所有代码,了解每行代码的作用和原因。如果您有任何疑问,请查阅文档。似乎您直接替换了旧文档。很有效,谢谢。但问题是,1025和OP 1026后面的数字不是固定数字。他们可以改变。所以这个模式就像1025x(X是从1到16的任意整数)。我只想更改“NO 1025”和“OP 1026”,而不是它们后面的号码。这就是为什么我使用next()扫描空格后的下一个单词。还是我弄错了?这是我第一次听说regex,我对它做了一些研究,似乎值得一试。谢谢你提供的信息:)@Immanuel