java 如何在input中创建段落?

2hh7jdfx  于 5个月前  发布在  Java
关注(0)|答案(2)|浏览(54)
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.io.File;
import java.io.FileWriter;
import java.util.Scanner;
import java.io.IOException;

public class MyFile {
    private static File file = new File("text.txt");
    private static StringBuilder stringBuilder = new StringBuilder();
    
    public static void copyToClipboard(String text) {
        StringSelection stringSelection = new StringSelection(text);
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        
        clipboard.setContents(stringSelection, null);
    }
    
    private String readContent() {
        try (Scanner fileReader = new Scanner(file)) {
            while (fileReader.hasNextLine()) {
                String line = fileReader.nextLine();
                stringBuilder.append(line).append("\n");
            }
        } catch (IOException e) {
            System.out.println("An error has occured.");
            e.printStackTrace();
        }
        
        return stringBuilder.toString();
    }
    
    public void write(String content) {
        try (FileWriter fileWriter = new FileWriter(file)) {
            fileWriter.write(content);
        } catch (IOException e) {
            System.out.println("An error has occured.");
            e.printStackTrace();
        }
    }
        
    public void read() {
        try (Scanner fileReader = new Scanner(file)) {
            while (fileReader.hasNextLine()) {
                String content = fileReader.nextLine();
                System.out.println(content);
            }
        } catch (IOException e) {
            System.out.println("An error has occured.");
            e.printStackTrace();
        }
    }
    
    public void delete() {
        while (true) {
            try (FileWriter fileWriter = new FileWriter(file)) {
                Scanner scan = new Scanner(System.in);
                System.out.println("Are you sure you want to delete all the file content (y or n)?");
                String answer = scan.nextLine().trim().toLowerCase();
                
                if (answer.equals("y")) {
                    fileWriter.write("");
                    System.out.println("Succesfully deleted all the file content.");
                    break;
                } else if (answer.equals("n")) {
                    System.out.println("Cancelling...");
                    break;
                } else { 
                    System.out.println("An error has occured.");
                    continue;
                }
                    
            } catch (IOException e) {
                System.out.println("An error has occured.");
                e.printStackTrace();
                continue;
            }
        }       
    }
    
    public void edit() {
        Scanner scan = new Scanner(System.in);
        MyFile file = new MyFile();
        String content = file.readContent();
        StringBuilder newContent = new StringBuilder();
        
        copyToClipboard(content);
        
        System.out.println("Enter new data or edit new (paste it). To stop editing, enter \"quitprogram\".");
        String line = "";
        
        do {
            line = scan.nextLine();

            if (line.equals("quitprogram")) {
                break;
            } else {
                newContent.append(line).append("\n");
            }
        } while (!line.equals("quitprogram"));
        
        file.write(newContent.toString());
        
        System.out.printf("Successfully edited the text!\n\nNew text:\n%s", newContent);
    }
}

字符串
当我运行edit()方法时,它允许我输入一些文本,并创建一个段落在另一行上输入smt.但是如果我打开text.txt,就不会有任何段落,所有的文本都只写在一行上。
我尝试使用.append(“\n”)在输入中创建段落。然而,即使用户按Enter键并创建新行,它也不起作用。

4si2a6ki

4si2a6ki1#

你可能想使用System.lineSeparator,因为行分隔符是依赖于平台的。例如,如果你在windows中,你可能在一些文本编辑器中看不到\n作为一个新行。
也就是说,这并不理想,因为生成的文本将依赖于平台。所以,你可能看不到段落的原因可能是。
为了测试这一点,你可以改变:

newContent.append(line).append("\n");

字符串
通过:

newContent.append(line).append(System.lineSperator());


或者使用十六进制编辑器查看内容,看看是否有\n,或者选择一个文本编辑器,在您的平台上检测\n作为行分隔符。
我在我的系统中运行了你的代码,这是Linux,段落在那里。

mctunoxg

mctunoxg2#

我在我的电脑上编译并运行了你的程序。你的程序成功地将多行文本写入“text.txt”,直到用户按预期输入“quitprogram”。
唯一的问题是程序没有在追加模式下打开文件。这意味着每次调用write()方法时,都会在覆盖模式下创建一个新的FileWriter。因此,它从文件的开头开始写入,而不是从它停止的地方继续。
要解决这个问题,您需要修改程序,使FileWriter以追加模式打开文件。

public void write(String content) {
        try (FileWriter fileWriter = new FileWriter(file,true)) {
            fileWriter.write(content);
        } catch (IOException e) {
            System.out.println("An error has occured.");
            e.printStackTrace();
        }
    }

字符串

相关问题