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);
}
}
分享到:
相关推荐
NULL 博文链接:https://chong0660.iteye.com/blog/2367232
java语言中的基本的文件操作介绍,附源码
JAVA 文件常用流操作 包括文件的创建,重命名,删除等等。字节流,字符流,缓存流,数据流,打印流,内存流等等
Java文件操作中的一些常用方法的总结,可以参考参考啦!
在Java后端中处理文件通常涉及到文件的读取、写入、删除以及可能的文件流处理等操作。
java文件的工具类,封装了常用的操作,尤其针对文件的实际类型,通过获取文件的byte,来查看文件起始字节的魔数值,通过魔数值来判断文件的类型,工具集合了常用的文件类型对应的魔数,也封装了文件类型的判断方法
最近在做ftp文件的上传与下载,基于此,整理了一下资料。本来想采用java自带的方法,可是看了一下jdk1.6与1.7的实现方法有点区别,于是采用了Apache下的框架实现的。。。
Java常用FTP 文件操作 说明 Apache FTPClient ftp4j jftp java中实现ftp 文件上传 文件下载
与大家分享一个功能丰富的Java文件操作类,类中封装了一些常用的文件操作,大部分涉及文件的修改、内容替换等。类中的方法都是静态方法,不需要生成此类的实例, 为避免生成此类的实例,构造方法被申明为private类型...
《Java文件操作大全》电子书 本文汇集常用文件操作方法,包括文件的建立/检查与删除,目录的建立/检查与删除,取出目录中文件,文件属性的取得,逐行读取数据等等。
java常用命令及常用选项:包含了对java文件的一般的命令行操作
工程简单的介绍了java常用类,并用这些类进行一些简单的操作 让初学者更好的了解java这门语言的特性。 1.StringAndInt.java 字符与整型的相互转换 2.WriteFile.java 简单的IO读写文件 3.CurrentMethod.java 获取当前...
使用jdk7及以后的文件操作,包含了常用类、常用方法的介绍和文件上传和下载的代码
该类主要对常见的一些文件操作进行了封装,如读写文件(UTF-8)、复制文件、删除文件,创建目录等
java 操作Zip文件(压缩、解压、加密) zip4j-1.3.2.jar ant-1.10.6.jar
A.2实验二:熟悉常用的HDFS操作 ...(3)熟悉HDFS操作常用的Java API。 A.2.2 实验平台 (1)操作系统:Linux(建议Ubuntu 16.04)。(2) Hadoop版本:2.7.1。 (3)JDK版本:1.7或以上版本。(4) Java IDE:Eclipse。
JAVAWEB各种配置文件加常用操作(spring strut2 jpa hibernate jdbc jndi springMVC velocity ant log4j ehcache等各种配置文件及常用操作),相信对你的开发一定会有很大的帮助!
《java实用组件集》 源码 文件操作组件 运行注意事项: 1、注意包名的大小写情况 2、注意页面的编码问题
阅读提示:本文介绍JAVA对数字证书的常用操作 一需要包含的包 import java.security.*; import java.io.*; import java.util.*; import java.security.*; import java.security.cert.*; import sun.security.x509....
提供了很丰富的java工具类,包括字符串、数字、日期、文件、图像、编码解码、校验工具、文档操作等。...文件操作,包括file、image、zip等 - 5.http - 6.jdbc - 7.json - 8.邮件 - 9.二维码、条形码 - 10.web