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
| public class FileUtil {
public static boolean createFile(File fileName) { boolean flag = false; if (!fileName.exists()) { try { flag = fileName.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } return flag; }
public static String readTxtFile(File fileName) { Reader reader = null; try { reader = new FileReader(fileName); StringBuilder sb = new StringBuilder(); int len = -1; char[] cache = new char[1024];
while ((len = reader.read(cache)) != -1) { sb.append(new String(cache, 0, len)); } return sb.toString(); } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } return null; }
public static boolean copyFile(String source, String target) { BufferedWriter bufferedWriter = null; try { bufferedWriter = new BufferedWriter(new FileWriter(target)); bufferedWriter.write(source); return true; } catch (IOException e) { e.printStackTrace(); } finally { if (bufferedWriter != null) { try { bufferedWriter.close(); } catch (IOException e) { e.printStackTrace(); } } } return false; }
public static boolean writeFile(String content,String target) { File file = new File(target); if (!file.exists()) { try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } }
Writer writer=null; try { writer = new FileWriter(target); writer.write(content); return true; } catch (IOException e) { e.printStackTrace(); }finally { if(writer!=null){ try { writer.close(); } catch (IOException e) { e.printStackTrace(); } } } return false; }
}
|