如何在Java文件处理中删除文本文件中的行?

时间:2018-10-15 11:58:02

标签: java arrays arraylist file-handling

我在文本中输入了一些单词,并且希望能够将其删除,该怎么做? 我希望能够在文本文件中键入一些单词,并且可以在再次运行代码时将其删除。 我是新手,请怜悯。

public static void main(String[] args) {
    boolean c = true;
    File fh = new File("students.txt");
    ArrayList als = new ArrayList(4);
    Scanner scan = new Scanner(System.in);

    System.out.println("MENU");
    System.out.println("");
    System.out.println("What do you want to do?");
    System.out.println("(1)add(2)remove");
    int choice = scan.nextInt();

    switch (choice) {
    case 1:
        try {
            PrintWriter ptf = new PrintWriter(new FileWriter(fh, true));
            switch (choice) {
            case 1:
                System.out.println("Enter name you want to add: ");
                String a = scan.next();

                ptf.println(a);
                ptf.close();
                System.out.println("Success!");
            }
        } catch (IOException e) {
            System.out.println("Error");
        }
        break;
}

}

0 个答案:

没有答案