java获取方法的行数_java获取调用当前方法的方法名和行数

java获取调用当前方法的方法名和行数

String className = Thread.currentThread().getStackTrace()[2].getClassName();//调用的类名

String methodName = Thread.currentThread().getStackTrace()[2].getMethodName();//调用的方法名

int lineNumber = Thread.currentThread().getStackTrace()[2].getLineNumber();//调用的行数

Thread.currentThread().getStackTrace()[1]是你当前方法执行堆栈

Thread.currentThread().getStackTrace()[2]就是上一级的方法堆栈 以此类推

________________________________________________________________________________________

1.System类

//获取服务器使用的系统

String system = System.getProperty("os.name");

Properties properties = System.getProperties();

for (String name : properties.stringPropertyNames()) {

System.out.println(name);

System.out.println(System.getProperty(name));

//单个实例:输出 os.name Windows 10 一共68组

}

2.EnvironmentAware类可以获取数据源,驱动等

@Configuration

public class EAwareInfo implements EnvironmentAware {

@Override

public void setEnvironment(Environment environment) {

String projectName =environment.getProperty("project.name");

System.out.println(projectName); //获取项目名称

}

}

//逻辑层使用

@Service

public class TestServiceImpl implements TestService, EnvironmentAware {

private static String projectName=null;

@Override

public void setEnvironment(Environment environment) {

//获取项目名称

projectName=environment.getProperty("project.name");

//获取到前缀是"spring.datasource." 的属性列表值.

RelaxedPropertyResolver relaxedPropertyResolver = new RelaxedPropertyResolver(environment, "spring.datasource.");

System.out.println("spring.datasource.url="+relaxedPropertyResolver.getProperty("url"));

}

}

————————————————

版权声明:本文为CSDN博主「吕小小布」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/qq_37059838/article/details/89192503

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在 Java 中,可以使用 `java.io.File` 类来获取文件的数和大小。 获取文件的数: 1. 使用 `java.io.BufferedReader` 和 `java.io.FileReader` 类打开文件,然后逐读取文件,累加数。代码示例如下: ``` import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; public class Main { public static void main(String[] args) throws IOException { File file = new File("example.txt"); int lineCount = 0; try (BufferedReader reader = new BufferedReader(new FileReader(file))) { while (reader.readLine() != null) { lineCount++; } } System.out.println("Number of lines: " + lineCount); } } ``` 获取文件的大小: 1. 使用 `java.io.File` 类的 `length()` 方法获取文件的大小,代码示例如下: ``` import java.io.File; public class Main { public static void main(String[] args) { File file = new File("example.txt"); long size = file.length(); System.out.println("Size of file: " + size + " bytes"); } } ``` ### 回答2: 要使用java的File类获取文件的数和大小,可以按照以下步骤进操作: 1. 首先,需要创建一个File对象,指向要获取数和大小的文件。可以使用File类的构造方法,传入文件的路径作为参数。 2. 为了获取文件的数,可以使用BufferedReader类。首先创建一个BufferedReader对象,并传入一个FileReader对象作为参数。FileReader对象用于读取文件内容。 3. 创建一个计数器变量,用于统计文件的数。初始化为0。 4. 使用BufferedReader对象的readLine()方法读取文件内容,每读取一,将计数器变量加1。 5. 当读取到文件的末尾时,readLine()方法会返回null,此时可以停止读取,得到文件的数。 6. 获取文件的大小是通过File类的length()方法来实现的。调用File对象的length()方法即可得到文件的字节大小。 7. 最后,将得到的文件数和大小进输出或者保存。 下面是示例代码: ```java import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; public class FileSizeAndLineCounter { public static void main(String[] args) { File file = new File("文件路径"); // 替换成实际文件路径 // 获取文件的数 int lineCount = 0; try (BufferedReader br = new BufferedReader(new FileReader(file))) { String line; while ((line = br.readLine()) != null) { lineCount++; } } catch (IOException e) { e.printStackTrace(); } // 获取文件的大小 long fileSize = file.length(); System.out.println("文件的数:" + lineCount); System.out.println("文件的大小:" + fileSize + "字节"); } } ``` 使用上述代码,可以通过File类获取文件的数和大小。需要注意的是,要替换代码中的"文件路径"部分为你实际的文件路径。 ### 回答3: 要使用Java中的File类来获取文件的数和大小,可以按照以下步骤进操作: 1. 首先,创建一个File对象,指定要获取信息的文件路径。 2. 使用FileReader类和BufferedReader类,以为单位读取文件内容。定义一个计数器变量lineCount来记录数。 3. 在循环中,使用BufferedReader的readLine()方法读取每一内容,如果读取到的内容不为空,则数加1。 4. 完成循环后,lineCount变量即为文件的数。 5. 使用File对象的length()方法,可以获取文件的大小,以字节为单位。 6. 最后,将得到的数和文件大小进输出或存储等后续处理。 以下是示例代码: ```java import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; public class FileUtil { public static void main(String[] args) { String filePath = "文件路径"; // 输入要获取信息的文件路径 File file = new File(filePath); int lineCount = getLineCount(file); long fileSize = file.length(); System.out.println("文件数:" + lineCount); System.out.println("文件大小:" + fileSize + "字节"); } public static int getLineCount(File file) { int lineCount = 0; try (BufferedReader reader = new BufferedReader(new FileReader(file))) { String line; while ((line = reader.readLine()) != null) { lineCount++; } } catch (IOException e) { e.printStackTrace(); } return lineCount; } } ``` 上述代码中,需要将"文件路径"替换为实际的文件路径,然后运程序即可获取文件的数和大小。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值