通过网络传输? 还是用ftp吧.

解决方案 »

  1.   

    那看看我是那里写的有问题!!  public static void saveAs(File oldF,File newF){
        if (oldF.exists()&& oldF.isFile()) {      File nf = null;
          File of = null;
          byte[] bytes = null;
          try {        FileInputStream fis = new FileInputStream(oldF);
            String path = newF.getAbsolutePath();        nf = new File(path);
            if (!nf.exists()) {
              File dir = new File(path.substring(0, path.lastIndexOf('\\')));
              if (!dir.exists())
                dir.mkdirs();
            }        nf.createNewFile();        FileOutputStream fos = new FileOutputStream(nf);        bytes = new byte[fis.available()];
            fis.read(bytes);
            fos.write(bytes);
            fis.close();
            fos.close();    
          }
          catch (Exception e) {
            System.out.print("error:"+e);
          }
        }  }执行到        bytes = new byte[fis.available()]; 时报
    java.lang.OutOfMemoryErrorException in thread "main"
      

  2.   

    bytes = new byte[fis.available()]; 
    可以定个死数,不要太大。
    读后判断读到多少就写多少。
      

  3.   

    谢谢  rower203(华仔)  按照你说的写已经不再报错
    bytes = new byte[1024];
            int length = (int)oldF.length();
            int count = length/1024;
    System.out.println(count+"****************");
            for(int i=0;i<count;i++){
              fis.read(bytes);
              fos.write(bytes);
            }
            int s = length%1024;
            if(s!=0){
              bytes = new byte[s];
              fis.read(bytes);
              fos.write(bytes);        }
    但是这样写要循环执行几十万次,有没有好一点的方法呢??
      

  4.   

    谁用过 java nio 能不能给解释一下下面这一句是什么意思?
    MappedByteBuffer out =
                new RandomAccessFile("C:/aa/test.txt", "rw").getChannel().map(FileChannel.MapMode.READ_WRITE, 0, length);MappedByteBuffer  与   FileInputStream/FileOutputStream 相比 那个效率更高些??
      

  5.   

    你这个写法不出错才怪
    我给你写一个....
    ....
    InputStream in=new FileInputStream("fromfilename");
    OutputStream out=new FileOutputStream("tofilename");
    byte[] buff=new byte[2048]//经过我测试,2048性能相对不错
    int len;
    while((len=in.read(buff))>-1){
       out.write(buff,0,len);
    }
    in.flush();
    in.close();
    out.close();
      

  6.   

    这个写法错了,这个也是大多数人错的地方,各位切记,
    fis.read(bytes);
    fos.write(bytes);
    因为不一定会把数组读满,特别是在对网络读取的时候更是如此,应该是读多少,写多少
      

  7.   

    详细请看这两个方法的API,不要不仔细而带来隐含的BUG,未来找错误都不知道出在哪儿