打印流PrintStream

打印流PrintStream

PrintStream:

是一个字节打印流,System.out对应的类型就是PrintStream

它的构造函数函数可以接收三种数据类型的值。

1,字符串路径。

2File对象。

3OutputStream

PrintWriter

是一个字符打印流。构造函数可以接收四种类型的值。

1,字符串路径。

2File对象。

对于12类型的数据,还可以指定编码表。也就是字符集。

3OutputStream

4Writer

对于34类型的数据,可以指定自动刷新。

注意:该自动刷新值为true时,只有三个方法可以用:println,printf,format.

//如果想要既有自动刷新,又可执行编码。如何完成流对象的包装?

PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream("a.txt"),"utf-8"),true);

 

//如果想要提高效率。还要使用打印方法。

PrintWriter pw = new PrintWriter(new BufferdWriter(new OutputStreamWriter(new FileOutputStream("a.txt"),"utf-8")),true);


 

import java.io.*;

public class PrintStreamTest {

public static void main(String[] args) throws IOException {

System.out.println((byte)1);

wirteFile1();

wirteFile2();

readFile2();

readFile22();

}

public static void wirteFile1() throws IOException{

DataOutputStream dos = new DataOutputStream(new FileOutputStream(new File("res/pst1.txt")));

dos.writeDouble(3.1415926);

dos.close();

}

public static void wirteFile2() throws IOException{

PrintStream ps = new PrintStream(new FileOutputStream(new File("res/pst2.txt")));

ps.print(3.1415926);

ps.print("北京");

ps.close();

}

public static void readFile2() throws IOException {

FileInputStream fis =new FileInputStream("res/pst2.txt");

int len = fis.available();

byte[] buf = new byte[len];

while(fis.read(buf) != -1){

System.out.println(new String(buf));

}

fis.close();

}

public static void readFile22() throws IOException{

BufferedReader br = new BufferedReader( new FileReader("res/pst2.txt"));

String content = null;

while((content = br.readLine()) != null){

System.out.println(content);

}

br.close();

}

}


 

管道流

n PipedInputStream

n PipedOutputStream

特点:

读取管道流流和写入管道流可以进行连接。

连接方式:

1,通过两个流对象的构造函数。

2,通过两个对象的connect方法。

通常两个流在使用时,需要加入多线程技术,也就是让读写同时运行。

注意;对于read方法。该方法是阻塞式的,也就是没有数据的情况,该方法会等待。

//sender.java 

import java.io.*;

import java.util.*;

public class Sender extends Thread {

PipedOutputStream out = new PipedOutputStream();

public PipedOutputStream getOut() {

return out;

}

public void run() {

String str = "Hello,receiver   !   I`m   sender\n ";

try {

for(int i=0; i<10; i++)

out.write(str.getBytes());

out.close();

} catch (Exception e) {

e.printStackTrace();

}

}

}

//Receiver.java

import java.io.PipedInputStream;

import java.util.*;

public class Receiver extends Thread {

PipedInputStream in = new PipedInputStream();

public PipedInputStream getIn() {

return in;

}

public void run() {

byte[] buf = new byte[1024];

try {

int len = in.read(buf);

System.out.println("the   following   is   from   sender:\n "

+ new String(buf, 0, len));

in.close();

} catch (Exception e) {

e.printStackTrace();

}

}

}

//TestPiped

import java.io.PipedInputStream;

import java.io.PipedOutputStream;

class TestPiped {

public static void main(String[] args) {

Sender s = new Sender();

Receiver r = new Receiver();

PipedOutputStream out = s.getOut();

PipedInputStream in = r.getIn();

try {

in.connect(out);

s.start();

r.start();

} catch (Exception e) {

e.printStackTrace();

}

}

}


 

序列流,也称为合并流——SequenceInputStream:

特点:可以将多个读取流合并成一个流。这样操作起来很方便。

原理:其实就是将每一个读取流对象存储到一个集合中。最后一个流对象结尾作为这个流的结尾。

两个构造函数:

1SequenceInputStream(InputStream in1,InputStream in2)

可以将两个读取流合并成一个流。

2SequenceInputStream(Enumeration<? extends InputStream> en)

可以将枚举中的多个流合并成一个流。

作用:可以用于多个数据的合并。

//将两个文件拼接为一个流进行依次读取

import java.io.*;

public class DataIODemo1 {

public static void main(String[] args) throws IOException {

FileInputStream fis1 = new FileInputStream("res/a.txt");

FileInputStream fis2 = new FileInputStream("res/number.txt");

SequenceInputStream sis = new SequenceInputStream(fis1, fis2);

int ch;

while((ch = sis.read()) != -1){

System.out.print((char)ch);

}

sis.close();

fis1.close();

fis2.close();

}

}


 

注意:因为EnumerationVector中特有的取出方式。而VectorArrayList取代。

