算法Sedgewick第四版-第1章基础-004一封装输出(文件)

1.

  1 package algorithms.util;
  2
  3 /******************************************************************************
  4  *  Compilation:  javac Out.java
  5  *  Execution:    java Out
  6  *  Dependencies: none
  7  *
  8  *  Writes data of various types to: stdout, file, or socket.
  9  *
 10  ******************************************************************************/
 11
 12
 13 import java.io.FileOutputStream;
 14 import java.io.IOException;
 15 import java.io.OutputStream;
 16 import java.io.OutputStreamWriter;
 17 import java.io.PrintWriter;
 18 import java.net.Socket;
 19 import java.util.Locale;
 20
 21 /**
 22  *  This class provides methods for writing strings and numbers to
 23  *  various output streams, including standard output, file, and sockets.
 24  *  <p>
 25  *  For additional documentation, see
 26  *  <a href="http://introcs.cs.princeton.edu/31datatype">Section 3.1</a> of
 27  *  <i>Introduction to Programming in Java: An Interdisciplinary Approach</i>
 28  *  by Robert Sedgewick and Kevin Wayne.
 29  *
 30  *  @author Robert Sedgewick
 31  *  @author Kevin Wayne
 32  */
 33 public class Out {
 34
 35     // force Unicode UTF-8 encoding; otherwise it‘s system dependent
 36     private static final String CHARSET_NAME = "UTF-8";
 37
 38     // assume language = English, country = US for consistency with In
 39     private static final Locale LOCALE = Locale.US;
 40
 41     private PrintWriter out;
 42
 43    /**
 44      * Initializes an output stream from a {@link OutputStream}.
 45      *
 46      * @param  os the <tt>OutputStream</tt>
 47      */
 48     public Out(OutputStream os) {
 49         try {
 50             OutputStreamWriter osw = new OutputStreamWriter(os, CHARSET_NAME);
 51             out = new PrintWriter(osw, true);
 52         }
 53         catch (IOException e) {
 54             e.printStackTrace();
 55         }
 56     }
 57
 58    /**
 59      * Initializes an output stream from standard output.
 60      */
 61     public Out() {
 62         this(System.out);
 63     }
 64
 65    /**
 66      * Initializes an output stream from a socket.
 67      *
 68      * @param  socket the socket
 69      */
 70     public Out(Socket socket) {
 71         try {
 72             OutputStream os = socket.getOutputStream();
 73             OutputStreamWriter osw = new OutputStreamWriter(os, CHARSET_NAME);
 74             out = new PrintWriter(osw, true);
 75         }
 76         catch (IOException e) {
 77             e.printStackTrace();
 78         }
 79     }
 80
 81    /**
 82      * Initializes an output stream from a file.
 83      *
 84      * @param  filename the name of the file
 85      */
 86     public Out(String filename) {
 87         try {
 88             OutputStream os = new FileOutputStream(filename);
 89             OutputStreamWriter osw = new OutputStreamWriter(os, CHARSET_NAME);
 90             out = new PrintWriter(osw, true);
 91         }
 92         catch (IOException e) {
 93             e.printStackTrace();
 94         }
 95     }
 96
 97    /**
 98      * Closes the output stream.
 99      */
100     public void close() {
101         out.close();
102     }
103
104    /**
105      * Terminates the current line by printing the line-separator string.
106      */
107     public void println() {
108         out.println();
109     }
110
111    /**
112      * Prints an object to this output stream and then terminates the line.
113      *
114      * @param x the object to print
115      */
116     public void println(Object x) {
117         out.println(x);
118     }
119
120    /**
121      * Prints a boolean to this output stream and then terminates the line.
122      *
123      * @param x the boolean to print
124      */
125     public void println(boolean x) {
126         out.println(x);
127     }
128
129    /**
130      * Prints a character to this output stream and then terminates the line.
131      *
132      * @param x the character to print
133      */
134     public void println(char x) {
135         out.println(x);
136     }
137
138    /**
139      * Prints a double to this output stream and then terminates the line.
140      *
141      * @param x the double to print
142      */
143     public void println(double x) {
144         out.println(x);
145     }
146
147    /**
148      * Prints a float to this output stream and then terminates the line.
149      *
150      * @param x the float to print
151      */
152     public void println(float x) {
153         out.println(x);
154     }
155
156    /**
157      * Prints an integer to this output stream and then terminates the line.
158      *
159      * @param x the integer to print
160      */
161     public void println(int x) {
162         out.println(x);
163     }
164
165    /**
166      * Prints a long to this output stream and then terminates the line.
167      *
168      * @param x the long to print
169      */
170     public void println(long x) {
171         out.println(x);
172     }
173
174    /**
175      * Prints a byte to this output stream and then terminates the line.
176      * <p>
177      * To write binary data, see {@link BinaryOut}.
178      *
179      * @param x the byte to print
180      */
181     public void println(byte x) {
182         out.println(x);
183     }
184
185
186
187    /**
188      * Flushes this output stream.
189      */
190     public void print() {
191         out.flush();
192     }
193
194    /**
195      * Prints an object to this output stream and flushes this output stream.
196      *
197      * @param x the object to print
198      */
199     public void print(Object x) {
200         out.print(x);
201         out.flush();
202     }
203
204    /**
205      * Prints a boolean to this output stream and flushes this output stream.
206      *
207      * @param x the boolean to print
208      */
209     public void print(boolean x) {
210         out.print(x);
211         out.flush();
212     }
213
214    /**
215      * Prints a character to this output stream and flushes this output stream.
216      *
217      * @param x the character to print
218      */
219     public void print(char x) {
220         out.print(x);
221         out.flush();
222     }
223
224    /**
225      * Prints a double to this output stream and flushes this output stream.
226      *
227      * @param x the double to print
228      */
229     public void print(double x) {
230         out.print(x);
231         out.flush();
232     }
233
234    /**
235      * Prints a float to this output stream and flushes this output stream.
236      *
237      * @param x the float to print
238      */
239     public void print(float x) {
240         out.print(x);
241         out.flush();
242     }
243
244    /**
245      * Prints an integer to this output stream and flushes this output stream.
246      *
247      * @param x the integer to print
248      */
249     public void print(int x) {
250         out.print(x);
251         out.flush();
252     }
253
254    /**
255      * Prints a long integer to this output stream and flushes this output stream.
256      *
257      * @param x the long integer to print
258      */
259     public void print(long x) {
260         out.print(x);
261         out.flush();
262     }
263
264    /**
265      * Prints a byte to this output stream and flushes this output stream.
266      *
267      * @param x the byte to print
268      */
269     public void print(byte x) {
270         out.print(x);
271         out.flush();
272     }
273
274    /**
275      * Prints a formatted string to this output stream, using the specified format
276      * string and arguments, and then flushes this output stream.
277      *
278      * @param format the format string
279      * @param args   the arguments accompanying the format string
280      */
281     public void printf(String format, Object... args) {
282         out.printf(LOCALE, format, args);
283         out.flush();
284     }
285
286    /**
287      * Prints a formatted string to this output stream, using the specified
288      * locale, format string, and arguments, and then flushes this output stream.
289      *
290      * @param locale the locale
291      * @param format the format string
292      * @param args   the arguments accompanying the format string
293      */
294     public void printf(Locale locale, String format, Object... args) {
295         out.printf(locale, format, args);
296         out.flush();
297     }
298
299
300    /**
301      * A test client.
302      */
303     public static void main(String[] args) {
304         Out out;
305
306         // write to stdout
307         out = new Out();
308         out.println("Test 1");
309         out.close();
310
311         // write to a file
312         out = new Out("test.txt");
313         out.println("Test 2");
314         out.close();
315     }
316
317 }
时间: 2024-10-27 17:38:39

