You can use the getResourceAsStream() method to get the file in the JAR as an InputStream:
import java.io.*;
import java.awt.*;
import java.applet.*;
public class Read extends Applet {
TextArea ta = new TextArea();
public void init() {
setLayout(new BorderLayout());
add(ta, BorderLayout.CENTER);
try {
InputStream in =
getClass().getResourceAsStream("read.txt");
InputStreamReader isr =
new InputStreamReader(in);
BufferedReader br =
new BufferedReader(isr);
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
String line;
while ((line = br.readLine()) != null) {
pw.println(line);
}
ta.setText(sw.toString());
} catch (IOException io) {
ta.setText("Ooops");
}
}
}
Source: java-tips.org
Sunday, May 03, 2009
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment