[英]Java, copying file to jre
我正在嘗試創建一個小型應用程序,它將一些.jar文件復制到最新的jre中。
無論如何,有沒有找出這條路? 我正在查看File類,並且找到了幾種可以創建一個空文件的方法,但是沒有找到任何有助於將文件復制到給定路徑的方法。
我錯過任何重要的課程嗎?
謝謝
要復制文件,可以使用nio庫中的java.nio.channels.FileChannel類。 包。
例如:
// 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();
首先,直到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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.