文件transfering我有方法问题在Java
protected String browsesFile() {
String url = null;
FileDialog dialog = new FileDialog(PlatformUI.getWorkbench()
.getActiveWorkbenchWindow().getShell(), SWT.NULL);
// set the filter options
dialog.setFilterExtensions(new String[] { "*.jpg", "*.jpeg", "*.png" });
String path = dialog.open();
if (path != null) {
File file = new File(path);
if (file.isFile())
url = file.toString();
else
url = file.list().toString();
}
return url;
}// end of method browseFile()
它将使该文件 的url
。我把它称为text.setText(browsesFile());
。这将带来我选择的图像的网址。我希望将该图像转换成G:\myImage
。为了转移我做了以下。
public static void copyFile(File sourceFile, File destFile) throws IOException {
if(!destFile.exists()) {
destFile.createNewFile();
}
FileChannel source = null;
FileChannel destination = null;
try {
source = new FileInputStream(sourceFile).getChannel();
destination = new FileOutputStream(destFile).getChannel();
destination.transferFrom(source, 0, source.size());
}
finally {
if(source != null) {
source.close();
}
if(destination != null) {
destination.close();
}
}}
我送使用的功能
File source = new File(text.getText());
String url ="G:\\myImage";
File dest = new File(url);
try {
copyFile(source, dest);
} catch (IOException e1) {
e1.printStackTrace();
}
}
我得到的错误消息
java.io.FileNotFoundException: G:\myImage (Access is denied)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
可能是什么可能reasong这个?我正在使用Windows 7
FYI通过将源文件名到目标解决此:'transferFrom'传输_up to_'source.size()'字节。根据[文档](http://download.oracle.com/javase/1.4.2/docs/api/java/nio/channels/FileChannel.html#transferFrom%28java.nio.channels.ReadableByteChannel,%20long, %20long%29),它返回“实际传输的字节数,可能为零”。您可能希望精确验证实际传输了多少字节以避免细微的错误。 –
错误消息看起来很明显。检查G:驱动器是否在您的计算机上正确映射,并且您的Java程序运行时具有足够的权限来写入它。 – Perception
@perception G:\是我的本地驱动器。我在那里创建了文件夹myImage,我认为它已正确映射 –