import java.io.File;
import java.net.URL;
public class TestFile {
/**
* @param args
*/
public static void main(String[] args) {
URL url = TestFile.class.getClassLoader().getResource("");
System.out.println(url);
String path = url.getPath();
System.out.println(path);
File file = new File(path+"resources/imgs/logo.gif");
if(!file.exists()){
System.out.println("找不到文件 :"+file.getAbsolutePath());
}else{
System.out.println("成功找到文件");
System.out.println("文件名称 : "+file.getName());
System.out.println("文件路径 :"+file.getPath());
}
}
}
运行结果:
file:/F:/studyeveryday/myecworks/NoteBook/bin/
/F:/studyeveryday/myecworks/NoteBook/bin/
成功找到文件
文件名称 : logo.gif
文件路径 :F:\studyeveryday\myecworks\NoteBook\bin\resources\imgs\logo.gif
注:
TestFile.class.getClassLoader().getResource("resources/imgs/logo.gif");
TestFile.class.getResource("/");