File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Object in a file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Object in a file" Watch "Object in a file" New topic
Author

Object in a file

Zenitis Stoichkov
Greenhorn

Joined: Sep 01, 2009
Posts: 14
Hello to everybody, I m new to java so i ve got some problems with the new project that i have. I want to parse some variables to a txt file. I ve found some ways to do that by none of them worked properly. For example i ve got one class, some private variables and in the main method i tried to parse them to the file:
The code is:

Any idea?
Nishan Patel
Ranch Hand

Joined: Sep 07, 2008
Posts: 689

Hi Zenitis,

Welcome to Java Ranch.


Thanks, Nishan Patel
SCJP 1.5, SCWCD 1.5, OCPJWSD Java Developer,My Blog
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40061
    
  28
Please tell us more details; you don't say what you expect to happen, nor what actually happens.

You don't appear to be reading from any files anywhere. You appear to have both a Scanner and a BufferedReader reading from System,in; I am sure you don't need both.
Zenitis Stoichkov
Greenhorn

Joined: Sep 01, 2009
Posts: 14
Ok my problem that i want to parse in file more than one object. But in the file only one object is saved the last one. Sorry but i m new to java and i don t exactly understand some things. This is the reason that i tried both a Scanner and a BufferedReader reading.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40061
    
  28
I think you are trying to go too fast.You need to go very slowly.

Write down the stages of what you need to do. Then subdivide each stage into smaller stages, and subdivide those stages further . . .
Go through your main method and write down what each line does.
Zenitis Stoichkov
Greenhorn

Joined: Sep 01, 2009
Posts: 14
Generally i want to make a programme for a dentist. Something like a calendar and i want to make a menu in which dentist can store his clients. So in this example i tried to store to file some informations about clients name surname ID...I first use the BufferedReader to parse something to variables and afterthat i tried to store it in a file.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40061
    
  28
So what happens when you enter a name?

You are still trying too many things at the same time. Small pieces, then put them together.
Zenitis Stoichkov
Greenhorn

Joined: Sep 01, 2009
Posts: 14
the problem is that when i rtied to parse more than one object(a run of five variables) in a file only the last object is saved in the txt. I ll make a new method for bufferedReader in class if this is better..
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40061
    
  28
Look through the constructors for the file writer classes; they often have a parameter which allows appending to the file.
Zenitis Stoichkov
Greenhorn

Joined: Sep 01, 2009
Posts: 14
Ok after 2 days searching and searching i found some things that may work.I first split the program in 6methods(plus set/get and constructors). When i tried the program the first time it was ok and the first object is successfully stored in txt. But after that i was receiving this:

Exception in thread "main" java.lang.NullPointerException
at ofthalmiatreio.Asthenhs.write_new_record(Asthenhs.java:223)
at ofthalmiatreio.Asthenhs.main(Asthenhs.java:335)
Java Result: 1
Zenitis Stoichkov
Greenhorn

Joined: Sep 01, 2009
Posts: 14
the code is fistly
Zenitis Stoichkov
Greenhorn

Joined: Sep 01, 2009
Posts: 14
the find method it use the integer key (for client's id) and return an object
Zenitis Stoichkov
Greenhorn

Joined: Sep 01, 2009
Posts: 14
methods for opening a file close a file read file and readopen file
Zenitis Stoichkov
Greenhorn

Joined: Sep 01, 2009
Posts: 14
and the last one for writing objects into file firstly it try to find if in the file exist object with same account else write the object in file
Zenitis Stoichkov
Greenhorn

Joined: Sep 01, 2009
Posts: 14


of course the main method..
Zenitis Stoichkov
Greenhorn

Joined: Sep 01, 2009
Posts: 14
i guess that the problem is in write_new_record method especially in the try-catch. I don t exactly understand which exceptions i must use in this situation of files.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40061
    
  28
It is not possible to tell from the code posted where the Exception occurred; you will have to work that out for yourself. Go through the line and look at every object reference and work out whether it could be null. If you can't tell, put a System.err.printf command before that line:That will help you work out which object is null.
Zenitis Stoichkov
Greenhorn

Joined: Sep 01, 2009
Posts: 14
Is there any possibility that BufferedWriter outputt;may be the problem?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40061
    
  28
If it is the first line where you use that Writer, yes, it might be null. The code quoted doesn't show where you instantiate it. In fact Writers should be opened as local variables, and closed in a finally block, to release access to the File. Brian Legg gave a little example in this thread. You will have to look at older examples quoted from that thread and see where I made a mistake in a post to work out the whole thing. If you have difficulty following those old threads, I shall give an example later.
Zenitis Stoichkov
Greenhorn

Joined: Sep 01, 2009
Posts: 14
okay I got confused with Formatter bufferedWriter and FileWriter. I filnally change variables from class
# FileWriter out;
# Formatter out1;
# BufferedWriter output; //it s useless
# Scanner input=new Scanner(System.in);
# BufferedWriter outputt;

Formatter out1;
FileWriter out;
Scanner input=new Scanner(System.in);
BufferedWriter outputt;

and i changed in the method open_FIle from output to outputt.
After that the program run and show me the message The information is stored.
Nevertheless none of the variables stored in the file.
Zandis Murāns
Ranch Hand

Joined: Aug 18, 2009
Posts: 174

Why dont you create serializable object (implements Serializable) and then write whole object in to the file?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40061
    
  28
As promised yesterdayThat is how you can write an array of Strings to a text file, each on one line. The FileWriter class writes text into files. You will have to alter your logic for your application. It might be better to create a Patient object and give it a good toString() method, then you can useAs you probably already have realised, it is often easier to use a Formatter, but I have never worked out whether you can append anything to a file with a Formatter.
Zenitis Stoichkov
Greenhorn

Joined: Sep 01, 2009
Posts: 14
man thanks a lot for you help i ll try it right now.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Object in a file