src下面新建demo.properties

    1. level=debug
    1. @Test
    2. public void test05() throws Exception{
    3. InputStream in = new BufferedInputStream(new FileInputStream("src/demo.properties")) ;
    4. Properties p = new Properties();
    5. p.load(in);
    6. String level =p.getProperty("level");
    7. System.out.println(level);
    8. }
    9. @Test
    10. public void test06() throws IOException{
    11. InputStream in=Properties.class.getResourceAsStream("/demo.properties");
    12. Properties p = new Properties();
    13. p.load(in);
    14. System.out.println(p.getProperty("level"));
    15. }
    16. @Test
    17. public void test07() throws IOException{
    18. InputStream in = FileTest.class.getClassLoader().getResourceAsStream("demo.properties");
    19. Properties p = new Properties();
    20. p.load(in);
    21. System.out.println(p.getProperty("level"));
    22. }
    23. @Test
    24. public void test08() throws IOException{
    25. InputStream in =ClassLoader.getSystemResourceAsStream("demo.properties");
    26. Properties p = new Properties() ;
    27. p.load(in) ;
    28. System.out.println(p.getProperty("level"));
    29. }
    1. InputStream in = MyTest.class.getClassLoader().getResourceAsStream("a.txt");
    2. BufferedReader br = new BufferedReader(new InputStreamReader(in));
    3. String line = null;
    4. while( (line = br.readLine()) != null ){
    5. System.out.println(line);
    6. }
    7. br.close();
    8. in.close();
    1. String filePath = MTest.class.getResource("/a.txt").getPath();
    2. filePath = filePath.substring(1);
    3. InputStream in = new FileInputStream(new File(filePath));
    4. BufferedReader br = new BufferedReader(new InputStreamReader(in));
    5. String str = null;
    6. while( (str = br.readLine())!=null ){
    7. System.out.println(str);
    8. }
    9. in.close();
    10. br.close();