import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
public class Demo {
public static void main(String[] args) throws IOException {
// 类加载的方式来读取
// 类加载器 classloader
//第一种:通过流来读取
InputStream in = Demo.class.getClassLoader().getResourceAsStream("a.txt"); // 相对于类路径
System.out.println((char)in.read());
//第二种:通过绝对路径来读取
URL url = Demo.class.getClassLoader().getResource("a.txt");
String path = url.getPath();
System.out.println(path);
File file = new File(path);
System.out.println(file.exists());
}
}
类加载来读取工程下的文件
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.