- 浏览: 46684 次
- 性别:
- 来自: 杭州
-
最新评论
-
chenyunhong:
[color=gray][/color][*][img][/i ...
OpenSessionInViewFilter 说明与配置
package test; 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); } public static void main(String[] args) { } }
发表评论
文章已被作者锁定,不允许评论。
-
jackrabbit 内容仓库
2011-05-12 09:16 1Java Content Rep ... -
使用jstl标签报错问题
2011-04-28 09:31 3059JSP中出现According to TLD or ... -
spring中配置dbcp连接池
2011-03-17 09:37 1676使用ApplicationServer级别的连接池, 在A ... -
java程序员需要掌握的25点
2010-12-14 10:18 10841.你需要精通面向对象分析与设计(OOA/OOD)、涉及模式( ... -
IT人士必去的10个网站
2010-12-07 13:04 24951、Chinaunix 网址:http://www.china ... -
spring jar 相关说明
2010-12-02 16:54 1116前半部分spring本身的 ... -
java枚举
2010-06-22 15:14 1142public class TestEnum { /*最普 ... -
Java调用SQL Server的存储过程详解
2010-01-20 12:11 12871使用不带参数的存储过程 使用 JDBC 驱动程序调用 ... -
正则表达式 匹配 手机和电话号码
2010-01-14 15:44 2146((^(13|15|18)[0-9]{9}$)|(^0[1,2 ... -
验证数字的正则表达式集
2010-01-12 14:03 1159验证数字的正则表达式集 验证数字:^[0-9]*$验证n位的 ... -
运行多个tomcat实例两法
2009-08-18 16:48 2215如果需要把一个alpha版 ... -
比较一下java写文本文件文件的性能
2009-06-05 10:58 1122最近对以前开发的一个通用数据迁移的软件进行优化。 ... -
java native关键字
2009-03-18 14:30 2940native是与C++联合开发的时候用的!java自己开发不用 ... -
Java利用对象流对对象进行序列化
2008-12-26 11:16 1421在一个程序运行的时候,其中的变量数据是保存在内存中的,一旦程序 ... -
Java中的instanceof关键字
2008-12-15 12:11 846instanceof是Java的一个二元操作符,和==,> ... -
==与.equals()方法的区别
2008-12-15 12:10 1091==与.equals()的区别 比较内存地址是否相同:.eq ...
相关推荐
java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java...
Java文件操作封装类
java文件操作工具类是java中针对文件操作的一个工具类,可以方便的读取,下载,上传文件等操作,希望可以帮到大家。
Java文件操作是编程中常见的任务,涉及到文件的创建、删除、读取、写入以及更复杂的操作如复制、移动、加密、压缩等。以下是一些关键的Java文件操作知识点: 1. **创建文件夹**:使用`java.io.File`类的`mkdir()`或...
java视频教程 Java文件操作 JavaFile