Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: BufferedWriter Problem

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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;

    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;

    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;

    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
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: BufferedWriter Problem

    I think you should have to change your data into string first and then place in the file.
    t.toString();

  3. #3
    Junior Member
    Join Date
    Apr 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: BufferedWriter Problem

    thank you Mr.777, i search Serialization method.i write this 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
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: BufferedWriter Problem

    Definitely, what is sc by the way?