1 /*
2 * Fsgrep is a simple Java application which allows a user to
3 * search all files in a directory structure for lines matching
4 * a given pattern. Its functionality is a combination of the
5 * Unix 'find' and 'grep' utilities.
6 * Visit [http://fsgrep.sourceforge.net/] for more information.
7 *
8 * Copyright (C) 2003-2006 Murali Krishnan [murali_ca_us@users.sourceforge.net]
9 *
10 * Fsgrep is free software; you can redistribute it and/or modify
11 * it under the terms of version 2 of the GNU General Public
12 * License as published by the Free Software Foundation.
13 *
14 * Fsgrep is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with Fsgrep (see the file named LICENSE.txt); if not, write
21 * to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
22 * Boston, MA 02111-1307 USA
23 */
24
25 package mk.fsgrep.util.output;
26
27
28 import java.io.*;
29
30
31 /***
32 * Send output to a given file.
33 *
34 * @author Murali Krishnan
35 *
36 */
37 public class FileOutput extends OutputDestination {
38
39 //------------------------------------------------------------
40 //- Class Variables
41
42
43
44 //------------------------------------------------------------
45 //- Class Functions
46
47
48
49 //------------------------------------------------------------
50 //- Instance Variables
51
52 private String _filename = "";
53 private FileWriter _fileWriter = null;
54 private PrintWriter _printWriter = null;
55
56
57 //------------------------------------------------------------
58 //- Constructors
59
60 public FileOutput(String filename) {
61 _filename = filename;
62 try {
63 _fileWriter = new FileWriter(filename);
64 _printWriter = new PrintWriter(_fileWriter, true);
65 } catch (IOException ioe) {
66 System.err.println("Could not create output file [" +
67 filename + "].");
68 ioe.printStackTrace();
69 }
70 }
71
72
73 //------------------------------------------------------------
74 //- Accessors
75
76 public String getFilename() {return _filename;}
77 protected FileWriter getFileWriter() {return _fileWriter;}
78 protected PrintWriter getPrintWriter() {return _printWriter;}
79
80
81
82 //------------------------------------------------------------
83 //- Settors
84
85
86
87
88 //------------------------------------------------------------
89 //- Private/Protected Utility Functions
90
91
92
93 //------------------------------------------------------------
94 //- Public Interface Functions
95
96
97
98 //------------------------------------------------------------
99 //- Class Interface Functions
100
101 public void print(String string) {
102 getPrintWriter().print(string);
103 }
104
105 public void flush() {
106 getPrintWriter().flush();
107
108 System.out.println("Results saved to [" + getFilename() + "].");
109 }
110
111
112
113 public void close() {
114 try {
115 getPrintWriter().close();
116 getFileWriter().close();
117 } catch (IOException ioe) {
118 System.err.println("Problem closing output file [" +
119 getFilename() + "].");
120 ioe.printStackTrace();
121 }
122 }
123
124
125
126 //------------------------------------------------------------
127 //- Inner Classes
128
129
130
131 //------------------------------------------------------------
132 //- Main
133
134
135
136 }