1 18 19 package sync4j.framework.tools; 20 21 import java.io.*; 22 23 29 public class IOTools { 30 31 40 static public byte[] readFileBytes(File file) 41 throws IOException { 42 FileInputStream fis = null; 43 44 byte[] buf = new byte[(int)file.length()]; 45 try { 46 fis = new FileInputStream(file); 47 fis.read(buf); 48 fis.close(); 49 } finally { 50 if (fis != null) { 51 fis.close(); 52 } 53 } 54 55 return buf; 56 } 57 58 59 68 static public byte[] readFileBytes(String filename) 69 throws IOException { 70 return readFileBytes(new File(filename)); 71 } 72 73 82 static public String readFileString(File file) 83 throws IOException { 84 return new String (readFileBytes(file)); 85 } 86 87 96 static public String readFileString(String filename) 97 throws IOException { 98 return readFileString(new File(filename)); 99 } 100 101 109 static public void writeFile(String str, File file) 110 throws IOException { 111 writeFile(str.getBytes(), file); 112 } 113 114 122 static public void writeFile(String str, String filename) 123 throws IOException { 124 writeFile(str.getBytes(), new File(filename)); 125 } 126 127 135 static public void writeFile(byte[] buf, String filename) 136 throws IOException { 137 writeFile(buf, new File(filename)); 138 } 139 140 148 static public void writeFile(byte[] buf, File file) 149 throws IOException { 150 FileOutputStream fos = null; 151 try { 152 fos = new FileOutputStream(file); 153 fos.write(buf); 154 fos.close(); 155 } finally { 156 if (fos != null) { 157 fos.close(); 158 } 159 } 160 } 161 162 172 public static FilenameFilter getFileTypeFilter(String type) { 173 return new FileTypeFilter(type); 174 } 175 176 178 182 public static class FileTypeFilter implements FilenameFilter { 183 184 private String type; 185 186 193 public FileTypeFilter(final String type) { 194 this.type = type.toUpperCase(); 195 } 196 197 public boolean accept(File dir, String name) { 198 if (type == null) { 199 return true; 200 } 201 202 if (type.length() == 0) { 203 return (name.indexOf('.') < 0); 204 } 205 206 return (name.toUpperCase().endsWith(type)); 207 } 208 } 209 } | Popular Tags |