「java拷贝文件夹」Java复制文件夹

博主:adminadmin 2023-01-09 08:03:06 996

本篇文章给大家谈谈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复制文件夹!

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

public class CopyFile {

public boolean copy(String file1,String file2) {

File in=new File(file1);

File out=new File(file2);

if(!in.exists()){

System.out.println(in.getAbsolutePath()+"源文件路径错误!!!");

return false;

}

else {

System.out.println("源文件路径"+in.getAbsolutePath());

System.out.println("目标路径"+out.getAbsolutePath());

}

if(!out.exists())

out.mkdirs();

File[] file=in.listFiles();

FileInputStream fin=null;

FileOutputStream fout=null;

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

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

try {

fin=new FileInputStream(file[i]);

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

System.out.println("in.name="+file[i].getName());

try {

fout=new FileOutputStream(new File(file2+"/"+file[i].getName()));

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

System.out.println(file2);

int c;

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

try {

while((c=fin.read(b))!=-1){

fout.write(b, 0, c);

System.out.println("复制文件中!");

}

------------------------------注意

fin.close();

fout.flush();

fout.close();

--------------------------------

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

-------------------------------注释掉

// return true;

}

else copy(file1+"/"+file[i].getName(),file2+"/"+file[i].getName());

}

return false;

}

public static void main(String[] args) {

CopyFile copyFile = new CopyFile();

copyFile.copy("E:\\study\\opngl", "E:\\opengl");

}

}

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如何复制拷贝一个文件到另一个文件夹?如:a文件夹中的.data文件拷贝到b文件夹。

你可以个java inputStrem流和outputStream流来实现这个功能。

import java.io.*;

public class FileStreamDemo {

public static void main(String[] args) {

try {

// 来源文件

FileInputStream in = new FileInputStream("D:/b.txt");

// 目的文件

FileOutputStream out = new FileOutputStream("C:/a.txt");

byte[] bytearray = new byte[1024];

do {

in.read(bytearray, 0, 1024);

out.write(bytearray);

} while (in.available() 0);

in.close();

out.close();

} catch (ArrayIndexOutOfBoundsException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

}

关于java拷贝文件夹和Java复制文件夹的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。