怎样用流将一个文件夹中的图片复制到另一个文件夹中

解决方案 »

  1.   

    public  static   void     Copy(String     oldPath,     String     newPath)    
       {    
              try     {    
                      int     bytesum     =     0;    
                      int     byteread     =     0;    
                      File     oldfile     =     new     File(oldPath);    
                      if     (oldfile.exists())     {      
                              InputStream     inStream     =     new     FileInputStream(oldPath);     
                              FileOutputStream     fs     =     new     FileOutputStream(newPath);    
                              byte[]     buffer     =     new     byte[1444];    
                              int     length;    
                              while     (     (byteread     =     inStream.read(buffer))     !=     -1)     {    
                                      bytesum     +=     byteread;        
                                      System.out.println(bytesum);    
                                      fs.write(buffer,     0,     byteread);    
                              }    
                              inStream.close();    
                      }    
              }    
              catch     (Exception     e)     {    
                      System.out.println( "error  ");    
                      e.printStackTrace();    
              }    
        }     
      

  2.   

     byte[] buffer = new byte[1444];   你怎么知道是1444?还是用file.size()吧。
      

  3.   


    /**
    * 复制单个文件

    * @param oldPath
    *            String 原文件路径 如:c:/fqf.txt
    * @param newPath
    *            String 复制后路径 如:f:/fqf.txt
    * @return boolean
    * @throws Exception 
    */
    public void copyFile(String oldPath, String newPath) throws IOException {
    try {
    String newPathFolder = newPath.substring(0, newPath.lastIndexOf(File.separator));
    File folder = new File(newPathFolder);
    if (!folder.exists()){
    folder.mkdirs();
    }
                    int bytesum = 0;
            int byteread = 0;
            File oldfile = new File(oldPath);
            if (oldfile.exists()) { // 文件存在时
             InputStream inStream = new FileInputStream(oldPath); // 读入原文件
             FileOutputStream fs = new FileOutputStream(newPath);
            byte[] buffer = new byte[1444];
            while ((byteread = inStream.read(buffer)) != -1) {
            bytesum += byteread; // 字节数 文件大小
            fs.write(buffer, 0, byteread);
            }
            inStream.close();
            fs.close();
             }
    } catch (IOException e) {
    throw new IOException(e);
    }}
      

  4.   

    // FastCopyFileimport java.io.*;
    import java.nio.*;
    import java.nio.channels.*;public class FastCopyFile
    {
      static public void main( String args[] ) throws Exception {
        if (args.length<2) {
          System.err.println( "Usage: java FastCopyFile infile outfile" );
          System.exit( 1 );
        }    String infile = args[0];
        String outfile = args[1];    FileInputStream fin = new FileInputStream( infile );
        FileOutputStream fout = new FileOutputStream( outfile );    FileChannel fcin = fin.getChannel();
        FileChannel fcout = fout.getChannel();    ByteBuffer buffer = ByteBuffer.allocateDirect( 1024 );    while (true) {
          buffer.clear();      int r = fcin.read( buffer );      if (r==-1) {
            break;
          }      buffer.flip();      fcout.write( buffer );
        }
      }
    }
    来个nio的。
      

  5.   

    恩,明白了。我一般都直接读到一个byte[]里,没考虑过内存。学习了。
      

  6.   

    String newPathFolder = newPath.substring(0, newPath.lastIndexOf(File.separator));报错啊。