• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

File IO

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I need some help...
How can I use FileWrite and FileReader in my Student class?
Should I make an extra class or I can add those to Student class?






This is my class:

 
Ranch Hand
Posts: 479
1
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Leni,

Since this looks like a simple program I'd say that you might consider adding two more methods, one for reading from the file and the other for writing to it? Don't see much sense in another helper class for this purpose.

Cheers,
Raj.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Disagree. File reading is not a responsibility of the Student class, and creating a helper class will make for better design. Also you can reuse the helper class.

But you may find it easier to use a java.util.Scanner for reading and a java.util.Formatter for writing, as long as you don't need to append details to your files.

Change the try-catch to read like thisThe use of "finally" ensures that the reader closes, even when there is an Exception. You need two nested try blocks. Similarly with a writer.
 
Lenny Peter
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks

But I need more help :roll:

I have a class Student and I will make an extra class StudentIO with private Student student?
And?








 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A well known way would be to write a Data Access Object. See http://java.sun.com/blueprints/patterns/DAO.html

That is, you could have a StudentFilesystemDAO, with methods like

Student getStudent(String name)
Student[] getAllStudents()
void saveStudent(Student student)

etc.pp.

Does that help?
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Leni Kravitskki:
Thanks
And?

public void what? openFile()?

Whatever the method your original reading code quoted earlier would have been in.
And don't use System.out for printing Exceptions, use System.err.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic