英文:
lineSeparator cant be resolved in Java
问题
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class Main {
public static void main(String args[]) throws IOException {
// 实例化File类
String filePath = "Data.txt";
// 实例化Scanner类以读取文件
Scanner sc = new Scanner(new File(filePath));
// 实例化StringBuffer类
StringBuffer buffer = new StringBuffer();
// 读取文件的每一行并将其添加到StringBuffer中
while (sc.hasNextLine()) {
buffer.append(sc.nextLine() + System.lineSeparator());
}
String fileContents = buffer.toString();
// 关闭Scanner对象
sc.close();
String oldLine = "No_Assessment";
String newLine = "Yes_Assessment";
// 用新行替换旧行
fileContents = fileContents.replaceAll(oldLine, newLine);
// 实例化FileWriter类
FileWriter writer = new FileWriter(filePath);
writer.append(fileContents);
writer.flush();
}
}
错误信息:无法解析方法'lineSeparator'中的'System'。是我的Java版本太低还是需要导入某个必要的库才能使用lineSeparator。提前致谢。
英文:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class Main {
public static void main(String args[]) throws IOException {
//Instantiating the File class
String filePath = "Data.txt";
//Instantiating the Scanner class to read the file
Scanner sc = new Scanner(new File(filePath));
//instantiating the StringBuffer class
StringBuffer buffer = new StringBuffer();
//Reading lines of the file and appending them to StringBuffer
while (sc.hasNextLine()) {
buffer.append(sc.nextLine()+System.lineSeparator());
}
String fileContents = buffer.toString();
//closing the Scanner object
sc.close();
String oldLine = "No_Assessment";
String newLine = "Yes_Assessment";
//Replacing the old line with new line
fileContents = fileContents.replaceAll(oldLine, newLine);
//instantiating the FileWriter class
FileWriter writer = new FileWriter(filePath);
writer.append(fileContents);
writer.flush();
}
}
Error I get : Cannot resolve method 'lineSeparator' in 'System'. Is that my java version too low or any needed library has to be imported to use lineSeparator. Thx in advance.
答案1
得分: 1
哪个Java版本正在被使用?
System.lineSeparator()
在1.7版本中被添加。
英文:
Which java version are you using?
System.lineSeparator()
was added in 1.7
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论