所以要使用ArrayList集合效率更高一些。那么如何获取Enumeration呢?

ArrayList<FileInputStream > al = new ArrayList<FileInputStream>();

for(int x=1; x<4; x++)

al.add(new FileInputStream(x+".txt"));

//返回按适当顺序在列表的元素上进行迭代的迭代器。

final Iterator<FileInputStream> it = al.iterator();

Enumeration<FileInputStream> en = new Enumeration<FileInputStream>() {

public boolean hasMoreElements() {

return it.hasNext();

}

public FileInputStream nextElement() {

return it.next();

}

};

//多个流就变成了一个流,这就是数据源。

SequenceInputStream sis = new SequenceInputStream(en);

//创建数据目的。

FileOutputStream fos = new FileOutputStream("4.txt");

byte[] buf = new byte[1024*4];

int len = 0;

while((len=sis.read(buf))!=-1)

{

fos.write(buf,0,len);

}

fos.close();

sis.close();

//如果要一个对文件数据切割。

一个读取对应多了输出。

FileInputStream fis = new FileInputStream("1.mp3");

FileOutputStream fos  = null;

byte[] buf = new byte[1024*1024];//是一个1m的缓冲区。

int len = 0;

int count = 1;

while((len=fis.read(buf))!=-1)

{

fos = new FileOutputStream((count++)+".part");

fos.write(buf,0,len);

fos.close();

}

fis.close();

//这样就是将1.mp3文件切割成多个碎片文件。


 

想要合并使用SequenceInputStream即可。

对于切割后,合并是需要的一些源文件的信息。

可以通过配置文件进行存储。该配置可以通过键=值的形式存在。

然后通过Properties对象进行数据的加载和获取。

操作数组的流对象

1、操作字节数组

u ByteArrayInputStream

u ByteArrayOutputStream

toByteArray();

toString();

writeTo(OutputStream);

2、操作字符数组。

u CharArrayReader

u CharArrayWriter

3、操作字符串

u StringReader

u StringWriter

对于这些流,源是内存。目的也是内存。

而且这些流并未调用系统资源。使用的就是内存中的数组。

所以这些在使用的时候不需要close

操作数组的读取流在构造是,必须要明确一个数据源。所以要传入相对应的数组。

对于操作数组的写入流,在构造函数可以使用空参数。因为它内置了一个可变长度数组作为缓冲区。

这几个流的出现其实就是通过流的读写思想在操作数组。

复合类型数据(比如:姓名、年龄、籍贯、性别等等)

ByteArrayOutputStream bos = new ByteArrayOutputStream();

DataOutputStream dos = new DataOutputStream (bos);

dos.writeUTF(..);

dos.writeInt(..);

dos.writeBoolean(..);

...

byte[] result = bos.toByteArray();

编码转换

io中涉及到编码转换的流是转换流和打印流。

但是打印流只有输出。

在转换流中是可以指定编码表的。

默认情况下,都是本机默认的码表GBK. 这个编码表怎么来的?

System.out.println(System.getProperty("file.encoding"));

常见码表:

ascii:美国标准信息交换码。使用的是1个字节的7位来表示该表中的字符。

ISO8859-1:拉丁码表。使用1个字节来表示。

GB2312:简体中文码表。

GBK:简体中文码表,比GB2312融入更多的中文文件和符号。

unicode:国际标准码表。都用两个字节表示一个字符。

UTF-8:对unicode进行优化,每一个字节都加入了标识头。

编码转换:

字符串 -->字节数组  :编码。通过getBytes(charset);

字节数组-->字符串 : 解码。通过String类的构造函数完成。String(byte[],charset);

如果编对了,解错了,有可能还有救!

import java.io.IOException;

import java.io.UnsupportedEncodingException;

public class CodeTest {

public static void main(String[] args) throws IOException {

String s = "你好";

// 编码。

byte[] b = s.getBytes("GBK");

System.out.println(new String(b, "GBK"));

// 解码。

String s1 = new String(b, "iso8859-1");

System.out.println(s1);// ????

// 想要还原。

/*

 * 对s1先进行一次解码码表的编码。获取原字节数据。 然后在对原字节数据进行指定编码表的解码。

 */

byte[] b1 = s1.getBytes("iso8859-1");

System.out.println(new String(b1, "GBK"));

}

}


 

这种情况在tomcat服务器会出现。

因为tomcat服务器默认是iso8859-1的编码表。

所以客户端通过浏览器向服务前通过get提交方式提交中文数据时,

服务端获取到会使用ISO8859-1进行中文数据的解码。会出现乱码。

这时就必须要对获取的数据进行iso8859-1编码。然后在按照页面指定的编码表进行解码。即可

而对于post提交,这种方法也通用。但是post有更好的解决方式。

request.setCharacterEncoding("utf-8");即可。

所以建立客户端提交使用post提交方式。

 

你可能感兴趣的:(exception,String,vector,Class,import,byte)