java创建文件夹,文件;删除文件夹,文件
来源:互联网 发布:淘宝退货协议 编辑:程序博客网 时间:2024/05/16 09:07
java创建文件夹,文件;删除文件夹,文件
package ifpre.web;
import gxdmif.GXStringUtil;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import org.springframework.web.multipart.MultipartFile;
public class SaveFile {
public boolean save(String path, MultipartFile file) throws Exception {
GXStringUtil gx = new GXStringUtil();
boolean result = false ;
File dirFile = null ;
try {
dirFile = new File(path);
if ( ! (dirFile.exists()) && ! (dirFile.isDirectory())) {
boolean creadok = dirFile.mkdirs();
if (creadok) {
System.out.println( " ok:创建文件夹成功! " );
} else {
System.out.println( " err:创建文件夹失败! " );
}
}
} catch (Exception e) {
e.printStackTrace();
System.out.println(e);
return false ;
}
if (file != null && ! file.isEmpty()) {
String fullPath = path + System.getProperty( " file.separator " )
+ gx.netStringToGBK(file.getOriginalFilename());
DataOutputStream out = null ;
InputStream is = null ;
try {
out = new DataOutputStream( new FileOutputStream(fullPath));
is = file.getInputStream();
byte [] buffer = new byte [ 1024 ];
while (is.read(buffer) > 0 ) {
out.write(buffer);
}
} finally {
if (is != null ) {
is.close();
}
if (out != null ) {
out.close();
}
}
result = true ;
}
return result;
}
public boolean delete(String path, MultipartFile file) throws Exception {
GXStringUtil gx = new GXStringUtil();
boolean result = false ;
if (file != null && ! file.isEmpty()) {
String fullPath = path + System.getProperty( " file.separator " )
+ gx.netStringToGBK(file.getOriginalFilename());
try {
File file2 = new File(fullPath);
file2.delete();
result = true ;
} catch (Exception e) {
e.printStackTrace();
result = false ;
}
}
return result;
}
public boolean deleteFolder(File folder) {
boolean result = false ;
try {
String childs[] = folder.list();
if (childs == null || childs.length <= 0 ) {
if (folder.delete()) {
result = true ;
}
} else {
for ( int i = 0 ; i < childs.length; i ++ ) {
String childName = childs[i];
String childPath =
folder.getPath() + File.separator + childName;
File filePath = new File(childPath);
if (filePath.exists() && filePath.isFile()) {
if (filePath.delete()) {
result = true ;
} else {
result = false ;
break ;
}
}
else if (filePath.exists() && filePath.isDirectory()) {
if (deleteFolder(filePath)) {
result = true ;
} else {
result = false ;
break ;
}
}
}
}
folder.delete();
} catch (Exception e) {
e.printStackTrace();
result = false ;
}
return result;
}
}
import gxdmif.GXStringUtil;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import org.springframework.web.multipart.MultipartFile;
public class SaveFile {
public boolean save(String path, MultipartFile file) throws Exception {
GXStringUtil gx = new GXStringUtil();
boolean result = false ;
File dirFile = null ;
try {
dirFile = new File(path);
if ( ! (dirFile.exists()) && ! (dirFile.isDirectory())) {
boolean creadok = dirFile.mkdirs();
if (creadok) {
System.out.println( " ok:创建文件夹成功! " );
} else {
System.out.println( " err:创建文件夹失败! " );
}
}
} catch (Exception e) {
e.printStackTrace();
System.out.println(e);
return false ;
}
if (file != null && ! file.isEmpty()) {
String fullPath = path + System.getProperty( " file.separator " )
+ gx.netStringToGBK(file.getOriginalFilename());
DataOutputStream out = null ;
InputStream is = null ;
try {
out = new DataOutputStream( new FileOutputStream(fullPath));
is = file.getInputStream();
byte [] buffer = new byte [ 1024 ];
while (is.read(buffer) > 0 ) {
out.write(buffer);
}
} finally {
if (is != null ) {
is.close();
}
if (out != null ) {
out.close();
}
}
result = true ;
}
return result;
}
public boolean delete(String path, MultipartFile file) throws Exception {
GXStringUtil gx = new GXStringUtil();
boolean result = false ;
if (file != null && ! file.isEmpty()) {
String fullPath = path + System.getProperty( " file.separator " )
+ gx.netStringToGBK(file.getOriginalFilename());
try {
File file2 = new File(fullPath);
file2.delete();
result = true ;
} catch (Exception e) {
e.printStackTrace();
result = false ;
}
}
return result;
}
public boolean deleteFolder(File folder) {
boolean result = false ;
try {
String childs[] = folder.list();
if (childs == null || childs.length <= 0 ) {
if (folder.delete()) {
result = true ;
}
} else {
for ( int i = 0 ; i < childs.length; i ++ ) {
String childName = childs[i];
String childPath =
folder.getPath() + File.separator + childName;
File filePath = new File(childPath);
if (filePath.exists() && filePath.isFile()) {
if (filePath.delete()) {
result = true ;
} else {
result = false ;
break ;
}
}
else if (filePath.exists() && filePath.isDirectory()) {
if (deleteFolder(filePath)) {
result = true ;
} else {
result = false ;
break ;
}
}
}
}
folder.delete();
} catch (Exception e) {
e.printStackTrace();
result = false ;
}
return result;
}
}
- java创建文件夹,文件;删除文件夹,文件
- java创建文件夹,文件;删除文件夹,文件
- java创建文件夹,文件;删除文件夹,文件
- Java创建、重命名、删除文件和文件夹
- Java创建、重命名、删除文件和文件夹
- java创建和删除文件夹、文件
- java 文件,文件夹的创建和删除
- java创建删除文件及文件夹大全
- Java创建、重命名、删除文件和文件夹
- Java创建、重命名、删除文件和文件夹
- Java创建、重命名、删除文件和文件夹
- java删除文件夹 文件
- Java 删除文件、文件夹
- java创建文件文件夹,删除空文件夹,删除文件夹以及所有文件
- java创建文件、文件夹
- java创建文件、文件夹
- JAVA 创建文件和文件夹,删除文件和文件夹,复制文件和文件夹,移动文件和文件夹的实用工具类
- 【C#】创建文件夹,删除文件夹,创建文件,删除文件
- 宽字符处理函数函数与普通函数对照表
- 办公自动化OA系统的主要功能
- 怎么样将sql server数据库中的结构导入到powerdesign中
- SQL: SSIS的配置文件设置以及ssis包在web中的调用
- 通信: 在 Windows Server 2008 上运行 Exchange
- java创建文件夹,文件;删除文件夹,文件
- 什么是JavaEE
- 你为什么在Windows桌面右键菜单上狂点“刷新”?
- 新版.Net开发必备十大工具
- 转一道有意思的面试题
- 如何恢复 Linux 上删除的文件,第 4 部分
- 一道关于容器的面试题
- Linux操作系统常用命令之cat
- 今天开始了我的博客之旅