主题:深入jar包:从jar包中读取资源文件
http://www.iteye.com/topic/483115
http://www.iteye.com/topic/7219
http://www.iteye.com/topic/149147
包结构
-src
-test_1
TEST.java
--test_1.test_2
hello_en.txt
hello_zh.txt
方法一
方法二
http://www.iteye.com/topic/483115
http://www.iteye.com/topic/7219
http://www.iteye.com/topic/149147
包结构
-src
-test_1
TEST.java
--test_1.test_2
hello_en.txt
hello_zh.txt
方法一
package test_1;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URISyntaxException;
import java.net.URL;
public class Test {
public static void main(String[] args) throws URISyntaxException,
IOException {
URL url = Test.class.getResource("/hello_zh.txt");
File file = new File(url.getFile());
System.out.println(file);
InputStream in = url.openStream();
BufferedReader br=new BufferedReader(new InputStreamReader(in));
String s="";
while((s=br.readLine())!=null)
System.out.println(s);
}
}
方法二
package test_1;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URISyntaxException;
public class Test {
public static void main(String[] args) throws URISyntaxException,
IOException {
InputStream is = Test.class.getResourceAsStream("/hello_zh.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String s = "";
while ((s = br.readLine()) != null)
System.out.println(s);
}
}
jar cvf classes.jar *
java -classpath ./classes.jar test_1.Test