[英]java read file -— Why my code works
I am new in Java. 我是Java新手。 I am learning the fileIO.
我正在学习fileIO。 I was writing a little program to display the content in a txt file.
我正在编写一个小程序以在txt文件中显示内容。 My code is as follow :
我的代码如下:
import java.io.*;
public class Readf {
public static void main(String arg[]) throws IOException {
FileInputStream in = null ;
try {
in = new FileInputStream("input.txt");
int c;
String cs="";
while ((c=in.read())!=-1) {
cs = cs + (char)c;
if ((char)c=='\n') {
System.out.println(cs);
cs="";
}
}
} finally {
if (in != null) {
in.close();
}
}
}
}
I was reading an online tutorial. 我正在阅读在线教程。 It told me to read the file with an int variable.
它告诉我使用int变量读取文件。 Since I wanted to display the content in char, I cast them into char type and store it into a sting and it WORKED!!!!
由于我想在char中显示内容,因此我将它们转换为char类型并将其存储在字符串中,并且可以正常工作!!!! In Java, a int variable is 32-bit but a char variable is 16-bit.
在Java中,int变量为32位,而char变量为16位。 I cast them into 16bit char every time I read an 32bit int.
每次读取32位int时,我都会将它们转换为16位char。 Why the result wasn't a chaos?
为什么结果不是那么混乱?
Check read() method description in FileInputStream class: 检查FileInputStream类中的read()方法描述:
FileInputStream.read() FileInputStream.read()
As you can see in specification, a read method "Reads a byte of data from this input stream" which means all your ints in your program (c variable) will always be less than or equal to 255 and greater that or equal to 0. It doesn't matter if you read txt file or pdf, png etc. 正如您在规范中所看到的,一种读取方法“从此输入流中读取一个字节的数据”,这意味着程序中的所有int(c变量)将始终小于或等于255且大于或等于0。阅读txt文件或pdf,png等都没有关系。
You can check it by trying to print something when c is greater than 255, eg.: 您可以通过在c大于255时尝试打印一些内容来进行检查,例如:
if (c > 255) {
System.out.println("c>255");
}
and "c>255" will never be printed. 和“ c> 255”将永远不会被打印。
Because int is signed you can also check if c is less than zero. 因为int是带符号的,所以您还可以检查c是否小于零。
if (c < 0) {
System.out.println("c<0");
}
and again "c<0" will never be printed as well. 再次不会打印“ c <0”。
Only last int will be -1 of course. 当然,仅最后一个int将为-1。
So every int from a range <0, 255> can be safely cast to char without loosing any information. 因此,可以将范围<0,255>中的每个int安全地转换为char而不丢失任何信息。
All the characters you will possibly keep in your file will be well within the range of 16 bit unicode character set which java works upon. 您可能会保留在文件中的所有字符都将位于Java可以使用的16位unicode字符集的范围内。 Hence there should not be any problem converting a char(16 bit) to an int(32 bit) or vice versa.
因此,将char(16位)转换为int(32位),反之亦然没有问题。
Actually, I respect your insistence to understand rather than to just get the desired output. 实际上,我尊重您坚持理解而不是仅仅获得所需输出的坚持。
The catch is that each character you scan has an ascii value below 2^8 which makes it fits in int
32-bit and fits in char
16-bit. 要注意的是,您扫描的每个字符的ascii值都低于2 ^ 8,这使其适合
int
32位,适合char
16位。 The chaos you are taking about will appear in the case an ascii value fits in 32-bit but not in 16-bit which will not exist in ascii character set but in unicode. 如果ascii值适合32位而不适合16位,则出现的混乱情况将出现在ascii字符集中,但在unicode中不存在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.