Reason...???
807601Apr 7 2008 — edited Apr 7 2008why is it that when i run a program related to ' file i/o and swing' under JDK1.6 environment doesnt work but when i do the same with eclipse or netbeans it works absolutely fine.
The Gui loads well but the file i/o doesnt work well.
please compile the code and use the applet code in a .html file.
code ://
-------------------------------------------------------------------------------------------------
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class TextFileGUI implements ActionListener
{
TextFile tf = new TextFile();
JTextField filenameField = new JTextField(30);
JScrollPane scrollPane = new JScrollPane();
JTextArea fileTextArea = new JTextArea();
JButton displayButton = new JButton("Display");
JButton updateButton = new JButton("Update");
JPanel panel = new JPanel();
JFrame frame = new JFrame("Text File GUI");
public static void main(String args[])
{
new TextFileGUI();
}
public TextFileGUI()
{
panel.setLayout(new BorderLayout());
JPanel vName = new JPanel();
vName.setLayout(new BorderLayout());
vName.add(new JLabel("Filename"), BorderLayout.WEST);
vName.add(filenameField, BorderLayout.CENTER);
panel.add(vName, BorderLayout.NORTH);
scrollPane.setViewportView(fileTextArea);
panel.add(scrollPane, BorderLayout.CENTER);
fileTextArea.setLineWrap(true);
JPanel vBtn = new JPanel();
vBtn.setLayout(new FlowLayout());
vBtn.add(displayButton);
displayButton.addActionListener(this);
vBtn.add(updateButton);
updateButton.addActionListener(this);
panel.add(vBtn, BorderLayout.SOUTH);
frame.setContentPane(panel);
frame.setSize(400, 400);
frame.setVisible(true);
} // end TextFileGUI()
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == displayButton)
{
String t;
try
{
t = tf.read(filenameField.getText());
fileTextArea.setText(t);
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
else if (e.getSource() == updateButton)
{
try
{
tf.write(filenameField.getText(), fileTextArea.getText(), false);
}
catch (IOException ex)
{
ex.printStackTrace();
} // end try-catch
} // end else if
} // end actionPerformed()
} // end TextFileGUI
import java.io.*;
class TextFile
{
public String read(String fileIn) throws IOException
{
String line;
StringBuffer text = new StringBuffer();
FileInputStream vFis = new FileInputStream(fileIn);
byte[] vByte = new byte[1024];
int vPos = -1;
while ((vPos = vFis.read(vByte)) > 0)
{
text.append(new String(vByte, 0, vPos));
}
vFis.close();
return text.toString();
} // end read()
public void write(String fileOut, String text, boolean append) throws IOException
{
File file = new File(fileOut);
FileWriter fw = new FileWriter(file, append);
PrintWriter pw = new PrintWriter(fw);
pw.println(text);
fw.close();
} // end write()
} // end class TextFile
--------------------------------------------------------------------------------------
The applet code
<HTML>
<APPLET
CODE = TextFile.class
WIDTH = 400
HEIGHT = 250>
</APPLET>
</HTML>
I would really appreciate if anybody could let me know what the problem is....
Thanks in advance