1 14 15 package org.quickserver.util; 16 17 import java.io.*; 18 import java.util.*; 19 20 24 public class TextFile extends ArrayList { 25 26 29 public static String read(String fileName) throws IOException { 30 File file = new File(fileName); 31 return read(file); 32 } 33 34 37 public static String read(File fileName) throws IOException { 38 StringBuffer sb = new StringBuffer (); 39 BufferedReader in = null; 40 try { 41 in = new BufferedReader(new FileReader(fileName)); 42 String s; 43 while((s = in.readLine()) != null) { 44 sb.append(s); 45 sb.append("\n"); 46 } 47 } finally { 48 if(in!=null) in.close(); 49 } 50 return sb.toString(); 51 } 52 53 56 public static void write(String fileName, String text) throws IOException { 57 File file = new File(fileName); 58 write(file, text); 59 } 60 61 64 public static void write(File file, String text) throws IOException { 65 PrintWriter out = null; 66 try { 67 out = new PrintWriter( 68 new BufferedWriter(new FileWriter(file, false))); 69 out.print(text); 70 } finally { 71 if(out!=null) out.close(); 72 } 73 } 74 75 public TextFile(String fileName) throws IOException { 76 super(Arrays.asList(read(fileName).split("\n"))); 77 } 78 79 82 public void write(String fileName) throws IOException { 83 PrintWriter out = null; 84 try { 85 out = new PrintWriter( 86 new BufferedWriter(new FileWriter(fileName))); 87 for(int i = 0; i < size(); i++) 88 out.println(get(i)); 89 } finally { 90 if(out!=null) out.close(); 91 } 92 } 93 94 97 public static String read(String fileName, Object parent) 98 throws IOException { 99 StringBuffer sb = new StringBuffer (); 100 InputStream is = null; 101 BufferedReader in = null; 102 try { 103 is = parent.getClass().getResourceAsStream(fileName); 104 in = new BufferedReader(new InputStreamReader(is)); 105 String s; 106 while((s = in.readLine()) != null) { 107 sb.append(s); 108 sb.append("\n"); 109 } 110 } finally { 111 if(is!=null) is.close(); 112 if(in!=null) in.close(); 113 } 114 return sb.toString(); 115 } 116 } 117 | Popular Tags |