I have an applet class and I want that when I click the button on the applet, then it runs Java Class.
It runs Java Class, but in the Java Class I have created a .csv
file, so it gives me error of
file not found
..... If I separately run the Java Class it gives the correct output ....
What's the problem in linking the applet and Java Class?
Applet 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 Class
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();
}
}
}