Reputation: 35
I have few characters in notepad that takes 2 or 3 bytes. I am able to use inputstream and output stream to copy the files. Bytes stream is for ASCII characters and Character streams should be used for UNICODE characters. How does input stream process 2 or 3 bytes characters?
FileInputStream fis = new FileInputStream("E:\\Users\\17496382.WUDIP\\Desktop\\qwert.txt");
FileOutputStream fos = new FileOutputStream("E:\\Users\\17496382.WUDIP\\Desktop\\qwert1.txt");
byte[] buffer = new byte[1024];
int len;
while((len = fis.read()) != -1){ //do this until int len is not -1
System.out.println((char)len);
fos.write(buffer, 0, len);
Upvotes: 0
Views: 44
Reputation: 73558
It doesn't. InputStreams
read bytes and Readers
read characters.
Your code will display garbage if it encounters a multibyte char. It may display garbage otherwise too, since you're assuming that byte = char
(while that would work in many encodings).
Lastly: Joel Spolsky's excellent article on Unicode. Read it and you'll be smarter than a lot of other developers.
Upvotes: 1