dcsimg
create a file
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Andy_Gunn
Posted On:   Friday, August 27, 2004 09:22 AM

may somebody tell me what is wrong with this coding?? i got file which content is error... import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.io.*; public class file { private JFrame f; private JTextField t1; private JTextField t2; private JButton save; private ObjectOutputStream output; public file() { f = new JFrame("Test"); t1 = new JTextField(20); t2 = new JTextField(20); save = new JButton("Save"); Container c = f.getContentPane(); save.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { saveFile();   More>>

may somebody tell me what is wrong with this coding??
i got file which content is error...


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;

public class file
{ private JFrame f;
private JTextField t1;
private JTextField t2;
private JButton save;
private ObjectOutputStream output;

public file()
{ f = new JFrame("Test");
t1 = new JTextField(20);
t2 = new JTextField(20);
save = new JButton("Save");
Container c = f.getContentPane();

save.addActionListener(new ActionListener()
{ public void actionPerformed(ActionEvent e)
{ saveFile();
}
});
c.setLayout(new FlowLayout());
c.add(t1);
c.add(t2);
c.add(save);

f.pack();
f.setSize(250,200);
f.setVisible(true);
}

public void saveFile()
{ try{
output = new ObjectOutputStream(new FileOutputStream("test.txt"));

record r = new record(t1.getText(),t2.getText());
output.writeObject(r);
output.flush();
}
catch(IOException e)
{ System.out.println("Error"); }
}

public static void main(String args[])
{ new file(); }
}

class record
{ private String name1;
private String name2;

public record()
{ this("",""); }

public record(String name1,String name2)
{ setname1(name1);
setname2(name2);
}

public void setname1(String name1)
{ this.name1 = name1; }

public void setname2(String name2)
{ this.name2 = name2; }

public String getname1()
{ return name1; }

public String getname2()
{ return name2; }
}

   <<Less

Re: create a file

Posted By:   Axel_Richter  
Posted On:   Saturday, August 28, 2004 10:13 AM

Hi,



your Object "record" has to be serializable for ObjectOutputStream.writeObject(). Therefor it has to implement the interface Serializable.




...
class record implements Serializable{
...



By the way: _please_ use the
-tag for code.



greetings



Axel
About | Sitemap | Contact