文件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,我認爲它已正確映射 –