Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Unbuffered.... yes..... unbuffered output to a file  RSS feed

 
Janeice DelVecchio
Bartender
Posts: 1810
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok so this project for school has the following criteria:

1. read (unbuffered stream) from file A -- got it to work
2. write (unbuffered stream) to file B
3. read file B and write to file C (both buffered streams) -- I forsee no problem with this.

So when I try to write to file B, gradebook.txt, the first line (if I only do one) works. If I print more than one line I get a runtime error (string index out of bounds) and an empty file. When I println the output it's correct, so I'm pretty sure the problem is with my writing code. I think I'm in need of some syntax help.....

This is what I have:


Thanks!
Janeice
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The second argument to write() is not the file position; it's the character position in the String argument to start at. If you're writing a series of Strings, then that argument will always be 0. And the other version of write() that takes just the String argument is a shortcut for write(string, 0, string.length()), so you might as well just write write(string) for each line.
 
Janeice DelVecchio
Bartender
Posts: 1810
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply....

I tried this for what I thought was the first time.... and now I remember why I used the long version of write (with the three parameters)....

The output I get in this case is one line. The last line entered. I wanted to set the position to start the writing AFTER (or I suppose insert it before.... if that were possible) the position the last one ended. Now that I understand what is meant by position, it makes perfect sense why I'm getting the runtime error.

Please let me know what you think...

.... as an addendum...... do you think if I used a stringbuffer to build the output then send it to the "writer" that it would be considered buffered writing? I've been tossing this idea around but I'm not sure what EXACTLY is meant by "unbuffered"......


Janeice
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If this is a homework problem, I suspect as long as you're not using BufferedWriter/BufferedOutputStream, you're doing unbuffered output.
 
Janeice DelVecchio
Bartender
Posts: 1810
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks! that solved my issues!!



--Janeice
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!