为什么在读取文件时我们总是检查 -1?
why while reading a file we always check for -1?
我正在尝试从 "read.txt" 读取文件,并且在读取文件时我们总是检查 -1(行:while(readfile!=-1)。
为什么我们总是通过比较"-1"来检查结束文件。有什么具体原因吗?
(int readfile = filereadrer.read();)always returns unicode value by this argument -1 表示文件末尾的 unicode 值,如果是那么我们如何知道文件末尾的 unicode 值为“-1”
public class FilesPractise {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
File file = new File("read.txt");
file.createNewFile();
FileReader filereadrer = new FileReader(file);
int readfile = filereadrer.read();
while(readfile!=-1){
System.out.println((char)readfile);
readfile = filereadrer.read();
}
}
}
这些东西可以在方法的文档中找到。例如,您要查找的方法是 read
,来自 FileReader
.
这是 read
方法的文档:https://docs.oracle.com/javase/8/docs/api/java/io/InputStreamReader.html#read--
在页面的 "Returns" 部分下,
The character read, or -1 if the end of the stream has been reached
看到了吗?这就是你了解这些东西的方式。
Why we always check the end file by comparing it with "-1".Is there any specific reason?
因为如果你不这样做,你将永远检测不到流的结尾,你将无限循环。
(int readfile = filereadrer.read();)
always returns unicode value
不,不是。它 returns -1 或 一个 Unicode 值。请参阅 Javadoc。
by this argument
这不是争论。这是一个错误的假设。
does -1 means the unicode value of end of file
不存在 (a) -1 的 Unicode 值或 (b) 文件末尾的 Unicode 值。
if it is
不是。
then how we got to know that unicode value of end of file is "-1"
它既不是 -1 也不是 "-1"
。它是由 Unicode 值的 API 而不是 返回的标记值。
我正在尝试从 "read.txt" 读取文件,并且在读取文件时我们总是检查 -1(行:while(readfile!=-1)。
为什么我们总是通过比较"-1"来检查结束文件。有什么具体原因吗?
(int readfile = filereadrer.read();)always returns unicode value by this argument -1 表示文件末尾的 unicode 值,如果是那么我们如何知道文件末尾的 unicode 值为“-1”
public class FilesPractise {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
File file = new File("read.txt");
file.createNewFile();
FileReader filereadrer = new FileReader(file);
int readfile = filereadrer.read();
while(readfile!=-1){
System.out.println((char)readfile);
readfile = filereadrer.read();
}
}
}
这些东西可以在方法的文档中找到。例如,您要查找的方法是 read
,来自 FileReader
.
这是 read
方法的文档:https://docs.oracle.com/javase/8/docs/api/java/io/InputStreamReader.html#read--
在页面的 "Returns" 部分下,
The character read, or -1 if the end of the stream has been reached
看到了吗?这就是你了解这些东西的方式。
Why we always check the end file by comparing it with "-1".Is there any specific reason?
因为如果你不这样做,你将永远检测不到流的结尾,你将无限循环。
(int readfile = filereadrer.read();)
always returns unicode value
不,不是。它 returns -1 或 一个 Unicode 值。请参阅 Javadoc。
by this argument
这不是争论。这是一个错误的假设。
does -1 means the unicode value of end of file
不存在 (a) -1 的 Unicode 值或 (b) 文件末尾的 Unicode 值。
if it is
不是。
then how we got to know that unicode value of end of file is "-1"
它既不是 -1 也不是 "-1"
。它是由 Unicode 值的 API 而不是 返回的标记值。