我有一个applet类,我希望当我点击applet上的按钮时,它会运行Java Class。
它运行Java类,但是在Java类中我创建了一个.csv
文件,所以它给了我错误
找不到文件
.....如果我单独运行Java Class,它会给出正确的输出....
链接applet和Java Class有什么问题?
小程序类
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class anApplet extends Applet implements ActionListener{
Button b;
TextField tf;
public void init(){
tf=new TextField();
tf.setBounds(30,40,150,20);
b=new Button("Click");
b.setBounds(80,150,60,50);
add(b);add(tf);
b.addActionListener(this);
setLayout(null);
}
public void actionPerformed(ActionEvent e){
tf.setText("Welcome");
try {
callingClass.main(null);
} catch (Throwable e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
Java类
public class callingClass {
public static void main(String[] main) throws Exception, Throwable
{
String outputFile = "Data\\Volume.csv";
// before we open the file check to see if it already exists
boolean alreadyExists = new File(outputFile).exists();
try {
// use FileWriter constructor that specifies open for appending
CsvWriter csvOutput = new CsvWriter(new FileWriter(outputFile, true), ',');
// if the file didn't already exist then we need to write out the header line
if (!alreadyExists)
{
csvOutput.write("date");
csvOutput.write("value");
csvOutput.endRecord();
}
// else assume that the file already has the correct header line
// write out a few records
csvOutput.write("Hello");
csvOutput.write( "CSV file");
csvOutput.endRecord();
//csvOutput.write("2");
//csvOutput.write("John");
// csvOutput.endRecord();
csvOutput.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}