是否有任何功能可与列表一起使用以跳转到下一行?

Is there any function to use with list to jump next line?

尝试读取文本文件并将内容添加到数组列表,而不是写入新的文本文件,但其显示格式与原始文件不同。 我希望它与原始文件的格式相同。

public static void main(String args[]) throws IOException
{

    Scanner sc1 = new Scanner(new File("C:\Users\amank\eclipse-workspace\DataStructures\src\sample1-pp.txt")); 

    ArrayList<String> list = new ArrayList<String>();
    //testing
    FileWriter writer = new FileWriter ("C:\Users\amank\eclipse-workspace\DataStructures\src\output.txt");; 

    if(!sc1.hasNext())
    {
        System.out.println("File is empty");
    }

    try {
        while (sc1.hasNext())
        {

            list.add(sc1.nextLine().trim().toLowerCase().replaceAll("\p{P}", ""));


        }
        sc1.close();    

        //for testing

        for(String str: list)
        {
            writer.write(str);
        }
        writer.close();

预计

傲慢与偏见

简·奥斯丁

第 1 章

单身汉有福必缺妻,这是世人公认的真理。

======

实际

傲慢与偏见by jane austen 第1章单身男人有福必缺妻,这是举世公认的真理

为什么要从文件中读取行,将它们转换为小写,修剪它们然后将它们添加到 ArrayList...? 只需按原样将行添加到 ArrayList

   public static void main(String args[]) {
    try {
        Scanner sc1 = new Scanner(new File("C:\Users\amank\eclipse-workspace\DataStructures\src\sample1-pp.txt"));

        ArrayList<String> list = new ArrayList<String>();
        //testing
        FileWriter writer = new FileWriter("C:\Users\amank\eclipse-workspace\DataStructures\src\output.txt");;

        if (!sc1.hasNext()) {
            System.out.println("File is empty");
        }

        while (sc1.hasNext()) {

            list.add(sc1.nextLine());

        }
        sc1.close();

        //for testing
        for (String str : list) {
            writer.write(str);
        }
        writer.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

写入文件时需要添加行分隔符

public static void main(String args[]) throws IOException{

Scanner sc1 = new Scanner(new File("C:\Users\amank\eclipse-workspace\DataStructures\src\sample1-pp.txt")); 

ArrayList<String> list = new ArrayList<String>();
//testing
FileWriter writer = new FileWriter ("C:\Users\amank\eclipse-workspace\DataStructures\src\output.txt");; 

if(!sc1.hasNext())
{
    System.out.println("File is empty");
}

try {
    while (sc1.hasNext())
    {

        list.add(sc1.nextLine().trim().toLowerCase().replaceAll("\p{P}", ""));


    }
    sc1.close();    

    //for testing
    String newLine = System.getProperty("line.separator");

    for(String str: list)
    {
        writer.write(str+newLine);
    }
    writer.close();