如何在Java类中检测文件位置?

huangapple go评论58阅读模式
英文:

How to detect file location in Java Classes?

问题

让我们来看一个类A。

class A:
 
  def getAbsoluteLocation(self):
    
   ....

这个类如何知道它位于哪个目录中?

英文:

Lets take a class A.

class A{
 
  public getAbsoluteLocation(){
    
   ....
  }
}

How can this class know in which directory it is located?

答案1

得分: 1

以下是翻译好的部分:

假设您只想打印路径。您还可以根据您的方法返回类型返回值或执行任何其他操作。

class A {

  public void getAbsoluteLocation() {
    ClassLoader loader = A.class.getClassLoader();
    System.out.println(A.class.getResource("A.class"));
  }

}
英文:

Assumption is that you just want to print the path. You can also return the value or perform any other operations based on your method return type.

class A {
 
  public void getAbsoluteLocation() {
    ClassLoader loader = A.class.getClassLoader();
    System.out.println(A.class.getResource("A.class"));
  }

}

huangapple
  • 本文由 发表于 2020年9月4日 13:31:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/63735356.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定