import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class test1
{ private record rd;
private ObjectOutputStream output;
public test1()
{try{output=new ObjectOutputStream(new FileOutputStream("e:\\file.txt"));
rd=new record(12,"ert",12f,23f);
}catch(Exception e){}
}
private class record implements Serializable//记录类,用于写入
{private int account;
private String name;
private float price;
private float payment; public record(int account,String name,float price,float payment)
{this.account=account;
this.name=name;
this.payment=payment;
this.price=price;
}
} public static void main(String[] args)
{ test1 app=new test1();
try{
app.output.writeObject(app.rd);
app.output.flush();
}
catch(Exception e){}
} }
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class test1
{ private record rd;
private ObjectOutputStream output;
public test1()
{try{output=new ObjectOutputStream(new FileOutputStream("e:\\file.txt"));
rd=new record(12,"ert",12f,23f);
}catch(Exception e){}
}
private class record implements Serializable//记录类,用于写入
{private int account;
private String name;
private float price;
private float payment; public record(int account,String name,float price,float payment)
{this.account=account;
this.name=name;
this.payment=payment;
this.price=price;
}
} public static void main(String[] args)
{ test1 app=new test1();
try{
app.output.writeObject(app.rd);
app.output.flush();
}
catch(Exception e){}
} }
解决方案 »
- eclipse building worlspace 很慢……怎么办
- JAVA GUI基础题目:怎么样才能是点击【帮助】里的【退出帮助】在弹出的窗口时,为要关闭这一个窗口,而不关闭菜单窗口和登陆界面的窗口?该怎么办啊 其他的不好的地方望帮我改正啊
- 【真心求助】关于JBuileder2007的一个问题!
- 请教,为什么jar运行不出来?
- 请教实现示波器功能的绘图实现
- 我把javamail API下载了下来,然后怎么安装它?
- 热心人,帮忙寻找这四本电子书!
- 关于JBuilder7 ,8的最基本问题,高分求救,不够再加!!!!!!!!!
- 用JBuilder能将java程序生成exe可执行文件吗?如何生成?
- 请问怎样设置jbuilder7.0的环境
- 什么是soa
- 数据库中二进制 取出后 如何转换成 字符串?
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;public class ObjectIO { public static void main(String[] args) {
try {
//先把对象写入文件
ObjectOutputStream output = new ObjectOutputStream(
new FileOutputStream("e:\\file.txt"));
output.writeObject(new record(12, "ert", 12f, 23f)); output.flush();
output.close(); //在读进内存中
ObjectInputStream fin = new ObjectInputStream(new FileInputStream(
"e:\\file.txt"));
Object o = fin.readObject();
if (o instanceof record) {
System.out.println(o);
}
fin.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}class record implements Serializable//记录类,用于写入
{
private static final long serialVersionUID = 1L;
private int account;
private String name;
private float price;
private float payment; public record(int account, String name, float price, float payment) {
this.account = account;
this.name = name;
this.payment = payment;
this.price = price; } @Override
public String toString() {
return this.account + this.name + this.payment + this.price;
}
}
package demo.io;import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;public class ObjectIO { public static void main(String[] args) {
try {
//先把对象写入文件
ObjectOutputStream output = new ObjectOutputStream(
new FileOutputStream("e:\\file.txt"));
output.writeObject(new record(12, "ert", 12f, 23f)); output.flush();
output.close(); //在读进内存中
ObjectInputStream fin = new ObjectInputStream(new FileInputStream(
"e:\\file.txt"));
Object o = fin.readObject();
if (o instanceof record) {
System.out.println(o);
}
fin.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}class record implements Serializable//记录类,用于写入
{
private static final long serialVersionUID = 1L;
private int account;
private String name;
private float price;
private float payment; public record(int account, String name, float price, float payment) {
this.account = account;
this.name = name;
this.payment = payment;
this.price = price; } @Override
public String toString() {
return this.account + this.name + this.payment + this.price;
}
}