package com.tntxia.java.util;
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代码常用的算法代码,如ucs2,ascii,进制转换,以及APN相关的管理代码
工程简单的介绍了java常用类,并用这些类进行一些简单的操作 让初学者更好的了解java这门语言的特性。 1.StringAndInt.java 字符与整型的相互转换 2.WriteFile.java 简单的IO读写文件 3.CurrentMethod.java 获取当前...
java常用代码方法很适合初学者和刚刚参加工作的程序员,里面包含了常用正则表达式、公共日期类、串口驱动、各种数据库连接、公交换乘算法、 列出某文件夹及其子文件夹下面的文件,并可根据扩展名过滤等等很多有用的...
各种Java功能代码,Java访问XML文件,Java数据库访问技术,Java对工作方式日期的处理
◆弹出对话框后在本窗体打开文件index.jsp out.println("<script language='javascript'>alert('弹出内容!');window.location.href='index.jsp';</script>"); ◆弹出对话框后在父窗体打开文件index.jsp out....
Java
该文件是《Java常用算法手册》(第三版-宋娟)里的算法(包括数据结里的算法,都是用Java实现的,是Java学者的必备资料)实现代码,可直接编译运行。
将xml的创建,增加元素,查找元素,修改元素,使用dom,sax,dom4j方式解析xml文件,希望对大家有帮助
收集了一些实用的Java类库文件集,全部都是Java源文件,比如加密解密类、文件帮助类、ftp二进制与ascii传输方式区别、文件上传类、MD5加密类、文件滤镜类、数据库连接类和操作类、...,一共有近30个代码文件...
Java二进制IO类与文件复制操作实例,好像是一本书的例子,源代码有的是独立运行的,与同目录下的其它代码文件互不联系,这些代码面向初级、中级Java程序员。 Java访问权限控制源代码 1个目标文件 摘要:Java源码,...
这里收集了部分常用源代码。。。文件读取,多线程等等
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。
Java二进制IO类与文件复制操作实例,好像是一本书的例子,源代码有的是独立运行的,与同目录下的其它代码文件互不联系,这些代码面向初级、中级Java程序员。 Java访问权限控制源代码 1个目标文件 摘要:Java源码,...
这是java常用类解析系列博客中的示例代码及自己写的工具类,代码注释详细,博客地址:http://blog.csdn.net/touch_2011/article/details/6860043 主要讲解了System类、Object类、Arrays类、Cloneable接口、IO系统...
Java二进制IO类与文件复制操作实例,好像是一本书的例子,源代码有的是独立运行的,与同目录下的其它代码文件互不联系,这些代码面向初级、中级Java程序员。 Java访问权限控制源代码 1个目标文件 摘要:Java源码,...
2、采用链式调用模式,只需要简单的配置当前运行采用哪个厂商,即可灵活的传输文件到对应云文件服务器中 3、支持OSS、COS、OBS、Minio等,如果是单机应用直接配置成本地即可 4、适用于对文件依赖比较大的应用
常用java IO操作以及用jxl读写excel 工具源代码 此工具作用:把目录里面的文件旧的国际化key值换成新的国际化值。根据excel文件。
Java实现添加水印,文件上传,生成略缩图,文件操作,Md5加密码,时间日期操作等java常用的工具类源码(28个合集),可直接用于你的项目设计中,实战中有些代码直接套用就ok,不用动手了,会写代码的不一定是高手,...
代码范例列表 第1章 示例描述:本章演示如何开始使用JDK进行程序的开发。 HelloWorldApp.java 第一个用Java开发的应用程序。 firstApplet.java 第一个用Java开发的Applet小程序。 firstApplet.htm 用来装载...
Java一些常用验证整理,附有有关代码。