• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with .writeUTF  RSS feed

 
Emilian Chmiel
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I wrote a program:




its works fine, but in file.txt I have got:

loop: 1 loop: 2 loop: 3 loop: 4 loop: 5 loop: 6 loop: 7 loop: 8 loop: 9 loop: 10
 
Carey Brown
Saloon Keeper
Posts: 3317
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
DataOutputStream is used to write serialized objects, usually to a file. It is generally considered to be a binary file, not a text file. It's not clear what results you expected, were you expecting a new-line after each Loop #? "writeUTF()" does not append a new-line like println() does. If you want a new-line you'll have to add it yourself, being sensitive to platform conventions, ie, on Windows it is "\r\n", for everybody else it's "\n".
 
Tim Moores
Saloon Keeper
Posts: 4032
94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it works fine, then what is the problem? At a quick glance, what you posted as the contents of the file look correct. What did you expect instead, and why?

Edit: What he said :-)
 
Carey Brown
Saloon Keeper
Posts: 3317
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
P.S.
writeUTF() outputs the length of the string in binary followed by the actual string. Opening the file in a text editor or displaying it on the screen may or may not show that you have some non-text bytes embedded there.
 
Campbell Ritchie
Marshal
Posts: 56540
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Emilian Chmiel wrote:. . . its works fine. . . .
No, it doesn't. It just about works; had you written numbers as well, the file would never have opened. Don't use output streams to write text files and don't use input streams to read text files. Use writers and readers. Or a Scanner and a Formatter. It is all in the Java™ Tutorials.
▭loop: 1 ▭ loop: 2 ▭ loop: 3 ▭ loop: 4 ▭ loop: 5 ▭ loop: 6 ▭ loop: 7 ▭ loop: 8 ▭ loop: 9 ▭ loop: 10
That is because you are using a line end sequence which is inappropriate to your system (maybe \n, which is normal for Unix, on a Windows® box which expects \r\n). Many programs can correct for different line ends but not MS NotePad. It cannot recognise \n on its own and paints a little box which doesn't appear on the web page. I replaced the boxes with ▭ and got little boxes back. Look at Unicode and search for 25ad. Had you written...you would have had the correct line ends.

There are all sorts of stylistic things in your code which shou‍ld be improved, but there are other design problems.
Don't close the reader or writer explicitly. Use try with resources which ensures the close() method is called.
Don't call flush(). There is no need to flush the writer after every line, and the close() method calls flush() anyway.
Don't squeeze all your code into one method. You shou‍ld change that into multiple methods and move the code out of the main method.
 
Carey Brown
Saloon Keeper
Posts: 3317
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a hex dump of your file. Notice a "00 07" in front of each loop string, that is the length of the string. Again, not a text file. It's that binary 0007 that will cause a box character to appear when you view the file in notepad.
 
Campbell Ritchie
Marshal
Posts: 56540
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was going to say, Carey, that your explanation with the length before the text is likely to be correct and I was mistaken about line ends.
Agree that you shouldn't use those classes with text files.

I ran that code and tried to open the output file with pluma, and it failed to open at all because of incorrect encoding. I got the same result as you (I think) when I installed hexedit and used it to open that file.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!