import java.io.FileOutputStream
import java.io.IOException
import java.io.ObjectInputStream
import java.io.ObjectOutputStream
import java.io.Serializable
public class ObjTest {
ObjectOutputStream oos
ObjectInputStream ois
String url = "f:/a.txt"//txt文件的目录位置,请自行修改
{
try {
oos =new ObjectOutputStream(new FileOutputStream(url))
ois = new ObjectInputStream(new FileInputStream(url))
} catch (Exception e) {
e.printStackTrace()
}
}
//关闭流
public void close(){
try {
ois.close()
} catch (IOException e) {
e.printStackTrace()
}finally{
try {
oos.close()
} catch (IOException e) {
e.printStackTrace()
}
}
}
//写入txt的方法
public void writeOut(Object obj){
try {
oos.writeObject(obj)
oos.flush()
} catch (Exception e) {
e.printStackTrace()
}
}
//读取txt的方法
public Object readIn(){
try {
return ois.readObject()
} catch (Exception e) {
e.printStackTrace()
}
return null
}
public static void main(String[] args) throws Exception {
ObjTest ot = new ObjTest()
ot.writeOut(new Person(1,"Tom",true))//写入5个对象
ot.writeOut(new Person(2,"Mary",false))
ot.writeOut(new Person(3,"Jack",true))
ot.writeOut(new Person(4,"Rose",false))
ot.writeOut(new Person(5,"Peter",true))
Person p
while((p = (Person)ot.readIn()) != null){
if(p.id == 3){ //查找条件
System.out.println(p)
return
}
}
System.out.println("no found person")
ot.close()
}
}
class Person implements Serializable{
int id
String name
boolean sex
public Person(){}
public Person(int id, String name, boolean sex) {
super()
this.id = id
this.name = name
this.sex = sex
}
@Override
public String toString() {
return id+ "\r\n" + name + "\r\n" + sex +"\r\n"
}
}
java.io.File folder = new java.io.Folder("D:\\我的数据晌毁")if(!folder.exists()){
folder.mkdirs()///如果不宴拦备存在衡腊,创建目录
}
java.io.File file = new java.io.File(folder, "test1.txt")
java.io.FileOutputStream fou=new java.io.FileOutputStream(file)
fou.write("Test line 1\r\n")
fou.write("Test line 2\r\n")
fou.close()
大体如此,请参考
全实现,程序太长,发不上去,先实现基本的 图形用户界面import java.awt.*
import java.awt.event.*
public class TestMenu {
public static void main (String[] args) {
new MenuFrame("新建"+" "+"文本文档"+".txt"+" "+"-"+" "+"记事本").launchFrame()
}
}
class MenuFrame extends Frame {
MenuBar mb = null
MenuFrame (String s) {
super (s)
}
public void launchFrame() {
Menu file = new Menu ("文件")
Menu edit = new Menu ("编辑")
Menu format = new Menu ("格式")
Menu help = new Menu ("帮助")
MenuItem newItem = new MenuItem ("新建")
newItem.addActionListener (new ActionListener () {
public void actionPerformed(ActionEvent e) {
final Frame ff = new Frame ("记事本")
ff.setMenuBar(mb)
ff.setBounds (300,300,400,200)
ff.setVisible (true)
ff. addWindowListener (new WindowAdapter () {
public void windowClosing(WindowEvent e) {
ff.setVisible (false)
}
} )
}
})
MenuItem saveItem = new MenuItem ("保存罩行")
MenuItem exitItem = new MenuItem ("退出")
MenuItem helpTitle = new MenuItem ("帮助主题")
MenuItem line = new MenuItem ("-")
MenuItem about = new MenuItem ("关于记戚稿事本")
MenuItem copy = new MenuItem ("粘贴")
MenuItem serach = new MenuItem ("查找高闷孝")
edit.add (copy)
edit.add (serach)
help.add (helpTitle)
help.add (line)
help.add (about)
exitItem.addActionListener (new ActionListener () {
public void actionPerformed(ActionEvent e) {
System.exit (0)
}
} )
file.add (newItem)
file.add (saveItem)
file.add (exitItem)
mb = new MenuBar()
mb.add (file)
mb.add (edit)
mb.add (format)
mb.add (help)
addWindowListener (new WindowAdapter () {
public void windowClosing(WindowEvent e) {
System.exit (0)
}
} )
setLayout (new FlowLayout())
setMenuBar (mb)
setBounds (300,300,400,200)
setVisible (true)
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)