「java实现复制」java复制粘贴
本篇文章给大家谈谈java实现复制,以及java复制粘贴对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
怎样用java程序实现文件拷贝
通过输入输出流解决此问题,具体的可以查看JDK的API,实在不会的话,百度一下应该都有一堆这方面的代码。
Java 如何复制对象
可以使用clone来实现,clone用于为引用类型的复制
1.使用clone方法的类必须先实现Cloneable接口,不然clone方法会直接返回CloneNotSupportedException不支持克隆的异常
2、实现Cloneable接口的类应该使用公共方法重写 Object.clone(它是受保护的)。某个对象实现了此接口就克隆它是不可能的。即使 clone 方法是反射性调用的,也无法保证它将获得成功。
3、在Java.lang.Object类中克隆方法是这么定义的:
protected Object clone()
throws CloneNotSupportedException
创建并返回此对象的一个副本。表明是一个受保护的方法,同一个包中可见。
使用Java语言如何实现快速文件复制
使用Java语言如何实现快速文件复制:
代码:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
public class Test {
public static void main(String[] args){
long start = System.currentTimeMillis();
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
FileChannel inFileChannel = null;
FileChannel outFileChannel = null;
try {
fileInputStream = new FileInputStream(new File("C:\\from\\不是闹着玩的.flv"));
fileOutputStream = new FileOutputStream(new File("C:\\to\\不是闹着玩的.flv"));
inFileChannel = fileInputStream.getChannel();
outFileChannel = fileOutputStream.getChannel();
inFileChannel.transferTo(0, inFileChannel.size(), outFileChannel);//连接两个通道,从in通道读取数据写入out通道。
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(fileInputStream != null){
fileInputStream.close();
}
if(inFileChannel != null){
inFileChannel.close();
}
if(fileOutputStream != null){
fileOutputStream.close();
}
if(outFileChannel != null){
outFileChannel.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
long end = System.currentTimeMillis();
System.out.println("视频文件从“from”文件夹复制到“to”文件需要" + (end - start) + "毫秒。");
}
}
java如何实现文件的复制粘贴?
打开D盘,点编辑,全部选定,右键点变篮的文件选复制,打开E盘右键点空白处选粘贴。
java实现复制的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java复制粘贴、java实现复制的信息别忘了在本站进行查找喔。
发布于:2022-12-22,除非注明,否则均为
原创文章,转载请注明出处。