算法Sedgewick第四版-第1章基础-006一封装输出(文件)
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一封装输出(文件)的更多相关文章
- 算法Sedgewick第四版-第1章基础-005一封装输入(可以文件,jar包里的文件或网址)
1. package algorithms.util; /*********************************************************************** ...
- 算法Sedgewick第四版-第1章基础-004一封装交易对象
1. package ADT; /****************************************************************************** * Co ...
- 算法Sedgewick第四版-第1章基础-003一封装日期
1. package ADT; import algorithms.util.StdOut; /**************************************************** ...
- 算法Sedgewick第四版-第1章基础-001递归
一. 方法可以调用自己(如果你对递归概念感到奇怪,请完成练习 1.1.16 到练习 1.1.22).例如,下面给出了 BinarySearch 的 rank() 方法的另一种实现.我们会经常使用递归, ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-001选择排序法(Selection sort)
一.介绍 1.算法的时间和空间间复杂度 2.特点 Running time is insensitive to input. The process of finding the smallest i ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-007归并排序(自下而上)
一. 1. 2. 3. 二.代码 package algorithms.mergesort22; import algorithms.util.StdIn; import algorithms.uti ...
- 算法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 ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-005插入排序的改进版
package algorithms.elementary21; import algorithms.util.StdIn; import algorithms.util.StdOut; /***** ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-004希尔排序法(Shell Sort)
一.介绍 1.希尔排序的思路:希尔排序是插入排序的改进.当输入的数据,顺序是很乱时,插入排序会产生大量的交换元素的操作,比如array[n]的最小的元素在最后,则要经过n-1次交换才能排到第一位,因为 ...
随机推荐
- 面试题46:求1+2+...+n
题目:求1+2+...+n,要求不能使用乘除法.for.while.if.else.swithc.case等关键字及条件判断语句(A?B:C). 解法一:利用构造函数求解 class Temp { p ...
- commons-dbcp连接池的使用
数据库连接池 池参数(所有池参数都有默认值): 初始大小: 最小空闲连接数: 增量:一次创建的最小单位(5个) 最大空闲连接数:12个 最大连接数:20个 最大的等待时间:1000毫秒 四大连接参数: ...
- shell编程-条件判断与流程控制
1.条件判断式 按照文件类型进行判断: 两种判断格式: test -e /root/install.log [ -e /root/install.log ] 判断命令是否正确执行: [ -d /roo ...
- 2018-2019-2 20165210《网络对抗技术》Exp6 信息搜集与漏洞扫描
2018-2019-2 20165210<网络对抗技术>Exp6 信息搜集与漏洞扫描 一.实验目标: 掌握信息搜集的最基础技能与常用工具的使用方法. 二.实验内容: 各种搜索技巧的应用 G ...
- memcache内存分配问题
Memcached是一个高效的分布式内存cache,了解memcached的内存管理机制,便于我们理解memcached,让我们可以针对我们数据特点进行调优,让其更好的为我所用.这里简单谈一下我对me ...
- mybatis与oracle使用总结
Oracle使用总结 1.新建表删除表 新建表语句: CREATE TABLE +表名{ } create table AFA_USER ( USER_ID VARCHAR2() not null, ...
- xmind的使用及入门
初识 xmind是什么 比较 入门 下载 基本操作 主界面 美化 工具 导出 初识 xmind是什么 说白了就是传说中的思维导图,用它我们可以画出下面这些图: 额,这张图好丑. 好吧,换一张: 他就是 ...
- 关于一种fastjson的死循环情况记录
最近在一次项目中,使用fastjson做接口转换中,碰到了一个Stack Overflow.发现在getxxx方法内如果再次嵌套使用fastjson作json转换,就会无限循环. 错误实例: clas ...
- Hybrid App混合模式移动应用开发(AngularJS+Cordova+Ionic)
以前公司开发了某手机APP是通过jquerymobile来实现的,发现它对手机上的原生设备无能为力.于是在下一个项目到来之际,通过筛选最终决定使用cordova+Ionic.看起来简单,但是因为他们各 ...
- MySQL 备份脚本--mysqldump在slave 上进行备份
MySQL 备份脚本--mysqldump在slave 上进行备份 使用mysqldump在slave上进行备份,建议使用stop slave sql_thread,start slave sql_t ...