简体   繁体   中英

what's differences between windows and linux about FileInputStream read?

for example, a file size is 1024 bytes that size is the same in windows and linux. but as I read the file like below

FileInputStream fileInputStream = new FileInputStream(file);

for example, in windows, bytes length is 1026 but in linux, bytes length is 1024 or on the contrary to this in windows, bytes length is 1024 but in linux, bytes length is 1022 always in windows bytes length is greater than in linux.

what's differences?

The problem you are detecting probably has more to do with the Windows vs Unix line endings .

As you must be reading the file in text mode, in Windows the end of a line is determined by the two symbols \\r\\n (represented in hexadecimal as 0x0A and 0x0D respectively), while in Unix systems it uses only \\n .

I'll make a wild guess and say your file has two lines. In Windows it will be replacing \\n by \\r\\n in each line end, creating 2 bytes that don't really exist in the original file.

And in Linux, when reading a file generated in Windows, its doing the opposite, eating 2 bytes.

My guess is you are reading a text file and that file has been converted to use windows new lines which are \\r\\n instead of the linux \\n

If you write text or binary without converting, the files will contain exactly the same bytes and exactly the same number of bytes.

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