This following example is for an application. Simply give the filename to be displayed on the command line. import java.io.*;
public class cat {
public static void main (String args[]) {
String thisLine;
for (int i=0; i < args.length; i++) {
try {
FileInputStream fin = new FileInputStream(args[i]);
// JDK1.1+
BufferedReader myInput = new BufferedReader
(new InputStreamReader(fin));
// JDK1.02
// DataInputStream myInput = new DataInputStream(fin);
while ((thisLine = myInput.readLine()) != null) {
System.out.println(thisLine);
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
}
In Applet, we can only open file on the same server that the Applet is coming from.[JDK1.02] import java.applet.*;
import java.net.*;
import java.io.*;
public class MyApplet extends Applet {
public void init() {
readFile("mydatafile.txt");
}
public void readFile(String f) {
try {
String aLine = "";
URL source = new URL(getCodeBase(), f);
DataInputStream in =
new DataInputStream(source.openStream());
while(null != (aLine = in.readLine()))
System.out.println(aLine);
in.close();
}
catch(Exception e) {
e.printStackTrace();
}
}
[JDK1.1]
import java.applet.*;
import java.net.*;
import java.io.*;
public class MyApplet extends Applet {
public void init() {
readFile("mydatafile.txt");
}
public void readFile(String f) {
try {
String aLine = "";
URL source = new URL(getCodeBase(), f);
BufferedReader br =
new BufferedReader
(new InputStreamReader(source.openStream()));
while(null != (aLine = br.readLine()))
System.out.println(aLine);
br.close();
}
catch(Exception e) {
e.printStackTrace();
}
}
}
The following Applet reads a data file and inserts the data in a Choice component. [ReadDataInChoice.java JDK1.1]
public class ReadDataInChoice extends java.applet.Applet {
java.awt.Choice myChoice;
public void init() {
myChoice = new java.awt.Choice();
add(myChoice);
readFile("dataforchoice.txt");
}
public void readFile(String f) {
try {
String anItem = "";
java.net.URL source =
new java.net.URL(getCodeBase(), f);
java.io.BufferedReader in =
new java.io.BufferedReader
(new java.io.InputStreamReader(source.openStream()));
while(null != (anItem = in.readLine()))
myChoice.add(anItem);
in.close();
}
catch(Exception e) {
e.printStackTrace();
}
}
}
[dataforchoice.txt]
item 1
item 2
item 3
item 4
item 5
item 6
public class cat {
public static void main (String args[]) {
String thisLine;
for (int i=0; i < args.length; i++) {
try {
FileInputStream fin = new FileInputStream(args[i]);
// JDK1.1+
BufferedReader myInput = new BufferedReader
(new InputStreamReader(fin));
// JDK1.02
// DataInputStream myInput = new DataInputStream(fin);
while ((thisLine = myInput.readLine()) != null) {
System.out.println(thisLine);
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
}
In Applet, we can only open file on the same server that the Applet is coming from.[JDK1.02] import java.applet.*;
import java.net.*;
import java.io.*;
public class MyApplet extends Applet {
public void init() {
readFile("mydatafile.txt");
}
public void readFile(String f) {
try {
String aLine = "";
URL source = new URL(getCodeBase(), f);
DataInputStream in =
new DataInputStream(source.openStream());
while(null != (aLine = in.readLine()))
System.out.println(aLine);
in.close();
}
catch(Exception e) {
e.printStackTrace();
}
}
[JDK1.1]
import java.applet.*;
import java.net.*;
import java.io.*;
public class MyApplet extends Applet {
public void init() {
readFile("mydatafile.txt");
}
public void readFile(String f) {
try {
String aLine = "";
URL source = new URL(getCodeBase(), f);
BufferedReader br =
new BufferedReader
(new InputStreamReader(source.openStream()));
while(null != (aLine = br.readLine()))
System.out.println(aLine);
br.close();
}
catch(Exception e) {
e.printStackTrace();
}
}
}
The following Applet reads a data file and inserts the data in a Choice component. [ReadDataInChoice.java JDK1.1]
public class ReadDataInChoice extends java.applet.Applet {
java.awt.Choice myChoice;
public void init() {
myChoice = new java.awt.Choice();
add(myChoice);
readFile("dataforchoice.txt");
}
public void readFile(String f) {
try {
String anItem = "";
java.net.URL source =
new java.net.URL(getCodeBase(), f);
java.io.BufferedReader in =
new java.io.BufferedReader
(new java.io.InputStreamReader(source.openStream()));
while(null != (anItem = in.readLine()))
myChoice.add(anItem);
in.close();
}
catch(Exception e) {
e.printStackTrace();
}
}
}
[dataforchoice.txt]
item 1
item 2
item 3
item 4
item 5
item 6
private String getStr(String str) throws Exception
{
String temp_p=str;
byte[] temp_t=temp_p.getBytes("ISO8859_1");
String temp=new String(temp_t);
return temp;
}
用上面的方法试试。只需要将所读文件的字符串进行上面的转换即可!!!