java文件复制的代码,java文件复制的代码有哪些

JAVA高手请进!求一个JAVA程序:将一个文件中的内容复制到另一个文件中。

最简单的io流问题,不用什么高手,

创新互联公司技术团队10多年来致力于为客户提供成都网站制作、网站设计、品牌网站制作成都全网营销、搜索引擎SEO优化等服务。经过多年发展,公司拥有经验丰富的技术团队,先后服务、推广了成百上千家网站,包括各类中小企业、企事单位、高校等机构单位。

我给你写个方法,参数是2个字符串,第一个写原文件的全路径,第二个写目标文件的全路进。 你试试吧

public void copy(String fromFilePath, String toFilePath) {

try {

FileInputStream fis = new FileInputStream(fromFilePath);

FileOutputStream fos = new FileOutputStream(toFilePath);

byte[] b = new byte[100];

try {

while (fis.read(b) != (-1)) {

fos.write(b);

}

if (fis != null) {

fis.close();

fis = null;

}

if (fos != null) {

fos.flush();

fos.close();

fos = null;

}

} catch (IOException e) {

System.out.println("io异常");

}

} catch (FileNotFoundException e) {

System.out.println("源文件不存在");

}

public static void main(String[] args) {

//自己把路径补齐,别忘了!!!!!!!!!!!!!!!!

String fromFilePath=" "; // 源文件的全路径。 比方"d://myphoto//nihao.mp3"

String toFilePath=" "; //目标文件的全路劲。 如果不存在会自动建立,如存在则在文件尾继续添加

new CopyTest().copy(fromFilePath, toFilePath);

}

}

怎样用java程序实现文件拷贝

通过输入输出流解决此问题,具体的可以查看JDK的API,实在不会的话,百度一下应该都有一堆这方面的代码。

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代码拷贝文件夹 注:复制文件夹

Java代码复制文件夹时,则需要利用Flie类在目标文件夹中创建相应的目录,并且使用递归方法,代码如下:

import java.io.*;  

/** 

* 复制文件夹或文件夹 

*/  

public class CopyDirectory {  

// 源文件夹   

static String url1 = "F:/photos";  

// 目标文件夹   

static String url2 = "D:/tempPhotos";  

public static void main(String args[]) throws IOException {  

// 创建目标文件夹   

(new File(url2)).mkdirs();  

// 获取源文件夹当前下的文件或目录   

File[] file = (new File(url1)).listFiles();  

for (int i = 0; i  file.length; i++) {  

if (file[i].isFile()) {  

// 复制文件   

copyFile(file[i],new File(url2+file[i].getName()));  

}  

if (file[i].isDirectory()) {  

// 复制目录   

String sourceDir=url1+File.separator+file[i].getName();  

String targetDir=url2+File.separator+file[i].getName();  

copyDirectiory(sourceDir, targetDir);  

}  

}  

}  

// 复制文件   

public static void copyFile(File sourceFile,File targetFile)   

throws IOException{  

// 新建文件输入流并对它进行缓冲   

FileInputStream input = new FileInputStream(sourceFile);  

BufferedInputStream inBuff=new BufferedInputStream(input);  

// 新建文件输出流并对它进行缓冲   

FileOutputStream output = new FileOutputStream(targetFile);  

BufferedOutputStream outBuff=new BufferedOutputStream(output);  

// 缓冲数组   

byte[] b = new byte[1024 * 5];  

int len;  

while ((len =inBuff.read(b)) != -1) {  

outBuff.write(b, 0, len);  

}  

// 刷新此缓冲的输出流   

outBuff.flush();  

//关闭流   

inBuff.close();  

outBuff.close();  

output.close();  

input.close();  

}  

// 复制文件夹   

public static void copyDirectiory(String sourceDir, String targetDir)  

throws IOException {  

// 新建目标目录   

(new File(targetDir)).mkdirs();  

// 获取源文件夹当前下的文件或目录   

File[] file = (new File(sourceDir)).listFiles();  

for (int i = 0; i  file.length; i++) {  

if (file[i].isFile()) {  

// 源文件   

File sourceFile=file[i];  

// 目标文件   

File targetFile=new File(new File(targetDir).getAbsolutePath()+File.separator+file[i].getName());  

copyFile(sourceFile,targetFile);  

}  

if (file[i].isDirectory()) {  

// 准备复制的源文件夹   

String dir1=sourceDir + "/" + file[i].getName();  

// 准备复制的目标文件夹   

String dir2=targetDir + "/"+ file[i].getName();  

copyDirectiory(dir1, dir2);  

}  

}  

}  

}

java文件复制粘贴

复制粘贴实际上是文件的流读取和写入可以通过如下方法实现:

读写是两个不同的分支,通常都是分开单独使用的。

可以通过BufferedReader 流的形式进行流缓存,之后通过readLine方法获取到缓存的内容。

BufferedReader bre = null;

try {

String file = "D:/test/test.txt";

bre = new BufferedReader(new FileReader(file));//此时获取到的bre就是整个文件的缓存流

while ((str = bre.readLine())!= null) // 判断最后一行不存在,为空结束循环

{

System.out.println(str);//原样输出读到的内容

};

备注: 流用完之后必须close掉,如上面的就应该是:bre.close(),否则bre流会一直存在,直到程序运行结束。

可以通过“FileOutputStream”创建文件实例,之后过“OutputStreamWriter”流的形式进行存储,举例:

OutputStreamWriter pw = null;//定义一个流

pw = new OutputStreamWriter(new FileOutputStream(“D:/test.txt”),"GBK");//确认流的输出文件和编码格式,此过程创建了“test.txt”实例

pw.write("我是要写入到记事本文件的内容");//将要写入文件的内容,可以多次write

pw.close();//关闭流

备注:文件流用完之后必须及时通过close方法关闭,否则会一直处于打开状态,直至程序停止,增加系统负担。

使用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文件复制的代码有哪些
URL分享:http://lszwz.com/article/hdichj.html

其他资讯

售后响应及时

7×24小时客服热线

数据备份

更安全、更高效、更稳定

价格公道精准

项目经理精准报价不弄虚作假

合作无风险

重合同讲信誉,无效全额退款