简体   繁体   中英

Java Character Encoding

I have a text file having some Hindi characters and my default character encoding in ISO 8859-1. I am using "FileInputStream" to read the data from that file and "FileOutputStream" to write data to another text file.

My code is:

    FileInputStream fis = new FileInputStream("D:/input.txt");
    int i = -1;
    FileOutputStream fos = new FileOutputStream("D:/outputNew.txt");
    while((i = fis.read())!= -1){
        fos.write(i);
    }
    fos.flush();
    fos.close();
    fis.close();

I am not specifying encoding ("UTF-8") anywhere, but still the output file in having proper text.How it is happening , i am not getting?

It's working because you don't use any char in your program. You're just transferring raw bytes from one file to another. It would be a problem if you read and wrote characters, because then an encoding would be used to transform the bytes in the files to characters, and vice-versa.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM