你看一下api
the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.public int read(byte[] b,
                int off,
                int len)
返回值是你读取的字节数,要是读出来的字节数不够你数组的长度才会-1

解决方案 »

  1.   

    the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.
      

  2.   

    判断一下in.ready(),如果没有准备好,就先等一下
      

  3.   

    /*
     * Created on 2004-9-11
     *
     * To change the template for this generated file go to
     * Window>Preferences>Java>Code Generation>Code and Comments
     */
    package test;import java.io.FileInputStream;
    import java.io.FileOutputStream;/**
     * @author 崔占民
     *
     * To change the template for this generated type comment go to
     * Window>Preferences>Java>Code Generation>Code and Comments
     */
    public class copy
    {
    final private static int INT_READLEN = 10240; public static void main(String[] args)
    {
    if (args.length == 0 || (args.length == 1 && !args[0].equals("/?")))
    {
    System.out.println("command syntax is invalidity!");
    return;
    }
    else if (args[0].equals("/?"))
    {
    System.out.println("copy SrcFile DesFile");
    System.out.println("SrcFile: Source File.");
    System.out.println("DesFile: destination File.");
    return;
    }
    copy cpy = new copy(args[0], args[1]);
    } copy(String aStrSrcFile, String aStrDesFile)
    {
    if (aStrSrcFile.equals("") || aStrDesFile.equals(""))
    {
    System.out.println("please input SrcFile and DesFile!");
    System.out.println("copy SrcFile DesFile");
    return;
    } FileInputStream in = null;
    FileOutputStream out = null;
    try
    {
    int bytes = 0;
    byte[] bteFile = new byte[INT_READLEN];
    in = new FileInputStream(aStrSrcFile);
    out = new FileOutputStream(aStrDesFile); while ((bytes = in.read(bteFile)) != -1)
    {
    out.write(bteFile, 0, bytes);
    }

    System.out.println("File Copy finished!");
    }
    catch (Exception e)
    {
    System.out.println(e.toString());
    }
    finally
    {
    try
    {
    in.close();
    out.close();
    }
    catch (Exception e)
    {
    System.out.println(e.toString());
    return;
    }
    }
    }
    }
      

  4.   

    the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.返回-1表示流已经没有数据了对吧,如果是这样:
    流中有100个数据,一次就读取完毕了,结果肯定返回100而不是-1