KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > snapper > utils > ReadWriteTextFile


1 package org.enhydra.snapper.utils;
2
3 import java.io.*;
4
5 public class ReadWriteTextFile {
6     
7
8   /**
9   * Fetch the entire contents of a text file, and return it in a String.
10   * This style of implementation does not throw Exceptions to the caller.
11   *
12   * @param aFile is a file which already exists and can be read.
13   */

14   static public String JavaDoc getContents(File aFile) {
15     //...checks on aFile are elided
16
StringBuffer JavaDoc contents = new StringBuffer JavaDoc();
17
18     //declared here only to make visible to finally clause
19
BufferedReader input = null;
20     try {
21       //use buffering
22
//this implementation reads one line at a time
23
input = new BufferedReader( new FileReader(aFile) );
24       String JavaDoc line = null; //not declared within while loop
25
while (( line = input.readLine()) != null){
26         contents.append(line);
27         contents.append(System.getProperty("line.separator"));
28       }
29     }
30     catch (FileNotFoundException ex) {
31       ex.printStackTrace();
32     }
33     catch (IOException ex){
34       ex.printStackTrace();
35     }
36     finally {
37       try {
38         if (input!= null) {
39           //flush and close both "input" and its underlying FileReader
40
input.close();
41         }
42       }
43       catch (IOException ex) {
44         ex.printStackTrace();
45       }
46     }
47     return contents.toString();
48   }
49
50   /**
51   * Change the contents of text file in its entirety, overwriting any
52   * existing text.
53   *
54   * This style of implementation throws all exceptions to the caller.
55   *
56   * @param aFile is an existing file which can be written to.
57   * @throws IllegalArgumentException if param does not comply.
58   * @throws FileNotFoundException if the file does not exist.
59   * @throws IOException if problem encountered during write.
60   */

61   static public void setContents(File aFile, String JavaDoc aContents)
62                                  throws FileNotFoundException, IOException {
63     if (aFile == null) {
64       throw new IllegalArgumentException JavaDoc("File should not be null.");
65     }
66     if (!aFile.exists()) {
67       throw new FileNotFoundException ("File does not exist: " + aFile);
68     }
69     if (!aFile.isFile()) {
70       throw new IllegalArgumentException JavaDoc("Should not be a directory: " + aFile);
71     }
72     if (!aFile.canWrite()) {
73       throw new IllegalArgumentException JavaDoc("File cannot be written: " + aFile);
74     }
75
76     //declared here only to make visible to finally clause; generic reference
77
Writer output = null;
78     try {
79       //use buffering
80
output = new BufferedWriter( new FileWriter(aFile) );
81       output.write( aContents );
82     }
83     finally {
84       //flush and close both "output" and its underlying FileWriter
85
if (output != null) output.close();
86     }
87   }
88
89   public static void main ( String JavaDoc[] aArguments ) throws IOException {
90     File testFile = new File("C:\\Temp\\blah.txt");
91     setContents(testFile, "blah blah blah");
92
93     System.out.println( "File contents: " + getContents(testFile) );
94   }
95 }
Popular Tags