【问题标题】:Create a file with the received name使用收到的名称创建一个文件
【发布时间】:2022-11-26 14:42:53
【问题描述】:

我创建了一个函数来创建课堂。现在我想将创建的类的信息保存在一个类名相同的文件中,例如:

我想创建一个带有数学名称的班级,该班级将教室的信息(包括教室容量和教室名称)存储在一个 txt 文件中。

public void  createclass(String name,int capacity){
     System.out.println("class created !!!");

     try {
        File [The name we chose for the class]= new File("E:\\[The name we chose for the class].txt");
        String str ="class "+name+" "+"with "+capacity+" capacity"+"was created";
        FileWriter fileWriter=new FileWriter([our file name]);
        fileWriter.write(str);
        fileWriter.close();
    }
    catch (Exception e){
        System.out.println(e);
    }
 }

【问题讨论】:

标签: java


【解决方案1】:

您可以像这样获取类的名称:

this.getClass().getSimpleName()

希望那是问题所在。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-07
    • 1970-01-01
    相关资源
    最近更新 更多