「java拷贝一个文件」java复制一个文件

博主:adminadmin 2022-12-08 21:00:12 53

今天给各位分享java拷贝一个文件的知识,其中也会对java复制一个文件进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

java如何拷贝一个文件夹内的多个指定的文件到另外一个指定的文件夹下?

你好:

请看代码:

/**

* 把一个文件夹里的所有文件包括文件夹 一并原样拷贝到另一个目录中;

*@author shuishui

*/  

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 CopyDir001 {   

  

    public static File dirFrom;   

    public static File dirTo;   

  

    // 目标路径创建文件夹   

    public void listFileInDir(File file) {   

         File[] files = file.listFiles();   

        for (File f : files) {   

             String tempfrom = f.getAbsolutePath();   

             String tempto = tempfrom.replace(dirFrom.getAbsolutePath(),   

                     dirTo.getAbsolutePath()); // 后面的路径 替换前面的路径名   

            if (f.isDirectory()) {   

                 File tempFile = new File(tempto);   

                 tempFile.mkdirs();   

                 listFileInDir(f);   

             } else {   

                 System.out.println("源文件:" + f.getAbsolutePath());   

                //   

                int endindex = tempto.lastIndexOf("\\");// 找到"/"所在的位置   

                 String mkdirPath = tempto.substring(0, endindex);   

                 File tempFile = new File(mkdirPath);   

                 tempFile.mkdirs();// 创建立文件夹   

                 System.out.println("目标点:" + tempto);   

                 copy(tempfrom, tempto);   

             }   

         }   

     }   

    /**

      * 封装好的文件拷贝方法

      */  

    public void copy(String from, String to) {   

        try {   

             InputStream in = new FileInputStream(from);   

             OutputStream out = new FileOutputStream(to);   

  

            byte[] buff = new byte[1024];   

            int len = 0;   

            while ((len = in.read(buff)) != -1) {   

                 out.write(buff, 0, len);   

             }   

             in.close();   

             out.close();   

         } catch (FileNotFoundException e) {   

             e.printStackTrace();   

         } catch (IOException e) {   

             e.printStackTrace();   

         }   

     }   

  

    public static void main(String[] args) {   

         File fromfile = new File("e:\\shui\\test");// 源文件夹   

         File tofile = new File("e:\\Jying\\shui");// 目标   

  

         CopyDir001 copy = new CopyDir001();   

        // 设置来源去向   

         copy.dirFrom = fromfile;   

         copy.dirTo = tofile;   

         copy.listFileInDir(fromfile);   

  

     }   

}

java如何拷贝文件到另一个目录下

/**

*

复制单个文件

*

@param

oldPath

String

原文件路径

如:c:/fqf.txt

*

@param

newPath

String

复制后路径

如:f:/fqf.txt

*

@return

boolean

*/

public

void

copyFile(String

oldPath,

String

newPath)

{

try

{

int

bytesum

=

0;

int

byteread

=

0;

File

oldfile

=

new

File(oldPath);

if

(oldfile.exists())

{

//文件存在时

InputStream

inStream

=

new

FileInputStream(oldPath);

//读入原文件

FileOutputStream

fs

=

new

FileOutputStream(newPath);

byte[]

buffer

=

new

byte[1444];

int

length;

while

(

(byteread

=

inStream.read(buffer))

!=

-1)

{

bytesum

+=

byteread;

//字节数

文件大小

System.out.println(bytesum);

fs.write(buffer,

0,

byteread);

}

inStream.close();

}

}

catch

(Exception

e)

{

System.out.println("复制单个文件操作出错");

e.printStackTrace();

}

}

/**

*

复制整个文件夹内容

*

@param

oldPath

String

原文件路径

如:c:/fqf

*

@param

newPath

String

复制后路径

如:f:/fqf/ff

*

@return

boolean

*/

public

void

copyFolder(String

oldPath,

String

newPath)

{

try

{

(new

File(newPath)).mkdirs();

//如果文件夹不存在

则建立新文件夹

File

a=new

File(oldPath);

String[]

file=a.list();

File

temp=null;

for

(int

i

=

0;

i

file.length;

i++)

{

if(oldPath.endsWith(File.separator)){

temp=new

File(oldPath+file[i]);

}

else{

temp=new

File(oldPath+File.separator+file[i]);

}

if(temp.isFile()){

FileInputStream

input

=

new

FileInputStream(temp);

FileOutputStream

output

=

new

FileOutputStream(newPath

+

"/"

+

(temp.getName()).toString());

byte[]

b

=

new

byte[1024

*

5];

int

len;

while

(

(len

=

input.read(b))

!=

-1)

{

output.write(b,

0,

len);

}

output.flush();

output.close();

input.close();

}

if(temp.isDirectory()){//如果是子文件夹

copyFolder(oldPath+"/"+file[i],newPath+"/"+file[i]);

}

}

}

catch

(Exception

e)

{

System.out.println("复制整个文件夹内容操作出错");

e.printStackTrace();

}

}

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环境的电脑

一款适合自己的开发集成环境,这里用的是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程序实现文件拷贝

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

利用JAVA语言编写一个 名为copy的程序 实现文件的拷贝功能,应该怎样做?

import java.io.File;\x0d\x0aimport java.io.FileInputStream;\x0d\x0aimport java.io.FileNotFoundException;\x0d\x0aimport java.io.FileOutputStream;\x0d\x0aimport java.io.IOException;\x0d\x0apublic class Copy {\x0d\x0a/**\x0d\x0a* @param args\x0d\x0a*/\x0d\x0apublic static void main(String[] args) {\x0d\x0a// TODO Auto-generated method stub\x0d\x0aif(args.length!=2){\x0d\x0aSystem.out.print("没有输入正确数目的参数,程序退出!");\x0d\x0aSystem.exit(0);\x0d\x0a}\x0d\x0aFile fileS = new File("./"+args[0]);\x0d\x0aFile fileD = new File("./"+args[1]);\x0d\x0aif(fileD.exists())System.out.println("目标文件 "+args[1]+" 已存在!");\x0d\x0abyte[] temp = new byte[50];\x0d\x0aint totalSize = 0;\x0d\x0atry {\x0d\x0aFileInputStream fr = new FileInputStream(fileS);\x0d\x0aFileOutputStream fo = new FileOutputStream(fileD);\x0d\x0aint length = 0;\x0d\x0awhile((length = fr.read(temp, 0, temp.length)) != -1){\x0d\x0atotalSize += length;\x0d\x0afo.write(temp, 0, length);\x0d\x0a}\x0d\x0aSystem.out.println("文件 "+args[0]+" 有 "+totalSize+" 个字节");\x0d\x0aSystem.out.println("复制完成!");\x0d\x0a} catch (FileNotFoundException e) {\x0d\x0a// TODO Auto-generated catch block\x0d\x0ae.printStackTrace();\x0d\x0aSystem.out.println("源文件 "+args[0]+" 不存在!");\x0d\x0a} catch (IOException e) {\x0d\x0a// TODO Auto-generated catch block\x0d\x0ae.printStackTrace();\x0d\x0a}\x0d\x0a}\x0d\x0a}

java拷贝一个文件的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java复制一个文件、java拷贝一个文件的信息别忘了在本站进行查找喔。

The End

发布于:2022-12-08,除非注明,否则均为首码项目网原创文章,转载请注明出处。