java实验题 急急急急急,
答案:3 悬赏:10 手机版
解决时间 2021-01-19 09:41
- 提问者网友:雪舞兮
- 2021-01-18 22:00
java实验题 急急急急急,
最佳答案
- 五星知识达人网友:英雄的欲望
- 2021-01-18 23:02
序列化 反序列化而已 给你个直接能用的
Student.java
import java.io.Serializable;
@SuppressWarnings("serial")
public class Student implements Serializable{
private int sNo;
private String sName;
private int sAge;
private String sGender;
public Student(){
}
public Student(int sNo, String sName, int sAge, String sGender){
System.out.println("Inside Student's Constructor");
this.sNo = sNo;
this.sName = sName;
this.sAge = sAge;
this.sAge = sGender;
}
public int getSNo(){
return sNo;
}
public void setSNo(int sNo){
this.sNo = sNo;
}
public String getSName(){
return sName;
}
public void setSName(String sName){
this.sName = sName;
}
public int getSAge(){
return sAge;
}
public void setSAge(int sAge){
this.sAge = sAge;
}
public String getSName(){
return sName;
}
public void setSGender(String sGender){
this.sGender = sGender;
}
public String toString(){
return "sName:" + sName
+ " sNo:"+sNo
+ " sAge:"+sAge
+ " sGender:"+sGender;
}
}SerializeToFlatFile 序列化反序列化用:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class SerializeToFlatFile {
public static void main(String[] args) {
SerializeToFlatFile ser = new SerializeToFlatFile();
ser.saveStudent();//存
System.out.println("存进去啦");
ser.restoreStudent(); //取
System.out.println("取出来啦");
}
//列化方法,存在E盘下的student.dat文件
public void saveStudent(){
Student myStudent = new Student(1001,"xiaoming",24,"male");
try {
FileOutputStream fos = new FileOutputStream("E:\student.dat");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(myStudent);
oos.flush();
oos.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
//反序列化方法,用来取序列化后的文件
public void restoreStudent() {
try {
FileInputStream fis = new FileInputStream("E:\student.dat");
ObjectInputStream ois = new ObjectInputStream(fis);
Student myStudent = (Student)ois.readObject();
System.out.println(myStudent);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
Student.java
import java.io.Serializable;
@SuppressWarnings("serial")
public class Student implements Serializable{
private int sNo;
private String sName;
private int sAge;
private String sGender;
public Student(){
}
public Student(int sNo, String sName, int sAge, String sGender){
System.out.println("Inside Student's Constructor");
this.sNo = sNo;
this.sName = sName;
this.sAge = sAge;
this.sAge = sGender;
}
public int getSNo(){
return sNo;
}
public void setSNo(int sNo){
this.sNo = sNo;
}
public String getSName(){
return sName;
}
public void setSName(String sName){
this.sName = sName;
}
public int getSAge(){
return sAge;
}
public void setSAge(int sAge){
this.sAge = sAge;
}
public String getSName(){
return sName;
}
public void setSGender(String sGender){
this.sGender = sGender;
}
public String toString(){
return "sName:" + sName
+ " sNo:"+sNo
+ " sAge:"+sAge
+ " sGender:"+sGender;
}
}SerializeToFlatFile 序列化反序列化用:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class SerializeToFlatFile {
public static void main(String[] args) {
SerializeToFlatFile ser = new SerializeToFlatFile();
ser.saveStudent();//存
System.out.println("存进去啦");
ser.restoreStudent(); //取
System.out.println("取出来啦");
}
//列化方法,存在E盘下的student.dat文件
public void saveStudent(){
Student myStudent = new Student(1001,"xiaoming",24,"male");
try {
FileOutputStream fos = new FileOutputStream("E:\student.dat");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(myStudent);
oos.flush();
oos.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
//反序列化方法,用来取序列化后的文件
public void restoreStudent() {
try {
FileInputStream fis = new FileInputStream("E:\student.dat");
ObjectInputStream ois = new ObjectInputStream(fis);
Student myStudent = (Student)ois.readObject();
System.out.println(myStudent);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
全部回答
- 1楼网友:旧脸谱
- 2021-01-19 01:23
package com.vprisk.lrm.base.action;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Student {
//学号
private String sNo="01";
//姓名
private String sName="zhanshan";
//年龄
private String sAge="18";
//性别
private String sGender="男";
public String getsNo() {
return sNo;
}
public void setsNo(String sNo) {
this.sNo = sNo;
}
public String getsName() {
return sName;
}
public void setsName(String sName) {
this.sName = sName;
}
public String getsAge() {
return sAge;
}
public void setsAge(String sAge) {
this.sAge = sAge;
}
public String getsGender() {
return sGender;
}
public void setsGender(String sGender) {
this.sGender = sGender;
}
public Student() {
super();
}
public Student(String sNo, String sName, String sAge, String sGender) {
super();
this.sNo = sNo;
this.sName = sName;
this.sAge = sAge;
this.sGender = sGender;
}
@Override
public String toString() {
return "Student [sNo=" + sNo + ", sName=" + sName + ", sAge=" + sAge
+ ", sGender=" + sGender + "]";
}
public static String txt2String(File file){
String result = "";
try{
BufferedReader br = new BufferedReader(new FileReader(file));//构造一个BufferedReader类来读取文件
String s = null;
while((s = br.readLine())!=null){//使用readLine方法,一次读一行
result = result + "\n" +s;
}
br.close();
}catch(Exception e){
e.printStackTrace();
}
return result;
}
public static void main(String[] args){
Student student=new Student();
File f = new File("C:\\Users\\xie\\Desktop\\student.dat");
try {
f.createNewFile();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
BufferedWriter output = null;
try {
output = new BufferedWriter(new FileWriter(f));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String str =student.toString();//比如这里是你输出的内容
try {
output.write(str);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
output.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(txt2String(f));
}
}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Student {
//学号
private String sNo="01";
//姓名
private String sName="zhanshan";
//年龄
private String sAge="18";
//性别
private String sGender="男";
public String getsNo() {
return sNo;
}
public void setsNo(String sNo) {
this.sNo = sNo;
}
public String getsName() {
return sName;
}
public void setsName(String sName) {
this.sName = sName;
}
public String getsAge() {
return sAge;
}
public void setsAge(String sAge) {
this.sAge = sAge;
}
public String getsGender() {
return sGender;
}
public void setsGender(String sGender) {
this.sGender = sGender;
}
public Student() {
super();
}
public Student(String sNo, String sName, String sAge, String sGender) {
super();
this.sNo = sNo;
this.sName = sName;
this.sAge = sAge;
this.sGender = sGender;
}
@Override
public String toString() {
return "Student [sNo=" + sNo + ", sName=" + sName + ", sAge=" + sAge
+ ", sGender=" + sGender + "]";
}
public static String txt2String(File file){
String result = "";
try{
BufferedReader br = new BufferedReader(new FileReader(file));//构造一个BufferedReader类来读取文件
String s = null;
while((s = br.readLine())!=null){//使用readLine方法,一次读一行
result = result + "\n" +s;
}
br.close();
}catch(Exception e){
e.printStackTrace();
}
return result;
}
public static void main(String[] args){
Student student=new Student();
File f = new File("C:\\Users\\xie\\Desktop\\student.dat");
try {
f.createNewFile();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
BufferedWriter output = null;
try {
output = new BufferedWriter(new FileWriter(f));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String str =student.toString();//比如这里是你输出的内容
try {
output.write(str);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
output.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(txt2String(f));
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