在Java中,可以使用FileInputStream和FileOutputStream来实现文件复制功能。以下是一个简单的示例代码:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileCopy {
public static void main(String[] args) {
String sourceFile = "source.txt";
String destFile = "destination.txt";
try {
File source = new File(sourceFile);
File dest = new File(destFile);
FileInputStream fis = new FileInputStream(source);
FileOutputStream fos = new FileOutputStream(dest);
byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer)) > 0) {
fos.write(buffer, 0, length);
}
fis.close();
fos.close();
System.out.println("File copied successfully.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
在这个示例中,首先指定源文件和目标文件的路径,然后创建FileInputStream和FileOutputStream来读取源文件和写入目标文件。接着,创建一个缓冲区数组,通过循环读取源文件内容并将内容写入目标文件。最后关闭输入流和输出流,并输出文件复制成功的信息。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,请发送邮件至 55@qq.com 举报,一经查实,本站将立刻删除。转转请注明出处:https://www.szhjjp.com/n/944632.html