-
BufferedWriter Problem
Hi,
i want to make mini-school project and so i create 3 class(School,Student and Run) i want to write students name out.txt with using bufferedwriter but i can't do it.
School Class Code;
Code:
import java.util.*;
public class School {
private int schoolId;
private String schoolName;
public int getSchoolId() {
return schoolId;
}
public School(int schoolId, String schoolName) {
super();
this.schoolId = schoolId;
this.schoolName = schoolName;
}
public void setSchoolId(int schoolId) {
this.schoolId = schoolId;
}
public String getSchoolName() {
return schoolName;
}
public void setSchoolName(String schoolName) {
this.schoolName = schoolName;
}
public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}
public List<Student> students;
}
Student Class Code;
Code:
public class Student {
private int studentId;
private String studentName;
public int getStudentId() {
return studentId;
}
public Student(int studentId, String studentName) {
super();
this.studentId = studentId;
this.studentName = studentName;
}
public void setStudentId(int studentId) {
this.studentId = studentId;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public School getSchool() {
return school;
}
public void setSchool(School school) {
this.school = school;
}
private School school;
}
and Run Class Code;
Code:
public static void main(String[] args) {
// TODO Auto-generated method stub
School sc = new School(1, "Berkeley");
sc.setStudents(new ArrayList<Student>());
Student st1 = new Student(1,"Bill Gates");
st1.setSchool(sc);
sc.getStudents().add(st1);
Student st2 = new Student(2,"Steve Jobs");
st2.setSchool(sc);
sc.getStudents().add(st2);
Student st3 = new Student(3,"Larry Page");
st3.setSchool(sc);
sc.getStudents().add(st3);
try {
String file="d:\\okul\\out.txt";
FileWriter fw = new FileWriter(file,true);
BufferedWriter bw = new BufferedWriter(fw);
List<Student> t=sc.getStudents();
bw.write(t);
bw.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
but line of "bw.write(t)" said problem.
"The method write(int) in the type BufferedWriter is not applicable for the arguments (List<Student>)"
how can i correnting this problem ?
thanx.
-
Read the API.
You can't stick an arraylist into any of the write() methods.
You, presumably, want to print out each Student, so iterate over that List and write them out individually. Of course you'll also need to write code to write out Student, or provide Student with a toString() method to override the base one.
I would recommend using PrintWriter instead, though.
-
thank you tolls, i search Serialization method.i write this code
Code:
String fileName="D:\\okul\\out.txt";
FileOutputStream fos=new FileOutputStream(fileName);
ObjectOutputStream oos=new ObjectOutputStream(fos);
List<Student> t=new sc.getStudents();
oos.writeObject(t);
oos.close();
but line of "List<Student> t=new sc.getStudents();" said this problem "sc cannot be resolved type". please help
-
You don't need the 'new' keyword.
Code:
List<Student> t=new sc.getStudents();