lineSeparator在Java中无法解析。

huangapple go评论86阅读模式
英文:

lineSeparator cant be resolved in Java

问题

  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.io.FileWriter;
  4. import java.io.IOException;
  5. import java.util.Scanner;
  6. public class Main {
  7. public static void main(String args[]) throws IOException {
  8. // 实例化File类
  9. String filePath = "Data.txt";
  10. // 实例化Scanner类以读取文件
  11. Scanner sc = new Scanner(new File(filePath));
  12. // 实例化StringBuffer类
  13. StringBuffer buffer = new StringBuffer();
  14. // 读取文件的每一行并将其添加到StringBuffer中
  15. while (sc.hasNextLine()) {
  16. buffer.append(sc.nextLine() + System.lineSeparator());
  17. }
  18. String fileContents = buffer.toString();
  19. // 关闭Scanner对象
  20. sc.close();
  21. String oldLine = "No_Assessment";
  22. String newLine = "Yes_Assessment";
  23. // 用新行替换旧行
  24. fileContents = fileContents.replaceAll(oldLine, newLine);
  25. // 实例化FileWriter类
  26. FileWriter writer = new FileWriter(filePath);
  27. writer.append(fileContents);
  28. writer.flush();
  29. }
  30. }

错误信息:无法解析方法'lineSeparator'中的'System'。是我的Java版本太低还是需要导入某个必要的库才能使用lineSeparator。提前致谢。

英文:
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.io.FileWriter;
  4. import java.io.IOException;
  5. import java.util.Scanner;
  6. public class Main {
  7. public static void main(String args[]) throws IOException {
  8. //Instantiating the File class
  9. String filePath = "Data.txt";
  10. //Instantiating the Scanner class to read the file
  11. Scanner sc = new Scanner(new File(filePath));
  12. //instantiating the StringBuffer class
  13. StringBuffer buffer = new StringBuffer();
  14. //Reading lines of the file and appending them to StringBuffer
  15. while (sc.hasNextLine()) {
  16. buffer.append(sc.nextLine()+System.lineSeparator());
  17. }
  18. String fileContents = buffer.toString();
  19. //closing the Scanner object
  20. sc.close();
  21. String oldLine = "No_Assessment";
  22. String newLine = "Yes_Assessment";
  23. //Replacing the old line with new line
  24. fileContents = fileContents.replaceAll(oldLine, newLine);
  25. //instantiating the FileWriter class
  26. FileWriter writer = new FileWriter(filePath);
  27. writer.append(fileContents);
  28. writer.flush();
  29. }
  30. }

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

huangapple
  • 本文由 发表于 2020年8月27日 13:51:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/63609939.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定