Win a copy of Java 9 Revealed this week in the Features new in Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Using '/r' as with 'C++'  RSS feed

 
Suresh Kanagalingam
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
Is there an escape sequence is Java, which mimicks '/r' in 'C++'?
Using C++ '/r' will overwrite current line. When I tried '/r' in Java it is simply writing in new line.
Thanks
Suresh
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The behavior you're observing depends on the operating system and/or file editor you're using, not the programming language. For example, I'm using Windows XP. If I write "123\r4" to standard output, I will see only "423" - the "1" is overwritten. But if I write the same string to a file, the results depend on the program I use to view the file. Using Notepad, I see "1234", but with WordPad or TextPad, I see
123
4
Ultimately this means that unless you're developing for a very specific environment, you have no way to guarantee the overwriting behavior you're trying to achieve using "\r". Your best options are: (a) save what you want to write in memory, and perform any needed overwrites or other edits in memory before you write anything to output; (b) use something like RandomAccessFile, which allows you to go back to a given part of a file and overwrite it; or (c) revise your algorithm to remove any need to overwrite lines. Good luck.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!