我有一個文件example.tar.gz,我需要複製到另一個名稱不同的位置 example_test.tar.gz。我試着用在不同位置複製和重命名文件
private void copyFile(File srcFile, File destFile) throws IOException
{
InputStream oInStream = new FileInputStream(srcFile);
OutputStream oOutStream = new FileOutputStream(destFile);
// Transfer bytes from in to out
byte[] oBytes = new byte[1024];
int nLength;
BufferedInputStream oBuffInputStream =
new BufferedInputStream(oInStream);
while ((nLength = oBuffInputStream.read(oBytes)) > 0)
{
oOutStream.write(oBytes, 0, nLength);
}
oInStream.close();
oOutStream.close();
}
}
其中
String from_path=new File("example.tar.gz");
File source=new File(from_path);
File destination=new File("/temp/example_test.tar.gz");
if(!destination.exists())
destination.createNewFile();
然後
copyFile(source, destination);
,但它不工作。路徑沒問題。它打印存在的文件。任何人都可以幫忙嗎?
嘗試'的flush()'你流之前'接近()'荷蘭國際集團將其改爲的transferTo。 – 2011-03-22 07:45:01
在你的文章中更正這段代碼:'''''from_path = new File(「example.tar.gz」);' – 2011-03-22 07:52:10
@Mohamed,關閉之前不需要刷新 – bestsss 2011-03-22 07:53:49