算法Sedgewick第四版-第1章基础-004一封装输出(文件)的相关文章

算法Sedgewick第四版-第1章基础-004一封装输入(可以文件,jar包里的文件或网址)

1. 1 package algorithms.util; 2 3 /****************************************************************************** 4 * Compilation: javac In.java 5 * Execution: java In (basic test --- see source for required files) 6 * Dependencies: none 7 * 8 * Read

算法Sedgewick第四版-第1章基础-003一封装交易对象

1. 1 package ADT; 2 3 /****************************************************************************** 4 * Compilation: javac Transaction.java 5 * Execution: java Transaction 6 * Dependencies: StdOut.java 7 * 8 * Data type for commercial transactions.

算法Sedgewick第四版-第1章基础-001递归

一. 方法可以调用自己(如果你对递归概念感到奇怪,请完成练习 1.1.16 到练习 1.1.22).例如,下面给出了 BinarySearch 的 rank() 方法的另一种实现.我们会经常使用递归,因为递归代码比相应的非递归代码更加简洁优雅.易懂.下面这种实现中的注释就言简意赅地说明了代码的作用.我们可以用数学归纳法证明这段注释所解释的算法的正确性.我们会在 3.1 节中展开这个话题并为二分查找提供一个这样的证明.编写递归代码时最重要的有以下三点.? 递归总有一个最简单的情况——方法的第一条语

算法Sedgewick第四版-第1章基础-008一用数组实现固定长度的栈

1 package algorithms.ADT; 2 3 /****************************************************************************** 4 * Compilation: javac FixedCapacityStackOfStrings.java 5 * Execution: java FixedCapacityStackOfStrings 6 * Dependencies: StdIn.java StdOut.

算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-005计测试算法

1. 1 package algorithms.analysis14; 2 3 import algorithms.util.StdOut; 4 import algorithms.util.StdRandom; 5 6 /****************************************************************************** 7 * Compilation: javac DoublingTest.java 8 * Execution: jav

算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-002插入排序法(Insertion sort)

一.介绍 1.时间和空间复杂度 运行过程 2.特点: (1)对于已排序或接近排好的数据,速度很快 (2)对于部分排好序的输入,速度快 二.代码 1 package algorithms.elementary21; 2 3 /****************************************************************************** 4 * Compilation: javac Insertion.java 5 * Execution: java I

算法Sedgewick第四版-第1章基础-018一解决不能声明泛型数组的两咱方法(强转或反射)

1. 1 /****************************************************************************** 2 * Compilation: javac ResizingArrayStackWithReflection.java 3 * Execution: java ResizingArrayStackWithReflection < input.txt 4 * Dependencies: StdIn.java StdOut.jav

算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-007归并排序(自下而上)

一. 1. 2. 3. 二.代码 1 package algorithms.mergesort22; 2 3 import algorithms.util.StdIn; 4 import algorithms.util.StdOut; 5 6 /****************************************************************************** 7 * Compilation: javac MergeBU.java 8 * Executio

算法Sedgewick第四版-第1章基础-1.3Bags, Queues, and Stacks-001可变在小的

1. package algorithms.stacks13; /****************************************************************************** * Compilation: javac ResizingArrayBag.java * Execution: java ResizingArrayBag * Dependencies: StdIn.java StdOut.java * * Bag implementatio