Andrey Boubriak wrote:And when I try adding a -1 mid way through the text document
In a computer, all letters spaces numbers represent as numbers called ascii.
Jeff Verdegan wrote:If you read the docs for InputStream.read(), you'll see that it returns the byte that was read, or else a -1 if there are no more bytes to read.
So internally, it's logic will look something like
No great mystery.
Andrey Boubriak wrote:I thought the byte FileInputStream reader read bytes, not the character stream FileReader? What's the difference then?
but how would you print the text in a file to the console if the both streams return integer values? Surely you don't have to write a method that converts the integer value to the equivalent unicode character?
Likewise if you want input from a user, how do you convert that (the word "orange" for example) into an integer number to be written?
Also what if you want have the number 65 and you want to write that to a text file? If you use an int variable it will then misinterpret that as the character "a" ...
Tony Docherty wrote:
You don't, the writer does that for you. Although it actually converts the characters to bytes and not integers.
Andrey Boubriak wrote:How should I get the text out of the file in a string format?
Jeff Verdegan wrote:My guess is that what you're reading is not a text file, but a Word doc or something--something that contains more than just text--and your console is just ignore the junk output, but your PrintWriter/FileWriter combo is trying to convert things that aren't characters into characters. Either that or some kind of weird encoding inconsistency.
How are you creating the original file?
How are you reading the one that appears messed up that your program produces?
Andrey Boubriak wrote:
I copied text of a website pasted it into notepade saved it as a .txt file making sure encoding was unicode
Paul Clapham wrote:I don't use Notepad (and really neither should you) but when I opened it up to see what you should be doing, I don't see anywhere which allows you to specify the charset when you save a file.
Tony Docherty wrote:
Paul Clapham wrote:I don't use Notepad (and really neither should you) but when I opened it up to see what you should be doing, I don't see anywhere which allows you to specify the charset when you save a file.
In the file save dialog there is an 'Encoding' drop down just below the 'File type' drop down which allows you to select from a few options. The default is ANSI.
@Andrey Boubriak: I suggest you save the file using the default Notepad encoding of ANSI and try to run your program again.
I've read about this kind of thing at the checkout counter. That's where I met this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
|