创建File类 及 this.getClass().getResource()方法 用到的文件路径的问题
1 package test; 2 3 import java.io.*; 4 import java.util.Scanner; 5 6 public class TestResource { 7 public TestResource() throws IOException{ 8 InputStream url = this.getClass().getResourceAsStream("Hello.txt"); 9 //此处的Hello.txt必须与class文件在一个目录下 10 BufferedReader br=new BufferedReader(new InputStreamReader(url)); 11 System.out.println(br.readLine()); 12 13 File file = new File("Hello0.txt");//此处的 Hello0.txt 直接放在工程目录下。 14 Scanner fileScanner = new Scanner(file); 15 System.out.print(fileScanner.nextLine()); 16 } 17 public static void main(String[] args) throws IOException{ 18 // TODO Auto-generated method stub 19 new TestResource(); 20 } 21 22 }