/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. */
  24. package hudson.util;
  25. import java.io.BufferedReader;
  26. import java.io.File;
  27. import java.io.FileInputStream;
  28. import java.io.IOException;
  29. import java.io.InputStreamReader;
  30. import java.io.PrintWriter;
  31. import java.io.StringWriter;
  32. /**
  33. * Represents a text file.
  34. *
  35. * Provides convenience methods for reading and writing to it.
  36. *
  37. * @author Kohsuke Kawaguchi
  38. */
  39. public class TextFile {
  40. public final File file;
  41. public TextFile(File file) {
  42. this.file = file;
  43. }
  44. public boolean exists() {
  45. return file.exists();
  46. }
  47. public void delete() {
  48. file.delete();
  49. }
  50. /**
  51. * Reads the entire contents and returns it.
  52. */
  53. public String read() throws IOException {
  54. StringWriter out = new StringWriter();
  55. PrintWriter w = new PrintWriter(out);
  56. BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file),"UTF-8"));
  57. try {
  58. String line;
  59. while((line=in.readLine())!=null)
  60. w.println(line);
  61. } finally{
  62. in.close();
  63. }
  64. return out.toString();
  65. }
  66. /**
  67. * Overwrites the file by the given string.
  68. */
  69. public void write(String text) throws IOException {
  70. file.getParentFile().mkdirs();
  71. AtomicFileWriter w = new AtomicFileWriter(file);
  72. try {
  73. w.write(text);
  74. w.commit();
  75. } finally {
  76. w.abort();
  77. }
  78. }
  79. public String readTrim() throws IOException {
  80. return read().trim();
  81. }
  82. @Override
  83. public String toString() {
  84. return file.toString();
  85. }
  86. }