1 23 24 package org.infoglue.cms.io; 25 26 import java.io.BufferedOutputStream ; 27 import java.io.BufferedReader ; 28 import java.io.BufferedWriter ; 29 import java.io.DataOutputStream ; 30 import java.io.File ; 31 import java.io.FileInputStream ; 32 import java.io.FileOutputStream ; 33 import java.io.FileWriter ; 34 import java.io.InputStream ; 35 import java.io.InputStreamReader ; 36 import java.io.OutputStreamWriter ; 37 import java.io.PrintWriter ; 38 import java.io.Writer ; 39 40 41 public class FileHelper 42 { 43 44 56 57 public synchronized static void writeToFile(File file, String text, boolean isAppend) throws Exception 58 { 59 PrintWriter pout = new PrintWriter (new FileWriter (file), isAppend); 60 pout.println(text); 61 pout.close(); 62 } 63 64 76 77 public synchronized static void writeUTF8ToFileSpecial(File file, String text, boolean isAppend) throws Exception 79 { 80 87 88 DataOutputStream dos = new DataOutputStream (new FileOutputStream (file, isAppend)); 89 dos.writeBytes(text); 90 dos.flush(); 91 dos.close(); 92 93 } 94 95 public synchronized static void writeUTF8(File file, String text, boolean isAppend) throws Exception 97 { 98 FileOutputStream fos = new FileOutputStream (file, isAppend); 99 Writer out = new OutputStreamWriter (fos, "UTF-8"); 100 out.write(text); 101 out.flush(); 102 out.close(); 103 } 104 105 106 public synchronized static void writeUTF8ToFile(File file, String text, boolean isAppend) throws Exception 107 { 108 Writer out = new BufferedWriter (new OutputStreamWriter (new FileOutputStream (file), "UTF8")); 109 out.write(text); 110 out.flush(); 111 out.close(); 112 } 113 114 126 127 public synchronized static String readUTF8FromFile(File file) throws Exception 128 { 129 BufferedReader in = new BufferedReader (new InputStreamReader (new FileInputStream (file), "UTF8")); 130 String str = in.readLine(); 131 132 StringBuffer sb = new StringBuffer (); 133 134 int ch; 135 while ((ch = in.read()) > -1) { 136 sb.append((char)ch); 137 } 138 in.close(); 139 140 return sb.toString(); 141 } 142 143 151 152 public static byte[] getFileBytes(File file) throws Exception 153 { 154 FileInputStream fis = new FileInputStream (file); 155 byte[] fileBytes = new byte[(int)file.length()]; 156 fis.read(fileBytes); 157 fis.close(); 158 159 return fileBytes; 160 } 161 162 163 171 172 public static String getFileAsString(File file) throws Exception 173 { 174 StringBuffer sb = new StringBuffer (); 175 176 FileInputStream fis = new FileInputStream (file); 177 int c; 178 while((c = fis.read()) != -1) 179 { 180 sb.append((char)c); 181 } 182 183 fis.close(); 184 185 return sb.toString(); 186 } 187 188 189 197 198 public static String getStreamAsString(InputStream inputStream) throws Exception 199 { 200 StringBuffer sb = new StringBuffer (); 201 202 if(inputStream != null) 203 { 204 int c; 205 while((c = inputStream.read()) != -1) 206 { 207 sb.append((char)c); 208 } 209 210 inputStream.close(); 211 } 212 213 return sb.toString(); 214 } 215 216 217 225 226 public static void writeToFile(File file, byte[] data) throws Exception 227 { 228 FileOutputStream fos = new FileOutputStream (file); 229 BufferedOutputStream bos = new BufferedOutputStream (fos); 230 for(int i=0; i < data.length; i++) 231 { 232 bos.write(data[i]); 233 } 234 235 bos.flush(); 236 bos.close(); 237 fos.close(); 238 } 239 } | Popular Tags |