每次我使用 writer 方法时都不会弹出这样的元素异常

no such element exception pop ups everytime i use writer method

import java.io.IOException;
import java.io.File;
import java.nio.file.Path;
import java.util.Scanner;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.NoSuchElementException;
public class filehandling{
    public static void main(String[] args){
        Scanner x = new Scanner(System.in);
        while(true){
            System.out.println("1. write 2. read 3. delete 4. create 5.exit");
            System.out.print("Enter choice: ");
            int ch = x.nextInt();
            if(ch==1){
                writer1 var0 = new writer1();
                var0.function1();
            }
            if(ch==2){
                reader1 var0 = new reader1();
                var0.function2();
            }
            if(ch==3){
                delete1 var0 = new delete1();
                var0.function4();
            }
            if(ch==4){
                create1 var0 = new create1();
                var0.function3();
            }
            if(ch==5){
                System.out.println("exited, thank you for using program");
                x.close();
                break;
            }
        }
    }
}
class writer1{
    void function1(){
            Scanner y = new Scanner(System.in);
            System.out.print("Input file name: ");
            String path = y.nextLine();

            File file = new File("D:\"+path);

            System.out.print("input number of lines: ");
            Scanner a = new Scanner(System.in);
            int n = a.nextInt();
            
            Scanner z = new Scanner(System.in);
            System.out.print("input data: ");
            String data = z.nextLine();
            
            FileWriter fr = null;
            BufferedWriter br = null;
            String datawithnewline = data+System.getProperty("line.separator");
            System.out.println(datawithnewline);
            try {
                for(int i = n; i>0;i--){
                    try {
                        fr = new FileWriter(file);
                        br = new BufferedWriter(fr);
                        br.write(datawithnewline);
                    } catch (NoSuchElementException e) {
                        System.out.println("DONE ");
                    }
                    
                }
            } catch (IOException e) {
                System.out.print("error");
            }
            finally{
                try{
                    br.close();
                    fr.close();
                    y.close();
                    z.close();
                    a.close();
                }
                catch(IOException e){
                    System.out.print("Error 2");
                }
            }
        }
    }
class reader1{
    void function2(){
        Scanner y = new Scanner(System.in);
        System.out.print("Input file name: ");
        String path = y.nextLine();
        File file = new File("C:\Users\subra\AppData\Local\Temp\vscodesws_32946\jdt_ws\jdt.ls-java-project\src"+path);
        if(file.canRead()){
            FileReader fr = null;
            BufferedReader br = null;
            try{
                fr = new FileReader(path);
                br = new BufferedReader(fr);
                int var = 0;
                while(( var=br.read())!= -1){
                    char text = (char) var;
                    System.out.print(text);
                }
            }
            catch(IOException e){
                System.out.println("Error");
            }
            finally{
                y.close();
                if (fr !=null){
                    try{
                        fr.close();
                    }
                    catch(IOException e){
                        System.out.println("Error");
                    }
                }
                if(br!=null){
                    try{
                        br.close();
                    }
                    catch(IOException e){
                        System.out.println("Error");
                    }
                }
            }
        }
    }
}
class create1{
    public void function3(){
        Scanner var1 = new Scanner(System.in);
        System.out.print("Input file name: ");
        String var2 = var1.nextLine();
        File file = new File("D:\"+var2);
        try {
            boolean createNewfile = file.createNewFile();
            System.out.println("File created: "+createNewfile);
        } catch (IOException e) {
            System.out.println("Error");
            var1.close();
        }
    }
}
class delete1{
    public void function4(){
        Scanner y = new Scanner(System.in);
        System.out.print("Input file name");
        String path = y.nextLine();
        Path path1 = Path.of(path);
        String path2 = path1.toString();
        File file = new File(path2);
        if(file.canRead()){

            boolean delete = file.delete();
            System.out.println("DELETED FILE: "+delete);
        }
        y.close();

    }
}

每次我运行这个程序,总是returns这个错误,其实我在java学习文件处理所以我用这个website,我是使用 visual studio 代码,我尝试将 br.write(...) 部分放在 writer1 class 的 for 循环内的 try 和 catch 块中, 终端中的总互动是

PS C:\Users\subra\AppData\Local\Temp\vscodesws_32946\jdt_ws\jdt.ls-java-project\src>  c:; cd 'c:\Users\subra\AppData\Local\Temp\vscodesws_32946\jdt_ws\jdt.ls-java-project\src'; & 'c:\Users\subra\.vscode\extensions\vscjava.vscode-java-debug-0.36.0\scripts\launcher.bat' 'C:\Program Files\Java\jdk-16.0.2\bin\java.exe' '-agentlib:jdwp=transport=dt_socket,server=n,suspend=y,address=localhost:50835' '--enable-preview' '-XX:+ShowCodeDetailsInExceptionMessages' '-Dfile.encoding=UTF-8' '-cp' 'C:\Users\subra\AppData\Roaming\Code\User\workspaceStorage43e469db802eccea9e87de0109e000\redhat.java\jdt_ws\src_c37eea88\bin' 'filehandling' 
1. write 2. read 3. delete 4. create 5.exit
Enter choice: 1
Input file name: hi
input number of lines: 5
input data: i love coding
i love coding

1. write 2. read 3. delete 4. create 5.exit
Enter choice: Exception in thread "main" java.util.NoSuchElementException
        at java.base/java.util.Scanner.throwFor(Scanner.java:937)
        at java.base/java.util.Scanner.next(Scanner.java:1594)
        at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
        at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
        at filehandling.main(filehandling.java:16)

我该怎么办??

您只应在所有 类 中使用一个扫描仪,并且只关闭一次

仅使用您的 类 之一的示例(类 应大写,一般不要使用数字)

public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    Reader reader = new Reader(sc);  // your class. Don't close the Scanner in it
    int ch = -1;
    while (ch !=5) {
        ch = sc.nextInt();
        if (ch == 2) {
            reader.read();
        }
    }
    sc.close(); // only done once
}

然后更新 类 添加构造函数

class Reader {
    private Scanner sc;
    public Reader(Scanner scanner) {
        this.sc = scanner;
    }

    public void read() {
        String filepath = sc.readLine();
       ... 
    }