java 文件拷贝问题_java操作文件——拷贝文件

/******************************************************************

*   最经闲暇之余,顺便总结了一点小东西,欢迎各位指点,主要操作文件的拷贝*

* 前几天朋友问我,关于文件操作的问题,我当时觉得请简单的,最后才发现里  *

*面的学问大着呢?自己只是略懂皮毛而已,顺便把问题说说吧:主要是关于文 *

*件的拷贝,即单层目录、多层目录、拷贝整个文件夹的文件等等。今天主要就 *

*说说单层目录、多层目录、拷贝整个文件夹的文件他们吧,希望对各位有帮助   *

* 对了,里面还有许多测试的代码没有删除,因为考虑到有些朋友可能需要测试 *

*还有就是文件操作的效率等问题,暂时还未考虑                                                            *

******************************************************************/

package com.java.FileOption;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

public class azazz {

public static void main(String[] args) {

//拷贝单个文件

coppyFile("e:/小红帽.txt", "d:/java/学习/文档/邢海明/你回家.txt");

//拷贝整个文件夹

//coppyAllFile("E:/新建文件夹", "d:/java/学习/文档/资料");

}

/**

* 拷贝文件

* @param sourcePath

* @param descPath

* @return

*/

public static Boolean coppyFile(String sourcePath, String descPath) {

Boolean flag = false;

FileInputStream fis = null;

FileOutputStream fos = null;

try {// 创建源文件路径【sourcePath】

//【从源文件中读取数据的数据流 fis】

fis = new FileInputStream(sourcePath);

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

System.out.println("系统找不到源文件,请确认以下路径是否正确: " + sourcePath);

e.printStackTrace();

return flag;

}

File file = new File(descPath);

if (!file.exists()) {// 如果目标路径不存在

createFiles(descPath);

}

try {// 创建目录文件【descPath】

//【写入目标文件中的数据流 fos】

fos = new FileOutputStream(file);

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

System.out.println("目标文件创建失败:" + descPath);

// e.printStackTrace();

return flag;

}

int c = 0;

byte buffer[] = new byte[1024];

try {

while ((c = fis.read(buffer)) != -1) {

fos.write(buffer);

}

flag = true;

System.out.println("恭喜你!文件拷贝成");

} catch (IOException e) {

// TODO Auto-generated catch block

System.out.println("文件写入失败:" + e.getLocalizedMessage());

// e.printStackTrace();

return flag;

}

// 关闭输入输出流

try {

fos.close();

fis.close();

} catch (IOException e) {

// TODO Auto-generated catch blocks

System.out.println("关闭输入输出流: " + e.getLocalizedMessage());

// e.printStackTrace();

return flag;

}

return flag;

}

/**

* 非根路径中创建文件,即多层文件夹是 前提是: path必须一个正确的路径

* 拷贝单个文件

* @param path文件

*/

public static Boolean createFiles(String path) {

Boolean flag = false;

File file = null;

if (!path.equals("") || path != null) {

file = new File(path);

if (!file.exists()) {

// 创建目录

File fileDir = new File(file.getParent());

fileDir.mkdirs();

System.out.println("上层文件夹: " + fileDir);

try {

flag = file.createNewFile();// 在已有文件路径上直接创建文件

System.out.println("文件名称:" + file);

} catch (IOException e) {

System.out.println("创建文件失败:" + e.getLocalizedMessage());

e.printStackTrace();

}

}else{

System.out.println("【"+path+"】:"+"该文件已经存在");

}

}

return flag;

}

/**

* 拷贝整个文件夹下的文件

* @param sourcePath

* @param descPath

* @return

*/

public static Boolean coppyAllFile(String sourcePath, String descPath) {

Boolean flag = false;

//创建源文件夹文件对象

File fileSource = new File(sourcePath);

if(fileSource.isDirectory()){//如果源文件家对象是一个目录

//创建目标

File file = new File(descPath);

if (!file.exists()) {// 如果目标路径不存在

createAllFiles(descPath);

}

//得到源文件的所有文件

File[] files = fileSource.listFiles();

for(File file2 : files) {

coppyAllFile(file2.toString(), descPath + "/" + file2.getName());

}

}else{

coppyFile(sourcePath, descPath);

}

return flag;

}

/**

* 非根路径中创建文件,即多层文件夹是 前提是: path必须一个正确的路径

* 拷贝整个文件

* @param path文件

*/

public static Boolean createAllFiles(String path) {

Boolean flag = false;

File file = null;

if (!path.equals("") || path != null) {

file = new File(path);

if (!file.exists()) {

// 创建目录

File fileDir = new File(file.getParent());

fileDir.mkdirs();

//System.out.println("上层文件夹: " + fileDir);

}else{

System.out.println("【"+path+"】:"+"该文件已经存在");

}

}

return flag;

}

/**

* 替换掉文件路径中的正斜杠

* @param path

* @return

*/

public String conversPath(String path) {

return path.replace("\\", "/");

}

public static void copyFile(String src, String dest) {

// 创建源文件输入流对象

FileInputStream in = null;

try {

in = new FileInputStream(src);

} catch (FileNotFoundException e) {

System.out.println("源文件不存在,请确认:" + e.getLocalizedMessage());

// TODO Auto-generated catch block

// e.printStackTrace();

// return;

}

File file = new File(dest);// 创建目标文件

if (!file.exists()) {// 如果该文件不存在,则必须创建该文件

try {

file.createNewFile();

System.out.println("文件创建成功:" + file.getPath());

} catch (IOException e) {

System.out.println("创建文件失败:" + e.getLocalizedMessage());

// TODO Auto-generated catch block

// e.printStackTrace();

// return;

}

}

// 创建文件输出流对象

FileOutputStream out = null;

try {

out = new FileOutputStream(file);

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

// return;

}

int c;

byte buffer[] = new byte[1024];

try {

while ((c = in.read(buffer)) != -1) {

for (int i = 0; i < c; i++) {

out.write(buffer[i]);

}

}

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

try {

in.close();

out.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值