Results 1 to 4 of 4
  1. #1
    dailywalker is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default 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;

    Java 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;

    Java 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;

    Java 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.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    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.

  3. #3
    dailywalker is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default

    thank you tolls, i search Serialization method.i write this code

    Java 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

  4. #4
    cultclassic is offline Member
    Join Date
    Mar 2011
    Posts
    64
    Rep Power
    0

    Default

    You don't need the 'new' keyword.
    Java Code:
    List<Student> t=new sc.getStudents();

Similar Threads

  1. Nullpointer Exception with BufferedWriter?
    By Solarsonic in forum New To Java
    Replies: 3
    Last Post: 03-23-2011, 12:08 AM
  2. BufferedWriter, FileWriter
    By ladykrimson in forum New To Java
    Replies: 4
    Last Post: 12-06-2010, 02:35 AM
  3. BufferedWriter
    By Karthika in forum New To Java
    Replies: 6
    Last Post: 04-22-2010, 09:23 AM
  4. Null Pointer Exception with BufferedWriter
    By Sasquatch192 in forum New To Java
    Replies: 3
    Last Post: 10-26-2009, 06:32 PM
  5. Replies: 0
    Last Post: 06-26-2008, 07:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •