Read a text file from a jar
[ReadFromJar.java]import java.applet.*;
import java.io.*;public class ReadFromJar extends Applet{ public void init(){
readTextFromJar("datafile1.txt");
readTextFromJar("test/datafile2.txt");
} public void readTextFromJar(String s) {
String thisLine;
try {
InputStream is = getClass().getResourceAsStream(s);
BufferedReader br = new BufferedReader
(new InputStreamReader(is));
while ((thisLine = br.readLine()) != null) {
System.out.println(thisLine);
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
Create 2 datafiles.
datafile1.txt in the same directory as ReadfromJar.class and datafile2.txt in a subdirectory called test [datafile1.txt]
datafile1 line 1
datafile1 line 2
datafile1 line 3
datafile1 line 4
datafile1 line 5 [test\datafile2.txt]
datafile2 line 1
datafile2 line 2
datafile2 line 3
datafile2 line 4
datafile2 line 5
Create the jar with jar cf readjar.jar ReadFromJar.class datafile1.txt test\datafile2.txt
Try with the follwing HTML page <HTML><HEAD></HEAD><BODY>
<APPLET CODE=ReadFromJar.class width=1 height=1 archive=readjar.jar>
</APPLET>
See java console for output</BODY></HTML>
[ReadFromJar.java]import java.applet.*;
import java.io.*;public class ReadFromJar extends Applet{ public void init(){
readTextFromJar("datafile1.txt");
readTextFromJar("test/datafile2.txt");
} public void readTextFromJar(String s) {
String thisLine;
try {
InputStream is = getClass().getResourceAsStream(s);
BufferedReader br = new BufferedReader
(new InputStreamReader(is));
while ((thisLine = br.readLine()) != null) {
System.out.println(thisLine);
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
Create 2 datafiles.
datafile1.txt in the same directory as ReadfromJar.class and datafile2.txt in a subdirectory called test [datafile1.txt]
datafile1 line 1
datafile1 line 2
datafile1 line 3
datafile1 line 4
datafile1 line 5 [test\datafile2.txt]
datafile2 line 1
datafile2 line 2
datafile2 line 3
datafile2 line 4
datafile2 line 5
Create the jar with jar cf readjar.jar ReadFromJar.class datafile1.txt test\datafile2.txt
Try with the follwing HTML page <HTML><HEAD></HEAD><BODY>
<APPLET CODE=ReadFromJar.class width=1 height=1 archive=readjar.jar>
</APPLET>
See java console for output</BODY></HTML>
[JDK1.1 application] String imgName = "AnImage.jpg";
URL imgURL = getClass().getResource(imgName);
Toolkit tk = Toolkit.getDefaultToolkit();
Image img = null;
try {
MediaTracker m = new MediaTracker(this);
img = tk.getImage(imgURL);
m.addImage(img, 0);
m.waitForAll();
}
catch (Exception e) {
e.printStackTrace();
}
[JDK 1.1 applet]
Because of some security reason, it's not possible with some browser (like Netscape) to use the getResource() method from an Applet. Instead we must use the getResourceAsStream method. Image img = null;try {
MediaTracker m = new MediaTracker(this);
InputStream is = getClass().getResourceAsStream("AnImage.gif");
//
// if your image is in a subdir in the jar then
// InputStream is = getClass().getResourceAsStream("images/AnImage.gif");
// for example
//
BufferedInputStream bis = new BufferedInputStream(is);
byte[] byBuf = = new byte[10000]; // a buffer large enough for our image
//
// can be
// byte[] byBuf = = new byte[is.available()];
// is.read(byBuf); or something like that...
//
int byteRead = bis.read(byBuf,0,10000);
img = Toolkit.getDefaultToolkit().createImage(byBuf);
m.addImage(img, 0);
m.waitForAll();
}
}
catch(Exception e) {
e.printStackTrace();
}
[JDK 1.2 application]
URL url = this.getClass().getResource("myIcon.gif");
button.setIcon(new ImageIcon(url));
---------------
[email protected]
以上方法能实现吗 ?
JarInputStream jin=new JarInputStream(new FileInputStream(FileName));
while(true){
JarEntry je=jin.getNextJarEntry();
if (je.getName().equals(PhotoFileName)){
in=new LineNumberReader(new InputStreamReader(jin));
// 读图片
in.read()
。。
}
}