lineSeparator在Java中无法解析。

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

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

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:

确定