1.

 package algorithms.util;

 /******************************************************************************
* Compilation: javac Out.java
* Execution: java Out
* Dependencies: none
*
* Writes data of various types to: stdout, file, or socket.
*
******************************************************************************/ import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Locale; /**
* This class provides methods for writing strings and numbers to
* various output streams, including standard output, file, and sockets.
* <p>
* For additional documentation, see
* <a href="http://introcs.cs.princeton.edu/31datatype">Section 3.1</a> of
* <i>Introduction to Programming in Java: An Interdisciplinary Approach</i>
* by Robert Sedgewick and Kevin Wayne.
*
* @author Robert Sedgewick
* @author Kevin Wayne
*/
public class Out { // force Unicode UTF-8 encoding; otherwise it's system dependent
private static final String CHARSET_NAME = "UTF-8"; // assume language = English, country = US for consistency with In
private static final Locale LOCALE = Locale.US; private PrintWriter out; /**
* Initializes an output stream from a {@link OutputStream}.
*
* @param os the <tt>OutputStream</tt>
*/
public Out(OutputStream os) {
try {
OutputStreamWriter osw = new OutputStreamWriter(os, CHARSET_NAME);
out = new PrintWriter(osw, true);
}
catch (IOException e) {
e.printStackTrace();
}
} /**
* Initializes an output stream from standard output.
*/
public Out() {
this(System.out);
} /**
* Initializes an output stream from a socket.
*
* @param socket the socket
*/
public Out(Socket socket) {
try {
OutputStream os = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os, CHARSET_NAME);
out = new PrintWriter(osw, true);
}
catch (IOException e) {
e.printStackTrace();
}
} /**
* Initializes an output stream from a file.
*
* @param filename the name of the file
*/
public Out(String filename) {
try {
OutputStream os = new FileOutputStream(filename);
OutputStreamWriter osw = new OutputStreamWriter(os, CHARSET_NAME);
out = new PrintWriter(osw, true);
}
catch (IOException e) {
e.printStackTrace();
}
} /**
* Closes the output stream.
*/
public void close() {
out.close();
} /**
* Terminates the current line by printing the line-separator string.
*/
public void println() {
out.println();
} /**
* Prints an object to this output stream and then terminates the line.
*
* @param x the object to print
*/
public void println(Object x) {
out.println(x);
} /**
* Prints a boolean to this output stream and then terminates the line.
*
* @param x the boolean to print
*/
public void println(boolean x) {
out.println(x);
} /**
* Prints a character to this output stream and then terminates the line.
*
* @param x the character to print
*/
public void println(char x) {
out.println(x);
} /**
* Prints a double to this output stream and then terminates the line.
*
* @param x the double to print
*/
public void println(double x) {
out.println(x);
} /**
* Prints a float to this output stream and then terminates the line.
*
* @param x the float to print
*/
public void println(float x) {
out.println(x);
} /**
* Prints an integer to this output stream and then terminates the line.
*
* @param x the integer to print
*/
public void println(int x) {
out.println(x);
} /**
* Prints a long to this output stream and then terminates the line.
*
* @param x the long to print
*/
public void println(long x) {
out.println(x);
} /**
* Prints a byte to this output stream and then terminates the line.
* <p>
* To write binary data, see {@link BinaryOut}.
*
* @param x the byte to print
*/
public void println(byte x) {
out.println(x);
} /**
* Flushes this output stream.
*/
public void print() {
out.flush();
} /**
* Prints an object to this output stream and flushes this output stream.
*
* @param x the object to print
*/
public void print(Object x) {
out.print(x);
out.flush();
} /**
* Prints a boolean to this output stream and flushes this output stream.
*
* @param x the boolean to print
*/
public void print(boolean x) {
out.print(x);
out.flush();
} /**
* Prints a character to this output stream and flushes this output stream.
*
* @param x the character to print
*/
public void print(char x) {
out.print(x);
out.flush();
} /**
* Prints a double to this output stream and flushes this output stream.
*
* @param x the double to print
*/
public void print(double x) {
out.print(x);
out.flush();
} /**
* Prints a float to this output stream and flushes this output stream.
*
* @param x the float to print
*/
public void print(float x) {
out.print(x);
out.flush();
} /**
* Prints an integer to this output stream and flushes this output stream.
*
* @param x the integer to print
*/
public void print(int x) {
out.print(x);
out.flush();
} /**
* Prints a long integer to this output stream and flushes this output stream.
*
* @param x the long integer to print
*/
public void print(long x) {
out.print(x);
out.flush();
} /**
* Prints a byte to this output stream and flushes this output stream.
*
* @param x the byte to print
*/
public void print(byte x) {
out.print(x);
out.flush();
} /**
* Prints a formatted string to this output stream, using the specified format
* string and arguments, and then flushes this output stream.
*
* @param format the format string
* @param args the arguments accompanying the format string
*/
public void printf(String format, Object... args) {
out.printf(LOCALE, format, args);
out.flush();
} /**
* Prints a formatted string to this output stream, using the specified
* locale, format string, and arguments, and then flushes this output stream.
*
* @param locale the locale
* @param format the format string
* @param args the arguments accompanying the format string
*/
public void printf(Locale locale, String format, Object... args) {
out.printf(locale, format, args);
out.flush();
} /**
* A test client.
*/
public static void main(String[] args) {
Out out; // write to stdout
out = new Out();
out.println("Test 1");
out.close(); // write to a file
out = new Out("test.txt");
out.println("Test 2");
out.close();
} }

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

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

    1. package algorithms.util; /*********************************************************************** ...

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

    1. package ADT; /****************************************************************************** * Co ...

  3. 算法Sedgewick第四版-第1章基础-003一封装日期

    1. package ADT; import algorithms.util.StdOut; /**************************************************** ...

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

    一. 方法可以调用自己(如果你对递归概念感到奇怪,请完成练习 1.1.16 到练习 1.1.22).例如,下面给出了 BinarySearch 的 rank() 方法的另一种实现.我们会经常使用递归, ...

  5. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-001选择排序法(Selection sort)

    一.介绍 1.算法的时间和空间间复杂度 2.特点 Running time is insensitive to input. The process of finding the smallest i ...

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

    一. 1. 2. 3. 二.代码 package algorithms.mergesort22; import algorithms.util.StdIn; import algorithms.uti ...

  7. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-006归并排序(Mergesort)

    一. 1.特点 (1)merge-sort : to sort an array, divide it into two halves, sort the two halves (recursivel ...

  8. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-005插入排序的改进版

    package algorithms.elementary21; import algorithms.util.StdIn; import algorithms.util.StdOut; /***** ...

  9. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-004希尔排序法(Shell Sort)

    一.介绍 1.希尔排序的思路:希尔排序是插入排序的改进.当输入的数据,顺序是很乱时,插入排序会产生大量的交换元素的操作,比如array[n]的最小的元素在最后,则要经过n-1次交换才能排到第一位,因为 ...

随机推荐

  1. LeetCode OJ:Contains Duplicate(是否包含重复)

    Given an array of integers, find if the array contains any duplicates. Your function should return t ...

  2. python二分查找模块bisect

    bisect模块用于二分查找,非常方便. Bisect模块提供的函数有: 1.查找 bisect.bisect_left(a,x, lo=0, hi=len(a)) : 查找在有序列表a中插入x的in ...

  3. SVN的安装和使用

    1.安装 下载SVN,一直默认安装 安装成功后,配置环境变量path=C:\Program Files\TortoiseSVN\bin 验证SVN安装是否成功:adb -help 或 adb -ver ...

  4. HihoCoder1070 区间最小值(简单线段树)

    个测试点(输入文件)有且仅有一组测试数据. 每组测试数据的第1行为一个整数N,意义如前文所述. 每组测试数据的第2行为N个整数,分别描述每种商品的重量,其中第i个整数表示标号为i的商品的重量weigh ...

  5. 如何加快Json 序列化?有哪些方法?

    1.使用阿里的fastjson 2.可以通过去除不必要属性加快序列化.如person对象,有id,name,address,我json需要用户姓名,此时序列化的时候就只序列化name,id和addre ...

  6. mvc那些事

    mvc的特点: 1.无控件,有HtmlHelper类,此类提供了各种生成html控件的方法.如果不能满足需要,就自定义扩展吧,比如说分页显示.HtmlHelper类提供了Partial(加载局部视图) ...

  7. Tomcat 工作原理 1 (转)

    Tomcat 系统架构与设计模式,第 1 部分: 工作原理 这个分为两个部分的系列文章将研究 Apache Tomcat 的系统架构以及其运用的很多经典设计模式.本文是第 1 部分,将主要从 Tomc ...

  8. Python collections系列之有序字典

    有序字典(orderedDict ) orderdDict是对字典类型的补充,他记住了字典元素添加的顺序 1.创建一个有序字典 import collections dic = collections ...

  9. boost_1.61.0编译安装

    1.下载源码boost_1_61_0.zip 2.进入目录 C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\Tools\Shor ...

  10. Python内置:items()方法

    文章转载于:https://www.cnblogs.com/wushuaishuai/p/7738118.html(博主:IT技术随笔) #Python3中已取消iteritems()方法 描述 Py ...