首页 > 解决方案 > 创建一个txt文件并将类名作为txt的名称

问题描述

我正在编写一个程序来创建一个带有类名的文本文件。我尝试了以下代码并说:不能从静态上下文中引用非静态变量类名。我还尝试将 className 字符串设为静态,但又说非静态变量,这不能从静态上下文中引用。

String className = this.getClass().getName();

File file = new File("C:\\" + className + ".txt");

public static String logFileName = "C:\\"+className +".txt";

标签: java

解决方案


使用它来获取类名。

String className = this.getClass().getSimpleName();


推荐阅读