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. Rxjs 修改Observable 里的值

    有这么一个对象c$: Observable<any> 修改里边的值: 声明一个subject subject: Subject<any>; 在ngOnInit()中进行初始化 ...

  2. [转]谈谈前端渲染 VS 后端渲染

    首先,预编译跟前后端没有关系,预编译一样可以用于后端渲染. 看看下面的测试时间,单位: ms 模板字符串: var s = '{{#datas}}{{name}} abcdefg {{type}} { ...

  3. 软件工程导论复习 如何画系统流程图和数据流图 part1

    一.数据流图与流程图的区别 数据流程图是以图形的方式表达在问题中信息的变换和传递过程.它把系统看成是由数据流联系的各种概念的组合,用分解及抽象手段来控制需求分析的复杂性,采用分层的数据流程图来表示一个 ...

  4. C#四舍五入说明

    string.Format("{0:N2}", d) 与 Math.Round(d, 2).ToString() 不总是相等 string.Format("{0:N2}& ...

  5. k8s 问题

    kubelet: Orphaned pod "4db449f0-4eaf-11e8-94ab-90b8d042b91a" found, but volume paths are s ...

  6. python 形参

    def fun(x="dx",y="dy"): print "fun------" print x print y fun()fun(&qu ...

  7. win10磁盘碎片整理

    如果我们想要加快win10系统运行速度的话,就需要定期整理碎片才可以,减少卡顿,提高性能. 一:注意事项 固态硬盘用户千万不要使用‘磁盘碎片整理功能’,因为使用的技术不一样,使用window自带的该功 ...

  8. maven tomcat7 远程热部署

    在maven项目开发中,一般推荐使用jetty进行开发调试.但是在项目发布的时候要求使用tomcat7作为发布服务器,为此在maven中配置了tomcat7插件,以支持项目在外部tomcat7进行远程 ...

  9. padright padleft

    在 C# 中可以对字符串使用 PadLeft 和 PadRight 进行轻松地补位. PadLeft(int totalWidth, char paddingChar) //在字符串左边用 paddi ...

  10. dfs小练 【dfs】

    1.前n个自然数的所有排列: #include <iostream> #include <cstdio> #include <cstring> using name ...