ut.println("新建文件操作出错"); 113 e.printStackTrace(); 114 } 115 } 116 117 /** 118 * 删除一个文件 119 * 120 * @param filePath 121 * 要删除文件的绝对路径 如:c:\\hongten\\Hello.txt 122 */ 123 public void deleteFile(String filePath) { 124 try { 125 File preDelFile = new File(filePath); 126 if (preDelFile.exists()) { 127 preDelFile.delete(); 128 } else { 129 System.out.println(filePath + "不存在!"); 130 } 131 } catch (Exception e) { 132 System.out.println("删除文件操作出错"); 133 e.printStackTrace(); 134 } 135 } 136 137 /** 138 * 删除一个文件夹 139 * 140 * @param folderPath 141 * 要删除的文件夹的绝对路径 如:c:\\hongten 142 */ 143 public void deleteFolder(String folderPath) { 144 try { 145 delAllFiles(folderPath); 146 File preDelFoder = new File(folderPath); 147 if (preDelFoder.exists()) { 148 preDelFoder.delete(); 149 } else { 150 System.out.println(folderPath + "不存在!"); 151 } 152 } catch (Exception e) { 153 System.out.println("删除文件操作出错"); 154 e.printStackTrace(); 155 } 156 } 157 158 /** 159 * 删除一个文件夹下的所有文件 160 * 161 * @param folderPath 162 * 要删除的文件夹的绝对路径 如:c:\\hongten 163 */ 164 public void delAllFiles(String folderPath) { 165 File file = new File(folderPath); 166 if (!file.exists()) { 167 return; 168 } 169 if (!file.isDirectory()) { 170 return; 171 } 172 String[] tempList = file.list(); 173 File temp = null; 174 for (int i = 0; i < tempList.length; i++) { 175 if (folderPath.endsWith(File.separator)) { 176 temp = new File(folderPath + tempList[i]); 177 } else { 178 temp = new File(folderPath + File.separator + tempList[i]); 179 } 180 if (temp.isFile()) { 181 temp.delete(); 182 } 183 if (temp.isDirectory()) { 184 delAllFiles(folderPath + "/" + tempList[i]);// 先删除文件夹里面的文件 185 deleteFolder(folderPath + "/" + tempList[i]);// 再删除空文件夹 186 } 187 } 188 } 189 190 /** 191 * 单个文件的复制 192 * 193 * @param oldPath 194 * 原路径 如:c:\\Hello.java 195 * @param newPath 196 * 新路径 如:f:\\Hello.java 197 */ 198 public void copyFile(String oldPath, String newPath) { 199 try { 200 int bytesum = 0; 201 int byteread = 0; 202 File oldfile = new File(oldPath); 203 if (oldfile.exists()) { // 文件存在时 204 InputStream inStream = new FileInputStream(oldPath); // 读入原文件 205 FileOutputStream fs = new FileOutputStream(newPath); 206 byte[] buffer = new byte[1444]; 207 // int length = 0; 208 while ((byteread = inStream.read(buffer)) != -1) { 209 bytesum += byteread; // 字节数 文件大小 210 fs.write(buffer, 0, byteread); 211 } 212 inStream.close(); 213 } 214 } catch (Exception e) { 215 S |