java - 创建一个txt文件并将类名作为txt的名称
问题描述
我正在编写一个程序来创建一个带有类名的文本文件。我尝试了以下代码并说:不能从静态上下文中引用非静态变量类名。我还尝试将 className 字符串设为静态,但又说非静态变量,这不能从静态上下文中引用。
String className = this.getClass().getName();
File file = new File("C:\\" + className + ".txt");
public static String logFileName = "C:\\"+className +".txt";
解决方案
使用它来获取类名。
String className = this.getClass().getSimpleName();
推荐阅读
- python-xarray - 将非维度坐标转换为经典维度
- kubernetes - 网关端口定义如何工作?
- php - 我使用 Laravel 的网页列表未显示数据库中的数据
- swift - 在 UIViewRepresentable 视图中使用 setStrokeColor 占用大量内存
- python - 在没有条件运算符和 Python 条件的情况下用 0 替换 Python 负数
- reactjs - Next.js 与 Express 服务器如何调用自定义错误页面?
- html - HTML 电子邮件深色模式 iOS 和 Gmail
- javascript - Webpack 5 和 ESM
- sympy - 在 sympy 中将 Sum 的平方扩展为双 Sum
- java - MapStruct 似乎创建了太多方法