一、前言
关于oschrenk-util源码包中com.oschrenk.utils.IOUtils输入输出工具类,进行文件移动move、指定路径读取readTextFile为字符串/读取为二进制数组readBinary/、写出文本字符串内容指定路径writeTextFile及目录的常见操作,详情参见源码说明。
二、源码说明
![]() 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 | package com.oschrenk.utils; import com.google.common.base.Nullable; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.Flushable; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintStream; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import java.util.Set; public class IOUtils { private static final String COPY_PREFIX = "copy" ; public static boolean move(File from, File to) { return from.renameTo(to); } public static boolean move(File from, File toDir, String newFilename) { return from.renameTo( new File(toDir, newFilename)); } public static boolean move(File from, File toDir, File toFile) { return from.renameTo( new File(toDir, toFile.getName())); } public static String readTextFile(String path) throws IOException { return readTextFile( new File(path)); } public static String readTextFile(File file) throws IOException { return readTextFile(file, getDefaultInputEncoding()); } public static String readTextFile(String path, String encoding) throws IOException, UnsupportedEncodingException { return readTextFile( new File(path), encoding); } public static String readTextFile(File file, String encoding) throws IOException, UnsupportedEncodingException { BufferedReader bufferedReader = new BufferedReader( new InputStreamReader( new FileInputStream(file), encoding)); StringBuffer stringBuffer = new StringBuffer(); try { while ((line = bufferedReader.readLine()) != null ) { String line; stringBuffer.append(line); stringBuffer.append(System.getProperty( "line.separator" )); } } finally { bufferedReader.close(); } return stringBuffer.toString(); } public static byte [] readBinary(String path) throws IOException { return readBinary( new File(path)); } public static byte [] readBinary(File file) throws IOException { BufferedInputStream bis = null ; try { bis = new BufferedInputStream( new FileInputStream(file)); int size = bis.available(); byte [] bytes = new byte [size]; bis.read(bytes); byte [] arrayOfByte1 = bytes; return arrayOfByte1; } finally { close( new Closeable[] { bis }); } } public static void writeBinaryFile( byte [] data, String path) throws IOException { writeBinaryFile(data, new File(path)); } public static void writeBinaryFile( byte [] data, File file) throws IOException { InputStream in = new BufferedInputStream( new ByteArrayInputStream(data)); OutputStream out = new BufferedOutputStream( new FileOutputStream(file)); byte [] buffer = new byte [ 65536 ]; int read = - 1 ; try { while ((read = in.read(buffer)) >= 0 ) out.write(buffer, 0 , read); } finally { in.close(); out.close(); } } public static void writeTextFile(String text, String path) throws IOException { writeTextFile(text, new File(path)); } public static void writeTextFile(String text, File file) throws IOException { writeTextFile(text, getDefaultOutputEncoding(), file); } public static void writeTextFile(String text, String encoding, String path) throws IOException { writeTextFile(text, encoding, new File(path)); } public static void writeTextFile(String text, String encoding, File file) throws IOException { try { file.delete(); } catch (SecurityException se) { throw new IOException( "Can't delete old file. Check Access Control." ); } appendText(text, encoding, file); } public static void appendText(String text, String path) throws IOException { appendText(text, new File(path)); } public static void appendText(String text, File file) throws IOException { appendText(text, getDefaultOutputEncoding(), file); } public static void appendText(String text, String encoding, String path) throws IOException { appendText(text, encoding, new File(path)); } public static void appendText(String text, String encoding, File file) throws IOException { if (!(file.exists())) { try { file.createNewFile(); } catch (IOException e) { throw new IOException( "Can't create new File. Check Access Control." ); } } if (!(file.canWrite())) { throw new IOException( "Can't write to file. Check Access Control." ); } PrintWriter pw = new PrintWriter( new BufferedWriter( new OutputStreamWriter( new FileOutputStream(file, true ), encoding))); try { pw.println(); pw.print(text); } finally { pw.close(); } } public static String getExtension(String path) { int lastSep = path.lastIndexOf(Systems.FILE_SEPARATOR); int lastExt = path.lastIndexOf( 46 ); if ((lastExt == - 1 ) || (lastSep > lastExt)) return "" ; return path.substring(lastExt + 1 , path.length()); } public static String getExtension(File file) { return getExtension(file.getName()); } public static String getFilename(String path) { String name = path.substring(path.lastIndexOf(Systems.FILE_SEPARATOR) + 1 , path.length()); if (name.lastIndexOf( 46 ) >= 0 ) { return name.substring( 0 , name.lastIndexOf( 46 )); } return name; } public static String getFilename(File file) { return getFilename(file.getName()); } public static String getBaseDir(File file) throws IOException { String path = file.getCanonicalPath(); return path.substring( 0 , path.lastIndexOf(Systems.FILE_SEPARATOR)); } public static String getBaseDir(String path) { return path.substring( 0 , path.lastIndexOf(Systems.FILE_SEPARATOR)); } public static String getDefaultOutputEncoding() { return new OutputStreamWriter( new ByteArrayOutputStream( 0 )).getEncoding(); } public static String getDefaultInputEncoding() { return new InputStreamReader( new ByteArrayInputStream( new byte [ 0 ])).getEncoding(); } public static boolean makeDirectories(Set<File> directories) throws IOException { boolean result = true ; for (File file : directories) result = (result) && (file.mkdirs()); return result; } public static boolean deleteDirectory(File dir) { String[] children; int i; if (dir.isDirectory()) { children = dir.list(); for (i = 0 ; i < children.length; ++i) { boolean success = deleteDirectory( new File(dir, children[i])); if (!(success)) return false ; } } return dir.delete(); } public static File getAutoRename(File file) throws IOException { if (!(file.exists())) { return file; } if (file.isDirectory()) { throw new IllegalArgumentException( "Can't rename directories." ); } String filename = getFilename(file.getName()); StringBuilder sb = new StringBuilder(); sb.append(getBaseDir(file)); sb.append(filename); int pos = filename.lastIndexOf( "copy" ); if (pos < 0 ) { sb.append( " copy" ); sb.append(getExtension(file.getName())); return getAutoRename( new File(sb.toString())); } if (pos < filename.length() - "copy" .length()) { int number; try { number = Integer.parseInt(filename.substring(pos, filename.length())); } catch (NumberFormatException e) { sb.append( " copy" ); sb.append(getExtension(file.getName())); return getAutoRename( new File(sb.toString())); } sb.append( " " ); sb.append(number + 1 ); sb.append(getExtension(file.getName())); return getAutoRename( new File(sb.toString())); } return file; } public static String streamToString(InputStream stream) throws IOException { InputStreamReader reader; try { reader = new InputStreamReader(stream); BufferedReader buffered = new BufferedReader(reader); StringBuilder builder = new StringBuilder(); while ((line = buffered.readLine()) != null ) { String line; builder.append(line); builder.append( "\n" ); } String str1 = builder.toString(); return str1; } finally { close( new Closeable[] { stream }); } } public static void close( @Nullable Closeable[] inputs) { if (inputs == null ) return ; Closeable[] arr$ = inputs; int len$ = arr$.length; for ( int i$ = 0 ; i$ < len$; ++i$) { Closeable input = arr$[i$]; if (input instanceof Flushable) try { ((Flushable)input).flush(); } catch (IOException e) { System.err.println( "IO Exception during closing, when trying to flush stream (" + input + ")." + e); } try { input.close(); } catch (IOException e) { System.err.println( "IO Exception during closing stream (" + input + ")." + e); } } } |