我正在嘗試創建一個小應用程序,它將一些.jar文件複製到最新的jre中。Java,將文件複製到jre
無論如何找出這條路是哪條路? 我看了File類,我發現了幾個方法會創建一個空文件,但我沒有找到任何可以幫助我將文件複製到給定路徑的方法。
我錯過任何重要的課程嗎?
感謝
我正在嘗試創建一個小應用程序,它將一些.jar文件複製到最新的jre中。Java,將文件複製到jre
無論如何找出這條路是哪條路? 我看了File類,我發現了幾個方法會創建一個空文件,但我沒有找到任何可以幫助我將文件複製到給定路徑的方法。
我錯過任何重要的課程嗎?
感謝
首先沒有用於複製一個文件,直到Java 7的一個輔助方法(尚未公佈)。其次,嘗試複製到JRE目錄是不可取的,因爲您可能沒有足夠的權限。爲了找到JRE的位置,應該使用System.getProperty(「java.home」) 複製:
byte[] buffer = new byte[16384];
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dst);
while (true) {
int n = in.read(buffer);
if (n == -1)
break;
out.write(buffer, 0, n);
}
in.close();
out.close();
要複製的文件,你可以使用java.nio.channels.FileChannel中的類從NIO庫。 包。
例如:
// Create channel for the source
FileChannel srcChannel = new FileInputStream("srcFileLocation").getChannel();
// Create channel for the destination
FileChannel dstChannel = new FileOutputStream("dstFileLocation").getChannel();
// Copy file contents from source to destination
dstChannel.transferFrom(srcChannel, 0, srcChannel.size());
// Close the channels
srcChannel.close();
dstChannel.close();
你有沒有考慮在一個腳本這樣做呢? – 2010-01-19 21:08:02