import java.io.*;
public class FileCopy
{
FileInputStream FIS;
FileOutputStream FOS;
public boolean copyFile(String src, String des)
{
try
{
//取得輸入出流
FIS = new FileInputStream(src);
//取得輸出流
FOS = new FileOutputStream(des);
//生成一個綬沖區
byte[] bt = new byte[1024];
int readNum = 0;
while ((readNum = FIS.read(bt)) != -1)
{
//將輸入流寫到輸出流
FOS.write(bt, 0, bt.length);
}
FIS.close();
FOS.close();
return true;
}
catch (Exception e)
{
try
{
FIS.close();
FOS.close();
}
catch (IOException f)
{
e.printStackTrace();
}
return false;
}
finally
{
}
}
//調用
public static void main(String[] arg)
{
FileCopy fc=new FileCopy();
if(fc.copyFile("123.rar","456.rar"))
{
System.out.println(" File Copy Successfully!");
}
}
}
public class FileCopy
{
FileInputStream FIS;
FileOutputStream FOS;
public boolean copyFile(String src, String des)
{
try
{
//取得輸入出流
FIS = new FileInputStream(src);
//取得輸出流
FOS = new FileOutputStream(des);
//生成一個綬沖區
byte[] bt = new byte[1024];
int readNum = 0;
while ((readNum = FIS.read(bt)) != -1)
{
//將輸入流寫到輸出流
FOS.write(bt, 0, bt.length);
}
FIS.close();
FOS.close();
return true;
}
catch (Exception e)
{
try
{
FIS.close();
FOS.close();
}
catch (IOException f)
{
e.printStackTrace();
}
return false;
}
finally
{
}
}
//調用
public static void main(String[] arg)
{
FileCopy fc=new FileCopy();
if(fc.copyFile("123.rar","456.rar"))
{
System.out.println(" File Copy Successfully!");
}
}
}
本文提供了一个使用Java实现文件复制的示例代码。通过创建输入流和输出流,并利用缓冲区进行读写操作来完成文件从源路径到目标路径的复制。
550

被折叠的 条评论
为什么被折叠?



