PageRenderTime 24ms CodeModel.GetById 14ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/hudson-core/src/main/java/hudson/util/TextFile.java

http://github.com/hudson/hudson
Java | 95 lines | 50 code | 9 blank | 36 comment | 0 complexity | 7ae70602adf95a7095653d8a034d4fbd MD5 | raw file
 1/*
 2 * The MIT License
 3 * 
 4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi
 5 * 
 6 * Permission is hereby granted, free of charge, to any person obtaining a copy
 7 * of this software and associated documentation files (the "Software"), to deal
 8 * in the Software without restriction, including without limitation the rights
 9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 * 
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 * 
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24package hudson.util;
25
26import java.io.BufferedReader;
27import java.io.File;
28import java.io.FileInputStream;
29import java.io.IOException;
30import java.io.InputStreamReader;
31import java.io.PrintWriter;
32import java.io.StringWriter;
33
34/**
35 * Represents a text file.
36 *
37 * Provides convenience methods for reading and writing to it.
38 *
39 * @author Kohsuke Kawaguchi
40 */
41public class TextFile {
42    public final File file;
43
44    public TextFile(File file) {
45        this.file = file;
46    }
47
48    public boolean exists() {
49        return file.exists();
50    }
51
52    public void delete() {
53        file.delete();
54    }
55
56    /**
57     * Reads the entire contents and returns it.
58     */
59    public String read() throws IOException {
60        StringWriter out = new StringWriter();
61        PrintWriter w = new PrintWriter(out);
62        BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file),"UTF-8"));
63        try {
64            String line;
65            while((line=in.readLine())!=null)
66                w.println(line);
67        } finally{
68            in.close();
69        }
70        return out.toString();
71    }
72
73    /**
74     * Overwrites the file by the given string.
75     */
76    public void write(String text) throws IOException {
77        file.getParentFile().mkdirs();
78        AtomicFileWriter w = new AtomicFileWriter(file);
79        try {
80            w.write(text);
81            w.commit();
82        } finally {
83            w.abort();
84        }
85    }
86
87    public String readTrim() throws IOException {
88        return read().trim();
89    }
90
91    @Override
92    public String toString() {
93        return file.toString();
94    }
95}