Java IO文件流之打印输出流
打印流是输出流OutputStream的子类,可以对OutputStream类对象进行包装设计,增强功能上的方便性;
装饰设计是基于核心实现的包装,使使用更简便和好看;
所有基本的输出操作流的转码、转换操作可以避免;因为该流的方法可以处理所有的类型包括类类型;
他封装了所有底层实现;
对数据的输出(写操作)提供了良好的支持!
类介绍:
public class PrintStream extends FilterOutputStream implements Appendable, Closeable;
A PrintStream adds functionality to another output stream, namely the ability to print representations of various data
values conveniently. Two other features are provided as well. Unlike other output streams, a PrintStream never throws
an IOException; instead, exceptional situations merely set an internal flag that can be tested via the checkError
method. Optionally, a PrintStream can be created so as to flush automatically; this means that the flush method is
automatically invoked after a byte array is written, one of the println methods is invoked, or a newline character or
byte (‘\n’) is written.
All characters printed by a PrintStream are converted into bytes using the platform’s default character encoding. The
PrintWriter class should be used in situations that require writing characters rather than bytes.
构造:
1.public PrintStream(OutputStream out)
Creates a new print stream. This stream will not flush automatically.
Parameters:
out – The output stream to which values and objects will be printed;
2.public PrintStream(OutputStream out,
boolean autoFlush)
Creates a new print stream.
Parameters:
out – The output stream to which values and objects will be printed
autoFlush – A boolean; if true, the output buffer will be flushed whenever a byte array is written, one of the println methods is invoked, or a newline character or byte (‘\n’) is written;
3.public PrintStream(OutputStream out,
boolean autoFlush,
String encoding)
throws UnsupportedEncodingException
Creates a new print stream.
Parameters:
out – The output stream to which values and objects will be printed
autoFlush – A boolean; if true, the output buffer will be flushed whenever a byte array is written, one of the println methods is invoked, or a newline character or byte (‘\n’) is written
encoding – The name of a supported character encoding
Throws:
UnsupportedEncodingException – If the named encoding is not supported;
4.public PrintStream(File file)
throws FileNotFoundException
Creates a new print stream, without automatic line flushing, with the specified file. This convenience constructor creates the necessary intermediate OutputStreamWriter, which will encode characters using the default charset for this instance of the Java virtual machine.
Parameters:
file – The file to use as the destination of this print stream. If the file exists, then it will be truncated to zero size; otherwise, a new file will be created. The output will be written to the file and is buffered.
5.public PrintStream(File file,
String csn)
throws FileNotFoundException,
UnsupportedEncodingException
Creates a new print stream, without automatic line flushing, with the specified file and charset. This convenience constructor creates the necessary intermediate OutputStreamWriter, which will encode characters using the provided charset.
Parameters:
file – The file to use as the destination of this print stream. If the file exists, then it will be truncated to zero size; otherwise, a new file will be created. The output will be written to the file and is buffered.
csn – The name of a supported charset ;
方法集:
1.public void flush()
Flushes the stream. This is done by writing any buffered output bytes to the underlying output stream and then flushing that stream.
2.public void close()
Closes the stream. This is done by flushing the stream and then closing the underlying output stream. ;
3.public void write(int b) // 该方法自动创建文件
Writes the specified byte to this stream. If the byte is a newline and automatic flushing is enabled then the flush method will be invoked.
Note that the byte is written as given; to write a character that will be translated according to the platform’s default character encoding, use the print(char) or println(char) methods.
4. public void write(byte[] buf,
int off,
int len)
Writes len bytes from the specified byte array starting at offset off to this stream. If automatic flushing is enabled then the flush method will be invoked.
5.public void print(…) // 该方法打印7中数据类型:6种基本型、所有类类型;但由于“these bytes are written in exactly the manner of the write(int) method.”,所以,它可以打印short、byte型,因为至少他们都可以转换int;
Prints a value. The string produced by String.valueOf(…) is translated into bytes according to the platform’s default character encoding, and these bytes are written in exactly the manner of the write(int) method.
打印char[]时调用write(int b)方法;
Parameters:
… – boolean,char,int,long,float,double,char[],String,Object;
6.public void println() // 就是打印当前系统相应的分隔符终结当前行,换一新行;
Terminates the current line by writing the line separator string. The line separator string is defined by the system property line.separator, and is not necessarily a single newline character (‘\n’).
7.public void println(…)
Prints a … and then terminate the line. This method behaves as though it invokes print(…) and then println().
Parameters:
… – boolean,char,int,long,float,double,char[],String,Object;
8.public PrintStream printf(String format, Object… args) // 返回的是输出流对象,打印流也是输出流,这是C语言的格式化方法;
A convenience method to write a formatted string to this output stream using the specified format string and arguments.
An invocation of this method of the form out.printf(format, args) behaves in exactly the same way as the invocation
out.format(format, args) ;
9.public PrintStream printf(Locale l, // 显然可以定义显示的语言环境
String format,
Object… args)
A convenience method to write a formatted string to this output stream using the specified format string and arguments.
An invocation of this method of the form out.printf(l, format, args) behaves in exactly the same way as the invocation
out.format(l, format, args)
10.public PrintStream format(String format, // 以本地语言环境打印格式化对象;
Object… args)
Writes a formatted string to this output stream using the specified format string and arguments.
The locale always used is the one returned by Locale.getDefault(), regardless of any previous invocations of other formatting methods on this object;
11.public PrintStream format(Locale l,
String format,
Object… args);
12.public PrintStream append(CharSequence csq) // 可以在当前流上追加字符串;
Appends the specified character sequence to this output stream.
An invocation of this method of the form out.append(csq) behaves in exactly the same way as the invocation
out.print(csq.toString())
13.public PrintStream append(CharSequence csq,
int start,
int end)
Appends a subsequence of the specified character sequence to this output stream.
An invocation of this method of the form out.append(csq, start, end) when csq is not null, behaves in exactly the same way as the invocation
out.print(csq.subSequence(start, end).toString()) ;
14.public PrintStream append(char c)
Appends the specified character to this output stream.
An invocation of this method of the form out.append(c) behaves in exactly the same way as the invocation
out.print(c) ;
—————–简单应用:对OutputStream的装饰设计:
package com.mldn;
import java.io.PrintStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.OutputStream;
import java.io.FileInputStream;
import java.io.IOException;
public class PrintDemo
{
public static void main(String[] args)
{
String path = File.separator + “home” + File.separator + “administrator” + File.separator + “ps.txt”;
String path1 = File.separator + “home” + File.separator + “administrator” + File.separator + “毛峰茶简介”;
OutputStream out = null; // 声明输出流
try
{
out = new FileOutputStream(new File(path), true); // 实例化输出流对象,自动追加
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
PrintStream ps = new PrintStream(out); // 打印流对象
ps.print(“写入内容:” + 1 + ” hello, world!\n”);
ps.println(“任何数据类型:” + 2 + ” ” + 0.2f + ” ” + 2.2 + ” ” + ‘a’);
FileInputStream in = null;
try
{
in = new FileInputStream(new File(path1));
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
try
{
int len = 0, temp = 0;
byte[] bytes = new byte[in.available()];
while ((temp = in.read()) != -1)
{
bytes[len] = (byte)temp;
len++;
}
ps.println(new String(bytes));
}
catch (IOException e)
{
e.printStackTrace();
}
ps.close();
}
}
————-print方法的测试:
package com.mldn;
import java.io.*;
public class PrintlnDemo
{
public static void main(String[] args)
{
short sh = 2;
byte b = (byte)250;
short[] shor = new short[4];
for (short s = 0; s < shor.length; s++)
{
shor[s] = s;
}
String str = “Hello, World!”;
byte[] bytes = str.getBytes();
System.out.print(“打印short + byte:” + sh + b + “\n”);
System.out.print(sh + b + ” 打印short + byte” + “\n”);
for (short s : shor)
{
System.out.println(“打印short:” + s);
}
System.out.println(new String(bytes));
System.out.println(“打印byte:” + b);
for (byte bt : bytes)
{
System.out.println(“打印byte[]:” + bt);
}
}
}
/*
administrator@xu-desktop:~$ java com.mldn.PrintlnDemo
打印short + byte:21 // 说明+的左操数决定是否进行加运算;
3 打印short + byte // 说明+的左操数决定是否进行加运算;显然是按照int处理了;
打印short:0
打印short:1
打印short:2
打印short:3
Hello, World!
打印byte:-6 // 按int处理;
打印byte[]:72 // H
打印byte[]:101 // e
打印byte[]:108 // l
打印byte[]:108 // l
打印byte[]:111 // o
打印byte[]:44 // ,
打印byte[]:32 // ‘ ‘
打印byte[]:87 // W
打印byte[]:111 // o
打印byte[]:114 // r
打印byte[]:108 // l
打印byte[]:100 // d
打印byte[]:33 // !
// 结论:
1.java中+的左操数决定是否进行加运算,和C++一样;
2.打印流的print方法处理byte,short型按int处理并以write(int b)风格进行;
3.print打印byte时,打印每个字节表示的int值,上面的字符串对应的是每个字符的ASCII值;
4.println调用print和println()实现;
5.java中的块作用域不可以屏蔽父作用域,但可以屏蔽同级作用域;而C++中的块可以屏蔽父作用域;
6.java是真正实现有根体系的语言;
*/
声明: 除非转自他站(如有侵权,请联系处理)外,本文采用 BY-NC-SA 协议进行授权 | 嗅谱网
转载请注明:转自《Java IO文件流之打印输出流》
本文地址:http://www.xiupu.net/archives-94.html
关注公众号:
微信赞赏
支付宝赞赏