繁体   English   中英

为什么我们将InputStream read()方法的返回值与-1比较?

[英]Why do we compare the return value of InputStream read() method with -1?

在此while语句中while ((c = in.read()) != -1) -1是什么意思? 有关更多详细信息,请参见以下程序:

 import java.*;

    public class CopyFile 
    {

     public static void main(String args[]) throws IOException 
         {  

          FileInputStream in = null;

          FileOutputStream out = null;

          try {
             in = new FileInputStream("input.txt ");
             out = new FileOutputStream("output.txt");

             int c;
             while ((c = in.read()) != -1) 
            {
                out.write(c);
             }
          }
         finally 
        {
             if (in != null) {
                in.close();
             }
             if (out != null) {
                out.close();
             }
          }
       }

}

read()方法返回int

返回值:

下一个数据字节;如果到达文件末尾,则返回-1。

只是想知道数据是否仍然存在或已到达文件末尾。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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