• Post Reply Bookmark Topic Watch Topic
  • New Topic

instantiate objects of class and writing them to a text file  RSS feed

 
bruce kenney
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to create a new text file and instantiate objects using an array that writes them to a file and I need help with the array part.

 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you clarify what the problem is?
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bruce, welcome to the Ranch!

Just looking at that code, it seems to me you're handling the array just fine. So could you be a bit more specific about what your question is?
 
bruce kenney
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I open the text file it is blank. Shouldn't the array objects appear on the notepad. Also, I deleted the text file from my folder to see if I could somehow refresh it in some way, but now when I recompile the program it and look for the text file in my folder nothing appears.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you closing the Formatter?
 
bruce kenney
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No not yet, could that be why nothing printed to it? And could I do the same thing if use File file = etc; instead of Formatter?
 
bruce kenney
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And now I can't get it to recreate the file. I deleted it from my folder and wrote the formatter again, but it won't create the text file again.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anything that implements the Closeable interface needs to be closed, or unpredictable things will happen.
 
bruce kenney
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, so if I use formatter I can use the close method like

}

but if use File to create a new file I cant use the close method nor can I use it to add the arrays, like I did with file.format etc. Any help with this matter?
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Except Formatters pointing to System.out and System.err.
If you close them you may never be able to use the Streams again.
If you don't close your Formatter it may never finish writing; that might be why your file is blank. Read this and this.
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bruce kenney wrote:Ok, so if I use formatter I can use the close method like
. . .
No.

The best way to close it is with try with resources.
 
bruce kenney
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We don't use BufferedReader or FileReader yet, so I can't use those. Either File or Formatter, but when I use File it won't work.
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You use a buffered reader for reading things; you will find you can replace that with Scanner in more recent code.
You use writers for writing, and Formatter in newer code. You still have to close whatever you are reading and writing with (except for System.in System.out and System.err).

Try with resources is probably the best way to close readers and writers.
 
bruce kenney
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok this is what I have to do. Instantiate the array objects into a new file, one that I create in the same program. Will my code that I showed do that? Or do I have to serialize it? Sorry, this section is confusing to me.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the only thing that's missing is to close the Formatter, then it should work.
 
bruce kenney
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, thanks. Now, if I can only create the file again. I deleted it in the folder that I have the other codes in and for some reason it won't create it. Thank you though!
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code creates a file in your current working directory. If you can't find it, then that would mean you don't know what your current working directory is, so you should find that out. If you're using a command shell (e.g. the Windows command line), then the chdir command will tell you that. If you're using some kind of an IDE then I don't know how to find out -- perhaps you should consider using an absolute path rather than a relative path.
 
bruce kenney
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nope it's not there. As soon as I deleted it from my files to try and recreate it, the file won't be formed again I deleted my files for my code and re-wrote everything and no file gets created again.
 
bruce kenney
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nevermind, I figured it out!
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what was the solution?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!