打印流PrintStream

本文深入探讨了在信息技术领域中几种关键的流技术,包括打印流、管道流、序列流以及操作数组的流,详细解释了它们的功能、用法及应用场景。重点介绍了PrintStream、PrintWriter的使用方法,管道流的实现过程,序列流的作用,以及操作数组流的特性。此外,还讨论了编码转换的概念和方法,强调了在不同编码环境下进行数据传输的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

打印流PrintStream

PrintStream:

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

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

1,字符串路径。

2,File对象。

3,OutputStream

PrintWriter:

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

1,字符串路径。

2,File对象。

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

3,OutputStream

4,Writer

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

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

  1. //如果想要既有自动刷新,又可执行编码。如何完成流对象的包装?
  2. PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream("a.txt"),"utf-8"),true);
//如果想要既有自动刷新,又可执行编码。如何完成流对象的包装? PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream("a.txt"),"utf-8"),true);

  1. //如果想要提高效率。还要使用打印方法。
  2. PrintWriter pw = new PrintWriter(new BufferdWriter(new OutputStreamWriter(new FileOutputStream("a.txt"),"utf-8")),true);
//如果想要提高效率。还要使用打印方法。 PrintWriter pw = new PrintWriter(new BufferdWriter(new OutputStreamWriter(new FileOutputStream("a.txt"),"utf-8")),true);


  1. import java.io.*;
  2. public class PrintStreamTest {
  3. public static void main(String[] args) throws IOException {
  4. System.out.println((byte)1);
  5. wirteFile1();
  6. wirteFile2();
  7. readFile2();
  8. readFile22();
  9. }
  10. public static void wirteFile1() throws IOException{
  11. DataOutputStream dos = new DataOutputStream(new FileOutputStream(new File("res/pst1.txt")));
  12. dos.writeDouble(3.1415926);
  13. dos.close();
  14. }
  15. public static void wirteFile2() throws IOException{
  16. PrintStream ps = new PrintStream(new FileOutputStream(new File("res/pst2.txt")));
  17. ps.print(3.1415926);
  18. ps.print("北京");
  19. ps.close();
  20. }
  21. public static void readFile2() throws IOException {
  22. FileInputStream fis =new FileInputStream("res/pst2.txt");
  23. int len = fis.available();
  24. byte[] buf = new byte[len];
  25. while(fis.read(buf) != -1){
  26. System.out.println(new String(buf));
  27. }
  28. fis.close();
  29. }
  30. public static void readFile22() throws IOException{
  31. BufferedReader br = new BufferedReader( new FileReader("res/pst2.txt"));
  32. String content = null;
  33. while((content = br.readLine()) != null){
  34. System.out.println(content);
  35. }
  36. br.close();
  37. }
  38. }
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方法。该方法是阻塞式的,也就是没有数据的情况,该方法会等待。

  1. //sender.java
  2. import java.io.*;
  3. import java.util.*;
  4. public class Sender extends Thread {
  5. PipedOutputStream out = new PipedOutputStream();
  6. public PipedOutputStream getOut() {
  7. return out;
  8. }
  9. public void run() {
  10. String str = "Hello,receiver ! I`m sender\n ";
  11. try {
  12. for(int i=0; i<10; i++)
  13. out.write(str.getBytes());
  14. out.close();
  15. } catch (Exception e) {
  16. e.printStackTrace();
  17. }
  18. }
  19. }
  20. //Receiver.java
  21. import java.io.PipedInputStream;
  22. import java.util.*;
  23. public class Receiver extends Thread {
  24. PipedInputStream in = new PipedInputStream();
  25. public PipedInputStream getIn() {
  26. return in;
  27. }
  28. public void run() {
  29. byte[] buf = new byte[1024];
  30. try {
  31. int len = in.read(buf);
  32. System.out.println("the following is from sender:\n "
  33. + new String(buf, 0, len));
  34. in.close();
  35. } catch (Exception e) {
  36. e.printStackTrace();
  37. }
  38. }
  39. }
  40. //TestPiped
  41. import java.io.PipedInputStream;
  42. import java.io.PipedOutputStream;
  43. class TestPiped {
  44. public static void main(String[] args) {
  45. Sender s = new Sender();
  46. Receiver r = new Receiver();
  47. PipedOutputStream out = s.getOut();
  48. PipedInputStream in = r.getIn();
  49. try {
  50. in.connect(out);
  51. s.start();
  52. r.start();
  53. } catch (Exception e) {
  54. e.printStackTrace();
  55. }
  56. }
  57. }
