3

我的任务是从服务器复制数据。我正在使用BufferedInputStream和输出流来复制数据,并且我正在逐字节进行。即使它正在运行,但复制数据需要很长时间,因为其中一些数据是 100 的 MB,所以它肯定行不通。谁能建议我逐字节复制的任何替代方案,以便我的代码可以复制几百 MB 的文件。缓冲区为 2048。

这是我的代码的样子:

static void copyFiles(SmbFile[] files, String parent) throws IOException {

  SmbFileInputStream input = null;
  FileOutputStream output = null;
  BufferedInputStream buf_input = null;
  try {
    for (SmbFile f : files) {
      System.out.println("Working on files :" + f.getName());
      if (f.isDirectory()) {

        File folderToBeCreated = new File(parent+f.getName());
        if (!folderToBeCreated.exists()) {
          folderToBeCreated.mkdir();
          System.out.println("Folder name " + parent
                + f.getName() + "has been created");
        } else {
          System.out.println("exists");

        }

        copyFiles(f.listFiles(), parent +  f.getName());
      } else {

        input = (SmbFileInputStream) f.getInputStream();

        buf_input = new BufferedInputStream(input, BUFFER);

        File t = new File(parent + f.getName());
        if (!t.exists()) {
          t.createNewFile();
        }
        output = new FileOutputStream(t);

        int c;

        int count;
        byte data[] = new byte[BUFFER];

        while ((count = buf_input.read(data, 0, BUFFER)) != -1) {
          output.write(data, 0, count);
        }
      }
    }
  } catch (IOException e) {
    e.printStackTrace();

  } finally {
    if (input != null) {
      input.close();
    }
    if (output != null) {
      output.close();
    }
  }
}
4

4 回答 4

16

这是一篇精彩文章的链接,该文章解释了如何使用nio通道来制作流的副本。它引入了一个帮助方法ChannelTools.fastChannelCopy,让您可以像这样复制流:

final InputStream input = new FileInputStream(inputFile);
final OutputStream output = new FileOutputStream(outputFile);
final ReadableByteChannel inputChannel = Channels.newChannel(input);
final WriteableByteChannel outputChannel = Channels.newChannel(output);
ChannelTools.fastChannelCopy(inputChannel, outputChannel);
inputChannel.close();
outputChannel.close()
于 2012-12-06T16:39:43.903 回答
4

好吧,因为您使用的是 a BufferedInputStream,所以您不是逐字节读取,而是缓冲区的大小。您可以尝试增加缓冲区大小。

于 2012-12-06T16:39:18.113 回答
1

逐字节读/写肯定会很慢,即使实际读/写是由缓冲区大小的块完成的。加速它的一种方法是按块读取/写入。看看read(byte[] b, int off, int len)方法BufferedInputStream。然而,它可能不会给你足够的改进。

更好的是使用nio包(新IO)使用nio通道复制数据。查看nio 文档以获取更多信息。

于 2012-12-06T16:40:23.157 回答
1

我建议使用FileUtilsfrom org.apache.commons.io。它有足够的实用方法来执行文件操作。

org.apache.commons.io.FileUtils API 在这里

于 2012-12-06T16:50:36.777 回答