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 }

 

相关文章:

  • 2022-12-23
  • 2022-12-29
  • 2022-12-23
  • 2021-12-08
  • 2021-06-26
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-29
  • 2022-12-23
  • 2021-07-10
  • 2021-10-15
相关资源
相似解决方案