//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:

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

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

两个构造函数:

1,SequenceInputStream(InputStream in1,InputStream in2)

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

2,SequenceInputStream(Enumeration<? extends InputStream> en)

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

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

  1. //将两个文件拼接为一个流进行依次读取
  2. import java.io.*;
  3. public class DataIODemo1 {
  4. public static void main(String[] args) throws IOException {
  5. FileInputStream fis1 = new FileInputStream("res/a.txt");
  6. FileInputStream fis2 = new FileInputStream("res/number.txt");
  7. SequenceInputStream sis = new SequenceInputStream(fis1, fis2);
  8. int ch;
  9. while((ch = sis.read()) != -1){
  10. System.out.print((char)ch);
  11. }
  12. sis.close();
  13. fis1.close();
  14. fis2.close();
  15. }
  16. }
//将两个文件拼接为一个流进行依次读取 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呢?

  1. ArrayList<FileInputStream > al = new ArrayList<FileInputStream>();
  2. for(int x=1; x<4; x++)
  3. al.add(new FileInputStream(x+".txt"));
  4. //返回按适当顺序在列表的元素上进行迭代的迭代器。
  5. final Iterator<FileInputStream> it = al.iterator();
  6. Enumeration<FileInputStream> en = new Enumeration<FileInputStream>() {
  7. public boolean hasMoreElements() {
  8. return it.hasNext();
  9. }
  10. public FileInputStream nextElement() {
  11. return it.next();
  12. }
  13. };
  14. //多个流就变成了一个流,这就是数据源。
  15. SequenceInputStream sis = new SequenceInputStream(en);
  16. //创建数据目的。
  17. FileOutputStream fos = new FileOutputStream("4.txt");
  18. byte[] buf = new byte[1024*4];
  19. int len = 0;
  20. while((len=sis.read(buf))!=-1)
  21. {
  22. fos.write(buf,0,len);
  23. }
  24. fos.close();
  25. sis.close();
  26. //如果要一个对文件数据切割。
  27. 一个读取对应多了输出。
  28. FileInputStream fis = new FileInputStream("1.mp3");
  29. FileOutputStream fos = null;
  30. byte[] buf = new byte[1024*1024];//是一个1m的缓冲区。
  31. int len = 0;
  32. int count = 1;
  33. while((len=fis.read(buf))!=-1)
  34. {
  35. fos = new FileOutputStream((count++)+".part");
  36. fos.write(buf,0,len);
  37. fos.close();
  38. }
  39. fis.close();
  40. //这样就是将1.mp3文件切割成多个碎片文件。
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);

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

  1. import java.io.IOException;
  2. import java.io.UnsupportedEncodingException;
  3. public class CodeTest {
  4. public static void main(String[] args) throws IOException {
  5. String s = "你好";
  6. // 编码。
  7. byte[] b = s.getBytes("GBK");
  8. System.out.println(new String(b, "GBK"));
  9. // 解码。
  10. String s1 = new String(b, "iso8859-1");
  11. System.out.println(s1);// ????
  12. // 想要还原。
  13. /*
  14. * 对s1先进行一次解码码表的编码。获取原字节数据。 然后在对原字节数据进行指定编码表的解码。
  15. */
  16. byte[] b1 = s1.getBytes("iso8859-1");
  17. System.out.println(new String(b1, "GBK"));
  18. }
  19. }
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提交方式。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值