Jump to content
xisto Community
Sign in to follow this  
xenador

Writing Text To A File bufferedWrite works good with Strings but what about ints?

Recommended Posts

So I really like the way that the BufferedWrite works on the Strings, I have tested it with several loops and such with varying text, but now I went to try it with my actual output and it destroyed any int that I feed it and replaced it with an unknown char symbol (more precisely ?) I am using a windows system, and viewing my files with notepad(windows)...is there any output that is platform independent and handles like the BufferedWriter, or operates with a comparable upper bounds on the space and time components?

Share this post


Link to post
Share on other sites

When you try to write an integer to a file, it is likely that it will be interpreted as a char. To append numbers to the file, you would need to use the toString method first to convert to a string, or concatenate it with a string (e.g. "myInt (converted to a string): "+myInt). The reason that the numbers you have sent to output are interpreted as a ? is likely to be that the bytes in the file which have been outputted from your program are not printable ASCII characters (i.e. 0-31, 127). If you open the file in a hex editor, then you should be able to see the output.

Edited by Nabb (see edit history)

Share this post


Link to post
Share on other sites

Nabb: interesting, everything I was writing to the file was a primative so I didnt have a to string for it, but string concatenation will work for anyone who is wondering aka:int num = 15;System.out.println("" + num);will result in "15"

Share this post


Link to post
Share on other sites

Depends a lot what method you're using to do the write. A lot of the methods are overloaded for not only strings but also raw bytes in various forms. It may be safest just to force a string conversion as myInt+"" or (new Integer(myInt)).toString() if you're going to be using it in a string context. The compiler is smart enough to convert for you in many cases, but unfortunately not all, and especially not when there is an ambiguity as to whether you want the string representation or raw numerical value.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.