snow_xp
作者snow_xp·2010-11-16 16:24
系统工程师·

Java 文件操作

字数 16146阅读 2020评论 0赞 0
[代码] java代码
001 package util; 

002   

003 import java.io.File; 

004 import java.io.FileInputStream; 

005 import java.io.FileOutputStream; 

006 import java.io.FilenameFilter; 

007   

008 public class FileUtil { 

009    private final static String FILE_SUFFIX = ".java.drl"; 

010   

011    private final static String FILE_TEMP = "C:/temp/"; 

012   

013    /** 

014      * 将已存在的drl文件删除 

015      * 

016      * @param ObjectPath 

017      */

018    public static void deleteExistedDRLFile(String ObjectPath) { 

019        File filePath = new File(ObjectPath); 

020        if (!filePath.exists()) { 

021            System.out.println("目录不存在!"); 

022        } else { 

023            if (filePath.isDirectory()) { 

024                File[] list = filePath.listFiles(new FilenameFilter() { 

025                    public boolean accept(File dir, String name) { 

026                        return name.endsWith(FILE_SUFFIX); 

027                    } 

028                }); 

029                for (int i = 0; i < list.length; i++) { 

030                    list.delete(); 

031                } 

032            } 

033        } 

034    } 

035   

036    /** 

037      * 创建文件夹,如果有则不创建 

038      * 

039      * @param ObjectPath 

040      */

041    public static boolean creareDirectory(String ObjectPath) { 

042        boolean flag = true; 

043   

044        File filePath = new File(ObjectPath); 

045        if (!filePath.exists()) { 

046            filePath.mkdir(); 

047            flag = false; 

048        } 

049   

050        return flag; 

051    } 

052   

053    /** 

054      * 查看某文件夹下面是否有文件,有文件则创建一个temp文件夹,将文件拷贝到temp目录下(备份文件) 没有文件怎什么都不做 

055      * 备份后,把原文件夹里文件删除 

056      * 

057      * @param ObjectPath 

058      */

059    public static void backupFile(String ObjectPath, String dirName) { 

060        String backupPath; 

061   

062        if (!FILE_TEMP.endsWith(File.separator)) { 

063            backupPath = FILE_TEMP + File.separator + dirName; 

064        } else { 

065            backupPath = FILE_TEMP + dirName; 

066        } 

067   

068        File backupFilePath = new File(backupPath); 

069        if (!backupFilePath.exists()) { 

070            backupFilePath.mkdirs(); 

071        } 

072        File filePath = new File(ObjectPath); 

073        if (!filePath.exists()) { 

074            System.out.println("目录不存在!"); 

075        } else { 

076            if (filePath.isDirectory()) { 

077                File[] list = filePath.listFiles(); 

078                if (list != null && list.length != 0) { 

079                    copyFolder(ObjectPath, backupPath);// 文件备份 

080                    for (int i = 0; i < list.length; i++) { 

081                        list.delete(); 

082                    } 

083                } 

084            } 

085        } 

086    } 

087   

088    /** 

089      * 复原文件,把文件从备份文件夹拷贝到原来文件夹 

090      * 

091      * @param ObjectPath 

092      * @param dirName 

093      */

094    public static void recoverFile(String ObjectPath, String dirName) { 

095        String backupPath; 

096        if (ObjectPath.endsWith(File.separator)) { 

097            ObjectPath = new StringBuffer(ObjectPath).append(dirName) 

098                    .toString(); 

099        } else { 

100            ObjectPath = new StringBuffer(ObjectPath) 

101                    .append(File.separatorChar).append(dirName).toString(); 

102        } 

103   

104        if (!FILE_TEMP.endsWith(File.separator)) { 

105            backupPath = FILE_TEMP + File.separator + dirName; 

106        } else { 

107            backupPath = FILE_TEMP + dirName; 

108        } 

109        File backupFilePath = new File(backupPath); 

110        if (!backupFilePath.exists()) { 

111            backupFilePath.mkdirs(); 

112        } 

113        File filePath = new File(ObjectPath); 

114        if (!filePath.exists()) { 

115            System.out.println("目录不存在!"); 

116        } else { 

117            if (filePath.isDirectory()) { 

118                File[] list = filePath.listFiles(); 

119                if (list != null && list.length != 0) { 

120                    copyFolder(backupPath, ObjectPath);// 文件复原 

121                } 

122            } 

123        } 

124    } 

125   

126    /** 

127      * 复制整个文件夹内容 

128      * 

129      * @param oldPath 

130      *            String 原文件路径 如:c:/fqf 

131      * @param newPath 

132      *            String 复制后路径 如:f:/fqf/ff 

133      * @return boolean 

134      */

135    public static void copyFolder(String oldPath, String newPath) { 

136        try { 

137            (new File(newPath)).mkdir(); // 如果文件夹不存在 则建立新文件夹 

138            File a = new File(oldPath); 

139            String[] file = a.list(); 

140            File temp = null; 

141            for (int i = 0; i < file.length; i++) { 

142                if (oldPath.endsWith(File.separator)) { 

143                    temp = new File(oldPath + file); 

144                } else { 

145                    temp = new File(oldPath + File.separator + file); 

146                } 

147   

148                if (temp.isFile()) { 

149                    FileInputStream input = new FileInputStream(temp); 

150                    FileOutputStream output = new FileOutputStream(newPath 

151                            + "/" + (temp.getName()).toString()); 

152   

153                    byte[] b = new byte[1024 * 5]; 

154                    int len; 

155                    while ((len = input.read(b)) != -1) { 

156                        output.write(b, 0, len); 

157                    } 

158                    output.flush(); 

159                    output.close(); 

160                    input.close(); 

161                } 

162                if (temp.isDirectory()) {// 如果是子文件夹 

163                    copyFolder(oldPath + "/" + file, newPath + "/" + file); 

164                } 

165            } 

166        } catch (Exception e) { 

167            System.out.println("复制整个文件夹内容操作出错"); 

168            e.printStackTrace(); 

169        } 

170    } 

171   

172    /** 

173      * 删除备份文件和存放备份文件的文件夹 

174      * 

175      * @param ObjectPath 

176      */

177    public static void deleteFileAndDirectory(String dirName) { 

178        String ObjectPath; 

179        if (!FILE_TEMP.endsWith(File.separator)) { 

180            ObjectPath = FILE_TEMP + File.separator + dirName; 

181        } else { 

182            ObjectPath = FILE_TEMP + dirName; 

183        } 

184   

185        File filePath = new File(ObjectPath); 

186        if (!filePath.exists()) { 

187            filePath.mkdirs(); 

188            System.out.println("目录不存在!"); 

189        } else { 

190            if (filePath.isDirectory()) { 

191                File[] list = filePath.listFiles(); 

192   

193                for (int i = 0; i < list.length; i++) { 

194                    list.delete(); 

195                } 

196            } 

197            filePath.delete(); 

198        } 

199    } 

200   

201    /** 

202      * 判断某文件夹下是否存在文件,存在返回true 

203      * 

204      * @param ObjectPath 

205      * @return 

206      */

207    public static boolean existFileInDirectory(String ObjectPath) { 

208        boolean flag = false; 

209        File filePath = new File(ObjectPath); 

210        if (filePath.exists()) { 

211   

212            if (filePath.isDirectory()) { 

213                File[] list = filePath.listFiles(); 

214                if (list != null && list.length != 0) { 

215                    flag = true; 

216                } 

217            } 

218        } 

219   

220        return flag; 

221    } 

222   

223    /** 

224      * 删除某个文件夹 

225      * @param ObjectPath 

226      */

227    public static void deleteDirectory(String ObjectPath) { 

228   

229        File filePath = new File(ObjectPath); 

230        if (filePath.exists()) { 

231            filePath.delete(); 

232        } 

233    } 

234    /** 

235      * 将已存在的文件删除 

236      * 

237      * @param ObjectPath 

238      */

239    public static boolean deleteExistedFile(String ObjectPath,final String fileName) { 

240        boolean flag =false; 

241        File filePath = new File(ObjectPath); 

242        if (filePath.exists()) {             

243            if (filePath.isDirectory()) { 

244                File[] list = filePath.listFiles(new FilenameFilter() { 

245                    public boolean accept(File dir, String name) { 

246                        return name.equals(fileName); 

247                    } 

248                }); 

249                for (int i = 0; i < list.length; i++) { 

250                    list.delete(); 

251                } 

252                flag=true; 

253            } 

254        } 

255           

256        return flag; 

257    } 

258 }

如果觉得我的文章对您有用,请点赞。您的支持将鼓励我继续创作!

0

添加新评论0 条评论

Ctrl+Enter 发表

作者其他文章

相关文章

相关问题

相关资料

X社区推广