我应该关闭FileChannel吗? [英] Should I close the FileChannel?

查看:250
本文介绍了我应该关闭FileChannel吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我今天遇到了一个实用程序类的问题。它是文件的帮助程序,包含一些静态文件复制例程。以下是与测试方法一起提取的相关方法。

I came across an issue with one of our utility classes today. It is a helper for files and contains some static file copy routines. Below are the relevant methods extracted along with a test method.

问题是有时setLastModified调用失败,返回false。

The problem is that sometimes the setLastModified call fails, returning false.

在我的电脑上(Windows 7,最新的Java)我有时会收到setLastModified failed消息(1000次中约25次)。

On my PC (Windows 7, latest Java) I sometimes get the "setLastModified failed" message (About 25 times out of 1000).

我有通过删除FileChannel.close调用来解决问题,但我更倾向于理解为什么会发生这种情况,即使这是正确的解决方案。

I have worked around the problem right now by removing the FileChannel.close calls but I would much prefer to understand why this is happening, even if that is the correct solution.

有没有人否则会遇到同样的问题?

Does anyone else get the same problem?

private void testCopy() throws FileNotFoundException, IOException {
  File src = new File("C:\\Public\\Test-Src.txt");
  File dst = new File("C:\\Public\\Test-Dst.txt");

  for (int i = 0; i < 1000; i++) {
    copyFile(src, dst);
  }
}

public static void copyFile(final File from, final File to) throws FileNotFoundException, IOException {
  final String tmpName = to.getAbsolutePath() + ".tmp";
  // Copy to a .tmp file.
  final File tmp = new File(tmpName);
  // Do the transfer.
  transfer(from, tmp);
  // Preserve time.
  if (!tmp.setLastModified(from.lastModified())) {
    System.err.println("setLastModified failed!");
  }
  // In case there's one there already.
  to.delete();
  // Rename it in.
  tmp.renameTo(to);
}

public static void transfer(final File from, final File to) throws IOException {
  FileInputStream in = null;
  FileOutputStream out = null;
  try {
    in = new FileInputStream(from);
    out = new FileOutputStream(to);
    transfer(in, out);
  } finally {
    if (null != in) {
      in.close();
    }
    if (null != out) {
      out.close();
    }
  }
}

public static void transfer(final FileInputStream from, final FileOutputStream to) throws IOException {
  FileChannel srcChannel = null;
  FileChannel dstChannel = null;
  //try {
    srcChannel = from.getChannel();
    dstChannel = to.getChannel();
    srcChannel.transferTo(0, srcChannel.size(), dstChannel);
  //} finally {
  //  if (null != dstChannel) {
  //    dstChannel.close();
  //  }
  //  if (null != srcChannel) {
  //    srcChannel.close();
  //  }
  }
}

编辑:我已将代码更改为仅关闭 Streams s而不是 FileChannel ,因为研究表明关闭 FileChannel 也会关闭 Stream

I have changed the code to only close the Streamss and not the FileChannels because research suggests closing the FileChannel also closes the Stream.

推荐答案

经过对包含java库源的各个站点的一些研究后,它看起来非常像 FileChannel.close 最终调用其父对象的FileInputStream.close FileOutputStream.close

After some research amongst the various sites that hold java library sources it looks very much like FileChannel.close ultimately calls the FileInputStream.close or FileOutputStream.close of its parent object.

这表明你应关闭FileChannel或Stream,但不能同时关闭

鉴于此,我正在更改我的原始帖子以反映一种正确的方法,即关闭而不是频道 s。

In view of this I am changing my original post to reflect one correct method, i.e. close the Streams not the Channels.

这篇关于我应该关闭FileChannel吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