1 30 31 package com.genimen.djeneric.util; 32 33 import java.io.BufferedReader ; 34 import java.io.BufferedWriter ; 35 import java.io.File ; 36 import java.io.FileInputStream ; 37 import java.io.FileOutputStream ; 38 import java.io.FileReader ; 39 import java.io.FileWriter ; 40 import java.io.IOException ; 41 import java.io.InputStream ; 42 import java.io.InputStreamReader ; 43 import java.io.OutputStream ; 44 import java.io.Reader ; 45 46 public class DjFileUtil 47 { 48 49 public static void writeFile(String filename, String contents) throws IOException 50 { 51 BufferedWriter w = new BufferedWriter (new FileWriter (filename)); 52 w.write(contents); 53 w.close(); 54 } 55 56 public static void writeFile(String filename, byte[] contents) throws IOException 57 { 58 FileOutputStream fos = new FileOutputStream (new File (filename)); 59 fos.write(contents); 60 fos.close(); 61 } 62 63 public static String readResource(Class root, String fileName) throws IOException 64 { 65 InputStream is = root.getResourceAsStream(fileName); 66 return readStream(new InputStreamReader (is)); 67 } 68 69 public static String readResource(String fileName) throws IOException 70 { 71 InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName); 72 return readStream(new InputStreamReader (is)); 73 } 74 75 public static String readFile(String p_filenaam) throws IOException 76 { 77 Reader r = new FileReader (p_filenaam); 78 return readStream(r); 79 } 80 81 public static String readFile(String p_filenaam, String p_charset) throws IOException 82 { 83 InputStream is = new FileInputStream (p_filenaam); 84 Reader r = new InputStreamReader (is, p_charset); 85 return readStream(r); 86 } 87 88 public static String readStream(Reader p_reader) throws IOException 89 { 90 BufferedReader br = new BufferedReader (p_reader); 91 StringBuffer src = new StringBuffer (100); 92 String ln; 93 while ((ln = br.readLine()) != null) 94 { 95 src.append(ln); 96 src.append("\n"); 97 } 98 br.close(); 99 return src.toString(); 100 } 101 102 public static boolean createDirectory(String dir) 103 { 104 try 105 { 106 File fd; 107 if (dir.endsWith("/") || dir.endsWith("\\")) 108 { 109 fd = new File (dir.substring(0, dir.length() - 1)); 110 } 111 else 112 { 113 fd = new File (dir); 114 } 115 return fd.mkdirs(); 116 } 117 catch (Exception iox) 118 { 119 DjLogger.log(iox); 120 return false; 121 } 122 } 123 124 public static String trimLastSlash(String path) 125 { 126 String result = path; 127 128 int idx1 = path.lastIndexOf("/"); 129 int idx2 = path.lastIndexOf("\\"); 130 131 if (idx1 == -1) idx1 = idx2; 132 if (idx2 == -1) idx2 = idx1; 133 134 idx1 = Math.max(idx1, idx2); 135 if (idx1 != -1) 136 { 137 result = path.substring(0, idx1); 138 } 139 return result; 140 } 141 142 public static void copy(InputStream source, OutputStream target) throws IOException 143 { 144 int chunkSize = 63 * 1024; 145 byte[] ba = new byte[chunkSize]; 146 147 while (true) 148 { 149 150 int bytesRead = readBlocking(source, ba, 0, chunkSize); 151 if (bytesRead > 0) 152 { 153 target.write(ba, 0, bytesRead); 154 } 155 else 156 { 157 break; } 159 } 160 } 161 162 public static final int readBlocking(InputStream in, byte b[], long off, int len) throws IOException 163 { 164 int totalBytesRead = 0; 165 166 while (totalBytesRead < len) 167 { 168 int bytesRead = in.read(b, (int) (off + totalBytesRead), len - totalBytesRead); 169 if (bytesRead < 0) 170 { 171 break; 172 } 173 totalBytesRead += bytesRead; 174 } 175 return totalBytesRead; 176 } 177 178 } | Popular Tags |