import java.io.*;

public class FileOperate { 
public FileOperate() { 
}

/** 
* 新建目录 
* @param folderPath String 如 c:/fqf 
* @return boolean 
*/ 
public void newFolder(String folderPath) { 
try { 
String filePath = folderPath; 
filePath = filePath.toString(); 
java.io.File myFilePath = new java.io.File(filePath); 
if (!myFilePath.exists()) { 
myFilePath.mkdir(); 


catch (Exception e) { 
System.out.println( "新建目录操作出错 "); 
e.printStackTrace(); 

}

/** 
* 新建文件 
* @param filePathAndName String 文件路径及名称 如c:/fqf.txt 
* @param fileContent String 文件内容 
* @return boolean 
*/ 
public void newFile(String filePathAndName, String fileContent) {

try { 
String filePath = filePathAndName; 
filePath = filePath.toString(); 
File myFilePath = new File(filePath); 
if (!myFilePath.exists()) { 
myFilePath.createNewFile(); 

FileWriter resultFile = new FileWriter(myFilePath); 
PrintWriter myFile = new PrintWriter(resultFile); 
String strContent = fileContent; 
myFile.println(strContent); 
resultFile.close();


catch (Exception e) { 
System.out.println( "新建目录操作出错 "); 
e.printStackTrace();

}

}

/** 
* 删除文件 
* @param filePathAndName String 文件路径及名称 如c:/fqf.txt 
* @param fileContent String 
* @return boolean 
*/ 
public void delFile(String filePathAndName) { 
try { 
String filePath = filePathAndName; 
filePath = filePath.toString(); 
java.io.File myDelFile = new java.io.File(filePath); 
myDelFile.delete();


catch (Exception e) { 
System.out.println( "删除文件操作出错 "); 
e.printStackTrace();

}

}

/** 
* 删除文件夹 
* @param filePathAndName String 文件夹路径及名称 如c:/fqf 
* @param fileContent String 
* @return boolean 
*/ 
public void delFolder(String folderPath) { 
try { 
delAllFile(folderPath); //删除完里面所有内容 
String filePath = folderPath; 
filePath = filePath.toString(); 
java.io.File myFilePath = new java.io.File(filePath); 
myFilePath.delete(); //删除空文件夹


catch (Exception e) { 
System.out.println( "删除文件夹操作出错 "); 
e.printStackTrace();

}

}

/** 
* 删除文件夹里面的所有文件 
* @param path String 文件夹路径 如 c:/fqf 
*/ 
public void delAllFile(String path) { 
File file = new File(path); 
if (!file.exists()) { 
return; 

if (!file.isDirectory()) { 
return; 

String[] tempList = file.list(); 
File temp = null; 
for (int i = 0; i < tempList.length; i++) { 
if (path.endsWith(File.separator)) { 
temp = new File(path + tempList[i]); 

else { 
temp = new File(path + File.separator + tempList[i]); 

if (temp.isFile()) { 
temp.delete(); 

if (temp.isDirectory()) { 
delAllFile(path+ "/ "+ tempList[i]);//先删除文件夹里面的文件 
delFolder(path+ "/ "+ tempList[i]);//再删除空文件夹 


}

/** 
* 复制单个文件 
* @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();

}

}

/** 
* 移动文件到指定目录 
* @param oldPath String 如:c:/fqf.txt 
* @param newPath String 如:d:/fqf.txt 
*/ 
public void moveFile(String oldPath, String newPath) { 
copyFile(oldPath, newPath); 
delFile(oldPath);

}

/** 
* 移动文件到指定目录 
* @param oldPath String 如:c:/fqf.txt 
* @param newPath String 如:d:/fqf.txt 
*/ 
public void moveFolder(String oldPath, String newPath) { 
copyFolder(oldPath, newPath); 
delFolder(oldPath);


}

Java文件复制删除操作合集的更多相关文章

  1. java基础 File 递归删除文件夹中所有文件文件夹 目录(包含子目录)下的.java文件复制到e:/abc文件夹中, 并统计java文件的个数

    File 递归删除文件夹中所有文件文件夹 package com.swift.kuozhan; import java.io.File; import java.util.Scanner; /*键盘录 ...

  2. Hadoop使用Java进行文件修改删除操作

    Hadoop使用Java进行文件修改删除操作 学习了:http://blog.csdn.net/menghuannvxia/article/details/44651061 学习了:http://bl ...

  3. Java基础面试操作题: File IO 文件过滤器FileFilter 练习 把一个文件夹下的.java文件复制到另一个文件夹下的.txt文件

    package com.swift; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File ...

  4. Java 分布式框架面试题合集

    Java 分布式框架面试题合集 1.什么是 ZooKeeper? 答:ZooKeeper 是一个开源的分布式应用程序协调服务,是一个典型的分布式数据一致性解决方案.设计目的是将那些复杂且容易出错的分布 ...

  5. SQL用法操作合集

    SQL用法操作合集   一.表的创建 1.创建表 格式: 1 CREATE TABLE 表名 2 (列名 数据类型(宽度)[DEFAULT 表达式][COLUMN CONSTRAINT], 3 ... ...

  6. java文件的读写操作

    java文件的读写操作主要是对输入流和输出流的操作,由于流的分类很多,所以概念很容易模糊,基于此,对于流的读写操作做一个小结. 1.根据数据的流向来分: 输出流:是用来写数据的,是由程序(内存)--- ...

  7. java文件夹相关操作 演示样例代码

    java文件夹相关操作 演示样例代码 package org.rui.io; import java.io.File; import java.io.FilenameFilter; import ja ...

  8. IO复制多级目录 控制台输入文件目录然后把目录下java文件复制到 D: 并统计java个数

    package cn.itcast_05; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; impor ...

  9. java 文件的读写操作

    java  文件的读写操作 一.读: public String getSetting() { HttpServletRequest request=org.apache.struts2.Servle ...

随机推荐

  1. ADO接口简介

    源地址:http://blog.csdn.net/xiaobai1593/article/details/7449151 参考: 1. 百度文库:http://wenku.baidu.com/view ...

  2. HQL中的Like查询需要注意的地方

    public List getOrgan(String organCode, String organName) {    String hsql;    List list; if (organCo ...

  3. 02 Tensorflow的安装配置

    1 anaconda 64 位,win10 安装 清华大学镜像网络,下载地址:https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/ 选择下载文件 ...

  4. 吴裕雄 数据挖掘与分析案例实战(8)——Logistic回归分类模型

    import numpy as npimport pandas as pdimport matplotlib.pyplot as plt # 自定义绘制ks曲线的函数def plot_ks(y_tes ...

  5. linux 杂

    set -e表示一旦脚本中有命令的返回值为非0,则脚本立即退出,后续命令不再执行; set -o pipefail表示在管道连接的命令序列中,只要有任何一个命令返回非0值,则整个管道返回非0值,即使最 ...

  6. dubbo2.5.3升级到dobbo2.8.4(dubbox) jar

    需要注意的地方: 1.pom文件中 dubbo的版本由2.5.3变为2.8.4,maven依赖如下:     <dependency>         <groupId>com ...

  7. MAVEN 引入外部JAR 包

    MAVEN引入AXIS依赖的JAR包 在POM.XML中加入即可 <!-- axis 1.4 jar start --> <dependency> <groupId> ...

  8. Oracle to_date()函数的用法《转载》

    to_date()是Oracle数据库函数的代表函数之一,下文对Oracle to_date()函数的几种用法作了详细的介绍说明, 原文地址:http://database.51cto.com/art ...

  9. How Many Answers Are Wrong(带权并查集)

    How Many Answers Are Wrong http://acm.hdu.edu.cn/showproblem.php?pid=3038 Time Limit: 2000/1000 MS ( ...

  10. Jmeter元件作用域

    转载自飞测团队