JAVA中如何实现文件读写对象?急吖
3个回答
展开全部
java中引进了I/O包用于对文件的处理,你说的文件处理对象是file类,若要对其进行输入输出处理的话,需要进行如下几步:1 创建文件对象:File file1=new File("文件目录");
2 用这个文件对像创建文件读写对象:FileInputStream inputstream=new FileInputStream (file1);
这是文件读对象,对应的写对象就是把FileInputStream改成FileOutputStream就可以,
希望能帮上你!!
2 用这个文件对像创建文件读写对象:FileInputStream inputstream=new FileInputStream (file1);
这是文件读对象,对应的写对象就是把FileInputStream改成FileOutputStream就可以,
希望能帮上你!!
展开全部
你说的是对象序列化。。。一个类,让此类实现Serializable接口。。。
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
/**
*
* @author top
*/
public class Inout {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws Exception {
// TODO code application logic here
Student student1 = new Student("top_beidu", 23, 99.9f);
Student student2 = new Student("student2", 24, 69.9f);
Student student3 = new Student("student3", 25, 89.9f);
System.out.println("输入的是:");
student1.print();
student2.print();
student3.print();
FileOutputStream fos = new FileOutputStream("student.txt");
// FileOutputStream fos = new FileOutputStream("F:/student.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(student1);
oos.writeObject(student2);
oos.writeObject(student3);
oos.close();
FileInputStream fis = new FileInputStream("student.txt");
// FileInputStream fis = new FileInputStream("F:/student.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
Student stu1 = (Student) ois.readObject();
Student stu2 = (Student) ois.readObject();
Student stu3 = (Student) ois.readObject();
System.out.println("输出的是:");
stu1.print();
stu2.print();
stu3.print();
ois.close();
}
}
class Student implements Serializable {
private String name;
private int age;
private float score;
public Student(String name, int age, float score) {
this.name = name;
this.age = age;
this.score = score;
}
public void print() {
System.out.println("name=" + name + ";age=" + age + ";score=" + score);
}
}
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
/**
*
* @author top
*/
public class Inout {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws Exception {
// TODO code application logic here
Student student1 = new Student("top_beidu", 23, 99.9f);
Student student2 = new Student("student2", 24, 69.9f);
Student student3 = new Student("student3", 25, 89.9f);
System.out.println("输入的是:");
student1.print();
student2.print();
student3.print();
FileOutputStream fos = new FileOutputStream("student.txt");
// FileOutputStream fos = new FileOutputStream("F:/student.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(student1);
oos.writeObject(student2);
oos.writeObject(student3);
oos.close();
FileInputStream fis = new FileInputStream("student.txt");
// FileInputStream fis = new FileInputStream("F:/student.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
Student stu1 = (Student) ois.readObject();
Student stu2 = (Student) ois.readObject();
Student stu3 = (Student) ois.readObject();
System.out.println("输出的是:");
stu1.print();
stu2.print();
stu3.print();
ois.close();
}
}
class Student implements Serializable {
private String name;
private int age;
private float score;
public Student(String name, int age, float score) {
this.name = name;
this.age = age;
this.score = score;
}
public void print() {
System.out.println("name=" + name + ";age=" + age + ";score=" + score);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
什么叫文件读写对象?
你要读什么文件呀?文本文件可以用BufferedReader和PrintWriter,二进制文件可以用FileInputStream和FileOutputStream,你要做对象读写可以用ObjectInputStream和ObjectOutputStream。
你要读什么文件呀?文本文件可以用BufferedReader和PrintWriter,二进制文件可以用FileInputStream和FileOutputStream,你要做对象读写可以用ObjectInputStream和ObjectOutputStream。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询