1 /****************************************************************************
2 * Copyright (c) 2005, 2006, 2007, 2008, 2009 Imola Informatica.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the LGPL License v2.1
5 * which accompanies this distribution, and is available at
6 * http://www.gnu.org/licenses/lgpl.html
7 ****************************************************************************/
8 package it.imolinfo.jbi4corba.jbi.component;
9
10 import it.imolinfo.jbi4corba.Logger;
11 import it.imolinfo.jbi4corba.LoggerFactory;
12 import it.imolinfo.jbi4corba.jbi.Messages;
13
14 import java.io.BufferedReader;
15 import java.io.BufferedWriter;
16 import java.io.File;
17 import java.io.FileNotFoundException;
18 import java.io.FileReader;
19 import java.io.FileWriter;
20 import java.io.IOException;
21 import java.io.Writer;
22
23 /**
24 *
25 * @author graj
26 */
27 public class ReadWriteTextFile {
28
29 // The logger.
30 private static final Logger LOG =
31 LoggerFactory.getLogger(ReadWriteTextFile.class);
32 private static final Messages MESSAGES =
33 Messages.getMessages(ReadWriteTextFile.class);
34
35 /**
36 * Fetch the entire contents of a text file, and return it in a String. This
37 * style of implementation does not throw Exceptions to the caller.
38 *
39 * @param aFile
40 * is a file which already exists and can be read.
41 */
42 static public String getContents(File aFile) {
43
44 // ...checks on aFile are elided
45 StringBuffer contents = new StringBuffer();
46
47 // declared here only to make visible to finally clause
48 BufferedReader input = null;
49 try {
50 // use buffering, reading one line at a time
51 // FileReader always assumes default encoding is OK!
52 input = new BufferedReader(new FileReader(aFile));
53 String line = null; // not declared within while loop
54 /*
55 * readLine is a bit quirky : it returns the content of a line MINUS
56 * the newline. it returns null only for the END of the stream. it
57 * returns an empty String if two newlines appear in a row.
58 */
59 while ((line = input.readLine()) != null) {
60 contents.append(line);
61 contents.append(System.getProperty("line.separator"));
62 }
63 } catch (FileNotFoundException ex) {
64 ex.printStackTrace();
65 } catch (IOException ex) {
66 ex.printStackTrace();
67 } finally {
68 try {
69 if (input != null) {
70 // flush and close both "input" and its underlying
71 // FileReader
72 input.close();
73 }
74 } catch (IOException ex) {
75 ex.printStackTrace();
76 }
77 }
78 return contents.toString();
79 }
80
81 /**
82 * Change the contents of text file in its entirety, overwriting any
83 * existing text.
84 *
85 * This style of implementation throws all exceptions to the caller.
86 *
87 * @param aFile
88 * is an existing file which can be written to.
89 * @throws IllegalArgumentException
90 * if param does not comply.
91 * @throws FileNotFoundException
92 * if the file does not exist.
93 * @throws IOException
94 * if problem encountered during write.
95 */
96 static public void setContents(File aFile, String aContents)
97 throws FileNotFoundException, IOException {
98 if (aFile == null) {
99 String msg=MESSAGES.getString("CRB000226_File_should_not_be_null");
100 LOG.error(msg);
101 throw new IllegalArgumentException(msg);
102
103 }
104 if (!aFile.exists()) {
105 String msg=MESSAGES.getString("CRB000227_File_does_not_exist", new Object[] {aFile});
106 LOG.error(msg);
107 throw new FileNotFoundException(msg);
108 }
109 if (!aFile.isFile()) {
110 String msg=MESSAGES.getString("CRB000228_Should_not_be_a_directory", new Object[] {aFile});
111 LOG.error(msg);
112 throw new IllegalArgumentException(msg);
113 }
114 if (!aFile.canWrite()) {
115 String msg=MESSAGES.getString("CRB000229_File_cannot_be_written", new Object[] {aFile});
116 LOG.error(msg);
117 throw new IllegalArgumentException(msg);
118 }
119
120 // declared here only to make visible to finally clause; generic
121 // reference
122 Writer output = null;
123 try {
124 // use buffering
125 // FileWriter always assumes default encoding is OK!
126 output = new BufferedWriter(new FileWriter(aFile));
127 output.write(aContents);
128 } finally {
129 // flush and close both "output" and its underlying FileWriter
130 if (output != null)
131 output.close();
132 }
133 }
134
135 /**
136 * Simple test harness.
137 */
138 public static void main(String... aArguments) throws IOException {
139 File testFile = new File("/tmp/blah.txt");
140 LOG.debug("Original file contents: " + getContents(testFile));
141 setContents(testFile, "Gopalan says hello.");
142 LOG.debug("New file contents: " + getContents(testFile));
143 }
144 }