Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How can PrintWriter know we have reached end of page?

 
Jehan Jaleel
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy,

If I was using PrintWriter to write some data to a plain ol' text file like so....



How can I know that I have reached the end of the current page and I am on the next page? I am asking because I also need to print page numbers.

Thanks in advance for any help,
Jahan

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would have to know how many lines a page can hold, keep a count, and handle it yourself. The PrintWriter has no way of knowing.
 
Jehan Jaleel
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jeff.

Would you or anyone else who views this happen to know how many lines there are in a standard 8 x 12 page given the standard font size that Java prints in?

Thanks again.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65115
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no "standard font" that Java prints in. In fact, there's no concept of printing here at all. You are just creating a text file.

How many lines fit on your "page" is completely and solely dependent upon your system and what tools you use to print the page.

If you want more control over page layout, you'll need to emit something like a PDF rather than a text file.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:There is no "standard font" that Java prints in. In fact, there's no concept of printing here at all.


And more importantly, no concept of font. Neither String nor PrintWriter has any font associated with it, nor any knowledge that fonts even exist. All they care about is characters.
 
Jehan Jaleel
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff,

You told me that I would need to know how many lines a page can hold, do you have any suggestions how I can figure this from my Java code?

Thanks again for your help.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jehan Jaleel wrote:Jeff,

You told me that I would need to know how many lines a page can hold, do you have any suggestions how I can figure this from my Java code?


I doubt it's possible for plain text. If you google for something like printing from Java, you'll find examples on how to get information about available printers. You might be able to ask those printers what their default font is, what page size they have, etc., and then you might be able to look at the metrics for that font and calculate how many lines per page.

In reality, though, that's probably an untenable approach. And you could only do it at the point when you go to print. It's absolutely impossible to do when you generate the text file, unless you know where it will be printed. But seriously, I don't think you'll be able to do all that.

As already suggested, look into something like PDF.

Or, when you go to print your text file, in the page setup, you should have the option to add page numbers, and the printer will figure it out.


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic