1 package hudson.util; 2 3 import java.io.BufferedReader ; 4 import java.io.File ; 5 import java.io.FileInputStream ; 6 import java.io.IOException ; 7 import java.io.InputStreamReader ; 8 import java.io.PrintWriter ; 9 import java.io.StringWriter ; 10 11 18 public class TextFile { 19 private final File file; 20 21 public TextFile(File file) { 22 this.file = file; 23 } 24 25 public boolean exists() { 26 return file.exists(); 27 } 28 29 32 public String read() throws IOException { 33 StringWriter out = new StringWriter (); 34 PrintWriter w = new PrintWriter (out); 35 BufferedReader in = new BufferedReader (new InputStreamReader (new FileInputStream (file),"UTF-8")); 36 try { 37 String line; 38 while((line=in.readLine())!=null) 39 w.println(line); 40 } finally{ 41 in.close(); 42 } 43 return out.toString(); 44 } 45 46 49 public void write(String text) throws IOException { 50 AtomicFileWriter w = new AtomicFileWriter(file); 51 w.write(text); 52 w.commit(); 53 } 54 55 public String readTrim() throws IOException { 56 return read().trim(); 57 } 58 59 public String toString() { 60 return file.toString(); 61 } 62 } 63 | Popular Tags |