「Java复制文库」java 复制
今天给各位分享Java复制文库的知识,其中也会对java 复制进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
使用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怎么实现文件拷贝
工具/原料
一台配置了java环境的电脑
一款适合自己的开发集成环境,这里用的是eclipse Kepler
文件拷贝DEMO
1.首先,理清思路,然后我们再动手操作。
拷贝,有源文件,和目的文件。
如果原文件不存在,提示,报错。
如果目的文件不存在,创建空文件并被覆盖。
如果目的地址,也即目的路径不存在,创建路径。
拷贝,输入流,输出流,关闭流。
拷贝前输出文件大小,计算拷贝大小,比较并核实。输出。
2.首先呢,先判断传参是否完整。
如果不够两个参数,或者多于两个参数,提示错误。
如果目标文件不存在,创建 空文件继续复制。
3.在开始前,输出被拷贝的源文件的大小。
4.获得文件名称,即短名。也即路径下的文件全名(包括文件扩展名)。
5.拷贝的关键,这里用的简单的缓冲流。从源文件到目的文件。
number of bytes copied 即是对拷贝长度的累计,直到拷贝完成,输出。
6.将步骤二中的判断并拷贝文件的代码写在一个main函数中,
执行拷贝,拷贝完成。结果拷贝大小和源文件大小一致,成功。
7.在执行前,记得输入参数。
如果是使用命令提示符,执行 javac CopyFile.java 之后,
执行 java CopyFile [源文件长名] [目的文件长名]
如果是使用的eclipse,在运行前设置一下运行参数,完成后点击运行,如下图。
P.S. 这里面的所谓“长名”是指完整绝对路径+文件名+文件类型扩展名
这里的源文件及目的文件的名称分别为:
E:/IP_Data.rar 和 D:/testFiles/IP_Data.rar
END
Java 将一个文件复制到另一处
test.copy("G:\\G盘寄存资料\\我的文档1\\音乐课堂.doc","G:\\G盘寄存资料");
请注意上面的有个文件夹名字叫“G盘寄存资料”,你复制的文件后的新文件名也叫“G盘寄存资料”,这样名字重复了,所以就出错了。
可以把程序改成这样的话就行了:
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class FileCopy {
public void copy(String src, String dest){//**********
InputStream is=null;
OutputStream os=null;
char ch[]=src.toCharArray();
//************新添加的代码**********
int pos=0;
for(int i=ch.length-1;i=0;i--)
{
if(ch[i]=='\\')
{
if(ipos)
pos=i;
}
}
String temp=src.substring(pos);
dest=dest+temp;
System.out.println("dest="+dest);
//****************************************
try {
is=new BufferedInputStream(new FileInputStream(src));
os=new BufferedOutputStream(new FileOutputStream(dest));
byte[] b=new byte[256];
int len=0;
String str=null;
StringBuilder sb=new StringBuilder();
try {
while((len=is.read(b))!=-1){
os.write(b,0,len);
}
os.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(is!=null){
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally{
if(os!=null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
FileCopy test=new FileCopy();
test.copy("G:\\G盘寄存资料\\我的文档1\\hello.txt","G:\\G盘寄存资料");//++++++++++++++++++++++
}
}
java如何复制文件(包括图片等其他格式的文件)
可以通过带缓冲的字节输入输出流来完成。下面我写的一个小例子
import java.io.*;
public class FileTest {
public static void main(String[] args) throws FileNotFoundException {
int i;
String file="e:/电影/123/2012.rmvb";//要复制的文件路径和名称
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));//创建一个带缓冲字节输入流读取文件
String fileName="2012.rmvb";//文件名
BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("c:/my/"+fileName));//复制目的的路径
try {
while ((i = bis.read()) != -1) {
bos.write(i);
}
} catch (IOException ex) {
ex.printStackTrace();
}finally{
try {
bis.close();
bos.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
运行正确,求采纳
java复制文件
in是一个文件输入流
data是一个byte数组
in.avaliable()是取得输入流的字节数
data = new byte[in.available()];是给这个数组根据输入字节数分配空间
in.read(data);是把输入内容读到数组里去
Java复制文库的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java 复制、Java复制文库的信息别忘了在本站进行查找喔。
发布于:2022-11-21,除非注明,否则均为
原创文章,转载请注明出处。